Skip to content

Instantly share code, notes, and snippets.

@tmaiaroto
Created August 7, 2015 17:26
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 tmaiaroto/73d281ca9115b6ea747f to your computer and use it in GitHub Desktop.
Save tmaiaroto/73d281ca9115b6ea747f to your computer and use it in GitHub Desktop.
Render image to Canvas in Node.js example
request({url: opts.url, method: 'GET', headers: { 'user-agent': opts.userAgent}, encoding: null}, function (error, response, body) {
if(!error && response.statusCode == 200) {
var img = new Image
, canvas = new Canvas
, ctx = canvas.getContext('2d');
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
var n = 5; // number of colors
var colors = palette(canvas, n);
// ... do some more stuff
return someData;
};
img.onerror = function() {
// ... probably want to handle this
};
img.src = body;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment