canvas-gif-encoder
A package to encode animated GIFs. Input frames are provided through a canvas, and the output is provided through a ReadStream
.
Installation
Install using npm:
npm i canvas-gif-encoder
Example
const CanvasGifEncoder = ;const createCanvas = ;const fs = ; const canvas = ;const ctx = canvas;const encoder = 120 120; let stream = fs;encoder; encoder; ctxfillStyle = 'black';ctx;encoder; let colors = 'white' 'yellow' 'cyan' 'lime' 'magenta' 'red' 'blue'; for let i = 0; i < colorslength; ++i ctxfillStyle = colorsi; ctx; encoder; encoder;
Documentation
In order to write a valid GIF file, the workflow is as follows:
- Construct an encoder using the
new
keyword - Open a read stream using
createReadStream
- Begin a file with
begin
- Draw the desired frame on a canvas
- Create a frame from the canvas with
addFrame
- Repeat steps 4 and 5 until you have all the frames you want
- End the file using
end
constructor(width, height, options)
- width: The width of the GIF in pixels, in the range [1, 65535].
- height: The height of the GIF in pixels, in the range [1, 65535].
- options: (optional) Currently unused.
Creates an encoder of the specified width and height.
begin()
Initializes the file by writing the header.
addFrame(context, delay)
- context: The canvas context from which the frame is constructed.
- delay: The length of the frame being added in milliseconds, in the range [0, 655350].
Writes a frame to the file.
end()
Finalizes the file by writing the trailer.
createReadStream()
Returns a read stream to which the other methods write.