Home

Awesome

GifCreator

A simple and lightweight Node.js server-side GIF rendering tool. This tool utilizes FFmpeg to compose GIF images, offering easy operation and fast production, making it suitable for applications that require quick GIF animation generation.

Installation

Install this module via npm:

$ npm install gifcreator

About ffmpeg

Please note that this package relies on ffmpeg, so you need to install ffmpeg before using it.

The methods for installing FFmpeg on various Linux distributions differ slightly. Here are the installation steps for some common Linux distributions:

Usage Example

Creating a GIF Animation

The following example demonstrates how to use the GifCreator class to create a GIF animation with multiple frames and add images from a file path pattern.

const path = require('path');
const { createCanvas } = require('canvas');
const GifCreator = require('gifcreator');

function createGif() {
  const width = 500;
  const height = 500;
  const gifCreator = new GifCreator(width, height, true);
  const outputPath = path.resolve(__dirname, 'output.gif');
  gifCreator.setOutput(outputPath);
  gifCreator.setRepeat(true);
  gifCreator.setQuality(10);
  gifCreator.setFrameRate(18);
  gifCreator.on('success', () => {});

  for (let i = 0; i < 50; i++) {
    const canvas = createCanvas(width, height);
    const ctx = canvas.getContext('2d');

    ctx.fillStyle = 'blue';
    ctx.beginPath();
    ctx.arc(50 + (i % 50) * 8, 250, 30, 0, Math.PI * 2);
    ctx.fill();
    ctx.save();
    ctx.translate(250, 250);
    ctx.rotate((i * Math.PI) / 40);
    ctx.fillStyle = 'red';
    ctx.fillRect(-50, -50, 100, 100);
    ctx.restore();
    gifCreator.addFrame(canvas);
  }

  gifCreator.start();
}

Other uses

GifCreator also supports image input and video input, for example:

const imagePattern = path.resolve(__dirname, 'images', 'h%d.png');
gifCreator.addImage(imagePattern);
// or
const video = path.resolve(__dirname, 'video.mp4');
gifCreator.addVideo(video);
<p align="center"> <img src="https://github.com/drawcall/gifcreator/blob/main/examples/demo/1.gif?raw=true" width="28%" /> <img src="https://github.com/drawcall/gifcreator/blob/main/examples/demo/2.gif?raw=true" width="28%" style="margin: 0 2%;" /> <img src="https://github.com/drawcall/gifcreator/blob/main/examples/demo/3.gif?raw=true" width="28%" /> </p>

API and Documentation

Constructor

new GifCreator(width, height, verbose = false)

Methods

setOutput(outputPath)

Sets the output path for the GIF.

setRepeat(repeat)

Sets the repeat option for the GIF.

setDelay(delay)

Sets the delay between frames in the GIF.

setFrameRate(frameRate)

Sets the frame rate for the GIF.

setTransparent(color)

Sets the transparent color for the GIF.

setQuality(quality)

Sets the quality of the GIF.

setFfmpegPath()

Sets the path for the FFmpeg executable.

addFrame(imageData)

Adds a frame to the GIF.

addImage(imageFiles)

Adds images to the GIF.

addVideo(videoPath)

Adds a video to the GIF.

start()

Starts the FFmpeg process to create the GIF.

destroy()

Destroys the FFmpeg command and cleans up resources.

Events

success

Emitted when the FFmpeg process finishes successfully.

error

Emitted when there is an error during the FFmpeg process.

Contributing

GifCreator is an OPEN Open Source Project. This means that: Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit.

License

MIT LICENSE