Skip to content

Instantly share code, notes, and snippets.

@bootrino
Created August 30, 2018 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootrino/1a7ba46276fb17fcd48ba1f4c0988445 to your computer and use it in GitHub Desktop.
Save bootrino/1a7ba46276fb17fcd48ba1f4c0988445 to your computer and use it in GitHub Desktop.
nodejs buildgif fragment
async function buildGIF(frames: any) {
let firstFrame = new PNG(frames[0].dataBytes);
let width = firstFrame.width;
let height = firstFrame.height;
//console.log(frames.length + " frames at " + width + "x" + height);
let encoder = new GIFEncoder(width, height);
encoder.createReadStream().pipe(fS.createWriteStream('output.gif'));
encoder.start();
let framePromises = frames.map(async (frame: any) => {
return await new Promise((resolve: any, reject: any) => {
let parsedFrame = new PNG(frame.dataBytes);
parsedFrame.decode((px: any) => {
resolve(px);
});
}).catch(e => {
//console.log(e);
});
});
for (let framePromise of framePromises) {
encoder.addFrame(await
framePromise
);
}
encoder.finish();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment