Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js, canvas: Image given has not completed loading

I have view all related issues, but do not find answer. When i'm using this code in node.js:

var canvas = new global.canvas(400, 400), ctx = canvas.getContext('2d');
global.fs.readFile('test.png', function (err, data) {
    if (!err) {
        var base_image = new global.canvas.Image;
        base_image.src = data;
        ctx.drawImage(base_image, 0, 0, 100, 100);        
    }
    else
        console.log(err);
});

I'm getting error: Image given has not completed loading When i'm add onload event, like this:

base_image.onload = function () {
            ctx.drawImage(base_image, 0, 0, 100, 100);
        }

It's never fires at all. Please help me to find answer.

like image 326
user2955324 Avatar asked Nov 10 '22 16:11

user2955324


1 Answers

Instead of

img.src = buffer
img.onload = function(){ ... }    

do the following:

img.onload = function(){ ... } 
img.src = buffer
like image 110
Stepan Yakovenko Avatar answered Nov 14 '22 22:11

Stepan Yakovenko