gif.js

repository·master·Indexed 26 days ago

https://github.com/jnordberg/gif.js

A high-performance JavaScript GIF encoding library (v0.2.0) that uses Web Workers and Typed Arrays to render frames in the background. It provides the GIFEncoder class for creating animated GIFs, LZWEncoder for compression, and NeuQuant/TypedNeuQuant for color quantization.

Tokens
816
Snippets
1
Records
9
Agent score
40%

What's inside gif.js

  1. Set up gif.js

    master
    To use gif.js, include the gif.js file found in the dist/ directory in your web page. You must also ensure that gif.worker.js is located in the same directory as gif.js so the web workers can be loaded correctly.
  2. Configure GIF constructor options

    master

    The GIF constructor accepts an options object to configure the encoder. These options can also be updated later using the setOptions method.

    NameDefaultDescription
    repeat0repeat count, -1 = no repeat, 0 = forever
    quality10pixel sample interval, lower is better
    workers2number of web workers to spawn
    workerScriptgif.worker.jsurl to load worker script from
    background#fffbackground color where source image is transparent
    widthnulloutput image width
    heightnulloutput image height
    transparentnulltransparent hex color, e.g. 0x00FF00
    ditherfalsedithering method (e.g. FloydSteinberg-serpentine)
    debugfalsewhether to print debug information to console
  3. Encode a GIF with gif.js

    master

    Initialize a new GIF instance with desired options, add frames using images, canvas elements, or canvas contexts, and call .render() to start the encoding process. You can listen for the finished event to receive the resulting Blob.

    var gif = new GIF({
      workers: 2,
      quality: 10
    });
    
    // add an image element
    gif.addFrame(imageElement);
    
    // or a canvas element
    gif.addFrame(canvasElement, {delay: 200});
    
    // or copy the pixels from a canvas context
    gif.addFrame(ctx, {copy: true});
    
    gif.on('finished', function(blob) {
      window.open(URL.createObjectURL(blob));
    });
    
    gif.render();
  4. Configure dithering methods

    master

    When setting the dither option, you can use one of the following methods:

    • FloydSteinberg
    • FalseFloydSteinberg
    • Stucki
    • Atkinson

    You can append -serpentine to any method to use serpentine scanning (e.g., Stucki-serpentine).

  5. Import the GIF encoder modules

    master
    The gif.js library exports several core modules for GIF encoding and color quantization. You can require the main entry point to access GIFEncoder, LZWEncoder, and quantization tools like NeuQuant or TypedNeuQuant.