-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support img.src = <url> #945
Comments
I assume you are asking how to load an image into node-canvas from a URL? You first have to use an HTTP client library to fetch the URL, and get yourself a Here is an example using my own pixl-request HTTP library, but there are tons of others available which can do this as well (request, got, superagent, etc.). var fs = require('fs');
var Request = require('pixl-request');
var Canvas = require('canvas');
var Image = Canvas.Image;
var url = "http://placehold.it/256x256/0000ff/ffffff&text=node-canvas";
var request = new Request();
request.get( url, function(err, resp, data) {
if (err) throw err;
var img = new Image();
img.src = data;
var canvas = new Canvas(256, 256);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, 256, 256);
canvas.createPNGStream().pipe(fs.createWriteStream('image-url.png'));
}); This should produce Good luck to you! - Joe |
Also, this really should be supported directly by node-canvas to mimic how it's working in the browser. I'll try and add it to the 2.x release as soon as possible... Something like this should work (although it doesn't at the moment) const { createCanvas, loadImage } = require('canvas')
const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')
loadImage('http://placehold.it/256x256/0000ff/ffffff&text=node-canvas').then((image) => {
ctx.drawImage(image, 50, 0, 70, 70)
console.log('<img src="' + canvas.toDataURL() + '" />')
}) |
Following @jhuckaby method, I got this error. Error: Image given has not completed loading It seems because I call it in asynchronous function. How can I resolve this? I add node-canvas version: 2.0.0-alpha.1 |
@RyuukiBeat Try using |
The way I did this was to subclass the Image object. Note that I'm using restify in another part of my app, but that doesn't matter to this example:
|
Fixes Automattic#945 - support img.src = <url> Fixes Automattic#807 - support for non-base64 `data:` URIs Fixes Automattic#1079 - ditto Closes Automattic#564 - it works, probably didn't pass a string or buffer Makes all examples and the README use onload, onerror.
Fixes Automattic#945 - support img.src = <url> Fixes Automattic#807 - support for non-base64 `data:` URIs Fixes Automattic#1079 - ditto Closes Automattic#564 - it works, probably didn't pass a string or buffer Makes all examples and the README use onload, onerror.
Fixes Automattic#945 - support img.src = <url> Fixes Automattic#807 - support for non-base64 `data:` URIs Fixes Automattic#1079 - ditto Closes Automattic#564 - it works, probably didn't pass a string or buffer Makes all examples and the README use onload, onerror.
Fixes Automattic#945 - support img.src = <url> Fixes Automattic#807 - support for non-base64 `data:` URIs Fixes Automattic#1079 - ditto Closes Automattic#564 - it works, probably didn't pass a string or buffer Makes all examples and the README use onload, onerror.
Give me a example i stuck in pleaaz
The text was updated successfully, but these errors were encountered: