Skip to content
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

getImageData not working for loaded image #787

Closed
badjano opened this issue Jun 26, 2016 · 2 comments
Closed

getImageData not working for loaded image #787

badjano opened this issue Jun 26, 2016 · 2 comments

Comments

@badjano
Copy link

badjano commented Jun 26, 2016

var Canvas = require('canvas')
var Image = Canvas.Image;
var fs = require('fs');

fs.readFile(__dirname + '/img/image.png', function(err, data){
if (err) throw err;
var img = new Image();
img.onload = function(){
var canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
var imageData = ctx.getImageData();
}
img.src = data;
});

// LOG BELOW

var imageData = ctx.getImageData();
^

Error: IndexSizeError: The source width is 0.
at Error (native)
at img.onload (/MyProject/test.js:12:25)
at Image.src (/MyProject/node_modules/canvas/lib/image.js:30:17)
at /MyProject/index.js:40:11
at FSReqWrap.readFileAfterClose as oncomplete

@zbjornson
Copy link
Collaborator

getImageData takes four parameters: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData. You probably want getImageData(0, 0, img.width, img.height).

@badjano
Copy link
Author

badjano commented Jun 27, 2016

I'm sorry, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants