• Stars
    star
    225
  • Rank 176,171 (Top 4 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Javascript library for creating animated GIFs

Animated_GIF NPM Version

A Javascript library for creating animated GIFs

How to use it?

Include dist/Animated_GIF.js in your HTML.

var imgs = document.querySelectorAll('img');

var ag = new Animated_GIF(); 
ag.setSize(320, 240);

for(var i = 0; i < imgs.length; i++) {
    ag.addFrame(imgs[i], { delay: 1000 });
}

var animatedImage = document.createElement('img');

// This is asynchronous, rendered with WebWorkers
ag.getBase64GIF(function(image) {
    animatedImage.src = image;
    document.body.appendChild(animatedImage);
});

If you instance lots of Animated_GIF objects, it's strongly recommended that you call their destroy method once you're done rendering the GIFs, as browsers don't seem to be happy otherwise. See the stress test for an example of this in use!

Minified versions

There's a minified version in dist/: dist/Animated_GIF.min.js.

Using from npm

You can also use this via npm.

To install:

npm install --save animated_gif

To use:

var Animated_GIF = require('animated_gif');

// And then the examples are as before
var ag = new Animated_GIF(); 
ag.setSize(320, 240);

// ... etc

Available options

Pass an object with the desired values when creating an Animated_GIF instance:

  • sampleInterval: how many pixels to skip when creating the palette. Default is 10. Less is better, but slower.
  • numWorkers: how many web workers to use. Default is 2.
  • useQuantizer: this is true by default, and provides the highest quality results, at the cost of slower processing and bigger files. When this is enabled, a neural network quantizer will be used to find the best palette for each frame. No dithering is available in this case, as the colours are chosen with the quantizer too.
  • dithering: selects how to best spread the error in colour mapping, to conceal the fact that we're using a palette and not true color. Note that using this option automatically disables the aforementioned quantizer. Best results if you pass in a palette, but if not we'll create one using the colours in the first frame. Possible options:
    • bayer: creates a somewhat nice and retro 'x' hatched pattern
    • floyd: creates another somewhat retro look where error is spread, using the Floyd-Steinberg algorithm
    • closest: actually no dithering, just picks the closest colour from the palette per each pixel
  • palette: An array of integers containing a palette. E.g. [ 0xFF0000, 0x00FF00, 0x0000FF, 0x000000 ] contains red, green, blue and black. The length of a palette must be a power of 2, and contain between 2 and 256 colours.

addFrame options

  • delay: set frame delay. Default is Animated_GIF instance delay.

Tests and examples

Check the files in the tests folder:

Start the server from the root folder (e.g. Animated_GIF). One way of doing it is using the simple Python web server:

python -m SimpleHTTPServer

starts a server in http://localhost:8000. So you can now go to http://localhost:8000/tests/ and see the available examples.

Contributing / walkthrough

Here's a quick walkthrough of each of the files in src/ and what they do:

  • Animated_GIF.js - definition of the Animated_GIF class. Holds the logic for the queueing and rendering of the files, and parsing config options.
  • Animated_GIF.worker.js - code for the web worker that color-indexes frames in the background, using node-dithering and NeuQuant.js. This is bundled in dist/Animated_GIF.js, using workerify.
  • main.js - stub in order to export the library using Browserify (you won't generally need to touch this one)

External / included libraries --see Credits for more information on these. You generally don't want to touch these because it will make very difficult to track updates in those libraries:

  • lib/NeuQuant.js - color quantizer based on a neural network algorithm, this is an external library
  • omggif.js - GIF89 encoder/decoder
  • node-dithering - class with three different types of dithering algorithms

Rebuild dist files

If you made changes in the library, you'll need to rebuild the files in dist/ in order to see the changes working. We have a node.js-based script to regenerate those files.

Once node.js is installed in your system, do:

cd Animated_GIF     # or however you cloned the library to
npm install         # this pulls dependencies for building (uglify, browserify)
npm run build       # and this actually builds

Once you do the initial two steps you just need to execute npm run build whenever you change things and want to rebuild the files in dist/. Or you can also use npm run watch to have it build the library automatically.

Credits

We're using these fantastic libraries to do GIF stuff:

  • Anthony Dekker's NeuQuant image quantization algorithm which was ported from C into Java by Kevin Weiner and then to ActionScript 3 by Thibault Imbert, and to JavaScript by antimatter15, and fixed, patched and revised by sole.
  • Dean McNamee's omggif library - for actually encoding into GIF89
  • sole's node-dithering.

And then, to build the dist files

  • node.js
  • uglify
  • browserify

More Repositories

1

aafm

Android ADB file manager
Python
215
star
2

gifwall

gif demo
JavaScript
53
star
3

node-zip-folder

zips a folder and calls your callback when it's done
JavaScript
44
star
4

audio-tags

audio tags
JavaScript
44
star
5

three.js-tutorials

the source code for my three.js tutorials
JavaScript
40
star
6

gumhelper

A module wrapping WebRTC's getUserMedia
JavaScript
35
star
7

rtcamera

A fun camera app to process images in real time, using Web technologies.
JavaScript
29
star
8

MIDIUtils

A series of methods to work with MIDI
JavaScript
28
star
9

demoscene

sole's demoscene projects
C++
28
star
10

phpMyID

A fork of phpMyID that works on php 5.4
PHP
27
star
11

sorollet.js

Monophonic additive synthesiser
JavaScript
27
star
12

howa

Hands On Web Audio slides
JavaScript
25
star
13

to_the_beat

to_the_beat // js
JavaScript
22
star
14

mvsd

minimally viable slide deck
JavaScript
21
star
15

sorollet

Monophonic additive synth written in C++. VSTi version + stand-alone player included.
C++
18
star
16

snippets

Code snippets. Miscellaneous purposes and languages.
HTML
18
star
17

npm_three_template

A template for using three.js with node.js modules and browserify and gulp and etc
JavaScript
13
star
18

munit.js

microunit testing for javascript
JavaScript
12
star
19

webcam-dithering

Realtime webcam dithering effect with WebGL
HTML
11
star
20

catserver

cats. cats everywhere.
JavaScript
11
star
21

node-dithering

various dithering algorithms written in JavaScript.
JavaScript
10
star
22

the-p2p-web

sole's researching stuff
10
star
23

dotvim

Sole's .vim directory
Vim Script
9
star
24

luisita

scriptable graphics environment à la Processing, only it's based on Lua & C++
C
8
star
25

install-to-adb

Installing apps to Firefox OS devices connected via USB+ADB
JavaScript
8
star
26

WebComponentsMonkeyPatch

One monkey to patch them all.
JavaScript
7
star
27

theremin.js

SPOOKY
JavaScript
7
star
28

kindle-clippings-parser

A node.js module to parse the clippings from a Kindle device.
JavaScript
7
star
29

arewedubstepyet.com

tracking the status of the web audio editor in Firefox
JavaScript
6
star
30

webaudio-tutorials

web audio tutorials
JavaScript
5
star
31

AudioDetector.js

AudioDetector.js is a small Javascript library for detecting audio capabilities
JavaScript
5
star
32

test_cases

Attaching a good test case with your bug report is a godssend for QA and developers. If you want to learn how to write test cases, feel free to have a look at this repo.
JavaScript
5
star
33

fxos-guide

you guessed it right!
JavaScript
5
star
34

StringFormat.js

Simple Javascript library to format strings
JavaScript
5
star
35

brickpresso

A simple preso tool built with Mozilla Brick
JavaScript
4
star
36

instalooper

Technical Web Audio + MediaRecorder demo
JavaScript
4
star
37

renoise-node

the home for the node.js Renoise module
JavaScript
4
star
38

jranular

granular-synths-inspired javascript toy
4
star
39

cinnabarify

Cinnabarify left, right and centre
Shell
4
star
40

node-quneo

Making it easy to control a QuNeo controller with OSC messages
JavaScript
4
star
41

raining-rainbows

inspired by @harthur's avatar
JavaScript
4
star
42

perriscript

guau vaya sintaxis tela perri muy futuro
JavaScript
4
star
43

SyHi

Minimalistic Syntax Highlighter plug-in for WordPress which also makes sure code can still be copied and pasted into your favourite compiler!
PHP
4
star
44

webgl_vga

Homage to a VGA test
JavaScript
3
star
45

triangles

meshes
JavaScript
3
star
46

slides-introduction-webcomponents

An introduction to Web Components
JavaScript
3
star
47

4x4JS

Four to the floor JavaScript - JSConf.EU 2013 talk
JavaScript
3
star
48

cascadiajs2013-slides

slides for cascadiajs 2013 talk
JavaScript
3
star
49

kindle-clippings-html-formatter

A module to format the clippings from a Kindle device into HTML
JavaScript
3
star
50

push-app

Push an app to a client, uninstalling first if need be
JavaScript
3
star
51

lxjs2014

Slides for my talk at LXJS2014
JavaScript
3
star
52

mindblown.js

A framework for creating presentations in 3D, starting with HTML content
JavaScript
3
star
53

cmpsr

we'll see...
JavaScript
3
star
54

3400

"3400 miles below" source code
JavaScript
3
star
55

danger-dashboard

you've been messing with about:config but you don't remember it anymore-you need HELP!
JavaScript
3
star
56

reveal-minimal

A minimal setup for using Reveal.js with npm, Browserify, Jade, etc
JavaScript
3
star
57

mediarecorder-slides

Slides for presentation on MediaRecorder
HTML
3
star
58

disconnected-ensemble-src

JavaScript
2
star
59

raspberrysensor

JavaScript
2
star
60

sole.github.com

www
JavaScript
2
star
61

PageVisibility.js

Simple utility class for listening to page visibility changes
JavaScript
2
star
62

template

no more writing this skeleton from scratch each time!
JavaScript
2
star
63

kindle-clippings-beautify

A module to turn the clippings from a Kindle device into an HTML page that is 'beautiful' (for some particular definition of 'beautiful')
JavaScript
2
star
64

x-livecode

A web component that uses x-editor for massive live coding.
2
star
65

metalsmith-blog-example

An example of using Metalsmith as static blog generator
JavaScript
2
star
66

WebComponentsSupport

a library for detecting current support for the usual features for building web components
JavaScript
2
star
67

pommed-fork

My pommed fork :-)
C
2
star
68

sample-packaged-app

Sample packaged app that can be used to test installing Firefox OS apps
JavaScript
2
star
69

zero-to-hello

A minimal Vite + React + ESlint set up
JavaScript
2
star
70

x-editor

The most generic name ever for a code editor component.
JavaScript
2
star
71

our-projects-in-the-future

Presentation for work week
CSS
1
star
72

slides-devtools-may-2017

Slides for presentation on Firefox Developer Tools
HTML
1
star
73

ck-node-firefox

know your nodes and your firefoxes
JavaScript
1
star
74

glowingnemesis

jukebox
JavaScript
1
star
75

servo-gradient

JavaScript
1
star
76

supergear

For lack of a better name
JavaScript
1
star
77

advent-of-code-2021

Not sure this is a good idea, but hey
JavaScript
1
star
78

TinajaApp

Le hablas y te amplifica … con ecos y aplausos
JavaScript
1
star
79

multilingual-tts

Demonstration project for creating mp3 from a number of text files in different languages
Shell
1
star
80

MACCHINA_I

endless algorithmically built textures, right in your browser!
JavaScript
1
star
81

wp-reliably-heic

A WordPress plugin to reliably handle HEIC files
PHP
1
star
82

bitmapiser

SVG ⟶ Bitmap
JavaScript
1
star
83

p2p-chat

experimenting with a p2p chat using wifidirect on Firefox OS
JavaScript
1
star
84

networkInfo

shows network information
JavaScript
1
star
85

art-prompts

Random art prompts for a 365 day project
HTML
1
star
86

eyes

👁
HTML
1
star
87

deathlynemesis

An idea I just had right now and shouldn't be having because I have sh*tloads of work to do, but noting it down anyway so I can pick it in the nearby future. Yay!
1
star
88

publish-and-cron

a simple repository to test publishing to github and using travis cron as well
JavaScript
1
star
89

slides-ff2014

Slides for my talk at Full Frontal 2014
JavaScript
1
star
90

cjs14-embracing-failure

slides for my talk at CascadiaJS 2014
JavaScript
1
star
91

readimagetest

why is this broken, bryce? HALP
JavaScript
1
star
92

npm_app_template

a template for building an app using npm modules + browserify + gulp
JavaScript
1
star
93

firefox-os-developerify

Put your Firefox OS device into super developer mode
JavaScript
1
star
94

slides-dotjs2014

Slides for my talk at dotJS 2014
JavaScript
1
star
95

dotfiles

the little files that make us happy
1
star
96

you-know-what

shhht
JavaScript
1
star
97

4x4JS_vol2

Four to the floor JavaScript, vol 2
JavaScript
1
star
98

humacchina

project for mozfest science
JavaScript
1
star
99

querying-bugzilla

How to ask questions from Bugzilla (and hopefully get the answers you want)
1
star
100

generator-firefoxos

FirefoxOS yeoman generator
JavaScript
1
star