Skip to content

Instantly share code, notes, and snippets.

@giacecco
Created October 30, 2013 15:16
Show Gist options
  • Save giacecco/7234364 to your computer and use it in GitHub Desktop.
Save giacecco/7234364 to your computer and use it in GitHub Desktop.
Strange behaviour of node-canvas that I can't understand. I expect this code to write 10 .png files each of which contains a "Page [n]" text. Instead, I get one .png only called page11.png (??? the last should be 10) with "Page 8" in it! There must be something about timing that I am missing.
{
"name": "test",
"version": "0.0.1",
"author": "Gianfranco Cecconi <[email protected]>",
"description": "Does what it says on the tin",
"contributors": [
{
"name": "Gianfranco Cecconi",
"email": "[email protected]"
}
],
"keywords": [
"test"
],
"dependencies" : {
"canvas": ">=1.1.1",
"optimist": ">=0.6.0"
},
"license": "MIT",
"engines": {
"node": ">=0.10.18"
}
}
var fs = require('fs'),
Canvas = require('canvas'),
argv = require('optimist')
.usage('Usage: $0 --out [output folder]')
.demand([ 'out' ])
.alias('out', 'o')
.argv;
for(var page = 1; page <= 10; page++) {
var canvas = new Canvas(200, 200),
ctx = canvas.getContext('2d');
ctx.font = '30px Impact';
ctx.fillText("Page " + page, 50, 100);
canvas.toBuffer(function (err, buf) {
if (err) throw err;
fs.writeFile(argv.out + '/page' + page + '.png', buf);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment