A Javascript library for creating animated GIFs
Include dist/Animated_GIF.js
in your HTML.
var imgs = document.querySelectorAll('img');
// change workerPath to point to where Animated_GIF.worker.js is
var ag = new Animated_GIF({ workerPath: 'dist/Animated_GIF.worker.js' });
ag.setSize(320, 240);
for(var i = 0; i < imgs.length; i++) {
ag.addFrame(imgs[i]);
}
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!
You can also use the minified versions in dist/
: dist/Animated_GIF.min.js
. Remember to set the worker path to dist/Animated_GIF.worker.min.js
!
These are set 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 workers to use. Default is 2.workerPath
: path to theAnimated_GIF.worker.js
file (orAnimated_GIF.worker.min.js
). Default isdist/Animated_GIF.worker.js
, change accordingly if you place the files somewhere else thandist
.
Check the tests_* files:
Some sites and apps using it:
- 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