Created
August 7, 2015 17:26
-
-
Save tmaiaroto/73d281ca9115b6ea747f to your computer and use it in GitHub Desktop.
Render image to Canvas in Node.js example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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