Skip to content

Instantly share code, notes, and snippets.

@kangax
Created January 31, 2013 19: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 kangax/4685628 to your computer and use it in GitHub Desktop.
Save kangax/4685628 to your computer and use it in GitHub Desktop.
var URL = require('url'),
HTTP = require('http'),
Canvas = require('canvas'),
Image = Canvas.Image,
img = new Image(),
canvas = new Canvas(200,200),
ctx = canvas.getContext('2d'),
// throws "Error: Image given has not completed loading"
url1 = 'http://printio.ru/canvas_images/c9e1973ac9e730a431ad631cddac02bb81e3bd9d.png',
// works
url2 = 'http://content0.printio.ru/assets/designs/large/72f8b6e1c251dd9cd80b927e7c0bfacb072e8bf2.png';
var oURL = URL.parse(url1),
client = HTTP.createClient(oURL.port, oURL.hostname),
req = client.request('GET', oURL.pathname, { 'host': oURL.hostname });
req.on('response', function (response) {
var body = "";
response.setEncoding('binary');
response.on('end', function () {
img.src = new Buffer(body, 'binary');
ctx.drawImage(img, 0, 0, img.width, img.height);
});
response.on('data', function (chunk) {
if (response.statusCode === 200) {
body += chunk;
}
});
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment