Skip to content
This repository has been archived by the owner on May 28, 2020. It is now read-only.

Latest commit

 

History

History
92 lines (66 loc) · 1.98 KB

README.md

File metadata and controls

92 lines (66 loc) · 1.98 KB

gif.node

Full-featured GIF encoder that runs in node. Based on jnordberg/gif.js which is a Javascript GIF encoder runs in browser.

Installation

Before install this module, you need to install node-canvas first.

After you installed node-canvas, then install this.

$ npm install gif.node

Testing

$ npm run test

Run Demo

$ npm run demo

Then a gif.gif file will be placed in the root directory.

Sample usage

const { loadImage, createCanvas } = require('canvas');
const GIF = require('gif.node');
const fs = require('fs');
const path = require('path');

const images = [
  path.join(__dirname, '../test/images/001.png'),
  path.join(__dirname, '../test/images/002.png'),
  path.join(__dirname, '../test/images/003.png'),
];

const gif = new GIF({
  worker: 2,
  quality: 10,
  debug: true
});

let canvas, cxt;

function addFrames(images, index) {
  return new Promise(resolve => {
    let i = index ? index : 0;

    if (!images[i]) {
      resolve();
    }

    loadImage(images[i]).then(image => {
      if (!canvas) {
        canvas = createCanvas(image.width, image.height);
        cxt = canvas.getContext('2d');
      }

      cxt.drawImage(image, 0, 0);

      gif.addFrame(cxt.getImageData(0, 0, canvas.width, canvas.height), {
        delay: 500 * (1 + i)
      });

      resolve(addFrames(images, ++i));
    });
  });
}

gif.on('finished', buffer => {
  fs.writeFileSync('gif.gif', buffer);
});

addFrames(images).then(() => {
  gif.render();
});

Options

Please see here