A Javascript library for creating animated GIFs
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!
There's a minified version in dist/
: dist/Animated_GIF.min.js
.
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
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 patternfloyd
: creates another somewhat retro look where error is spread, using the Floyd-Steinberg algorithmclosest
: actually no dithering, just picks the closest colour from the palette per each pixelpalette
: 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.delay
: set frame delay. Default is Animated_GIF
instance delay.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.
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 libraryomggif.js
- GIF89 encoder/decodernode-dithering
- class with three different types of dithering algorithmsdist
filesIf 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.
We're using these fantastic libraries to do GIF stuff:
And then, to build the dist
files