Skip to content

Instantly share code, notes, and snippets.

@ikr7
Created March 7, 2014 03:18
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 ikr7/9404682 to your computer and use it in GitHub Desktop.
Save ikr7/9404682 to your computer and use it in GitHub Desktop.
Node.js でアニメGIF生成
var fs = require('fs');
var Canvas = require('canvas');
var GIFEncoder = require('gifencoder');
var encoder = new GIFEncoder(320, 240);
encoder.start();
encoder.setRepeat(0);
encoder.setDelay(500);
encoder.setQuality(10);
var canvas = new Canvas(320, 240);
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#ff0000';
ctx.fillRect(0, 0, 320, 240);
encoder.addFrame(ctx);
ctx.fillStyle = '#00ff00';
ctx.fillRect(0, 0, 320, 240);
encoder.addFrame(ctx);
ctx.fillStyle = '#0000ff';
ctx.fillRect(0, 0, 320, 240);
encoder.addFrame(ctx);
encoder.finish();
var data = encoder.out.getData();
// gifencoder のREADMEには
// encoder.stream().getData();
// とあるけど、それでは動かないので注意
fs.writeFile('./anime.gif', data, function(err){
if (err) throw err;
console.log('Saved!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment