I would like to compare two images with javascript. One is drawn on a html5 canvas the other might be on another html5 canvas, or it might be a gif- or png-image-file. I need the comparision for automated testing. Thanks for help!
Asked
Active
Viewed 4,916 times
1 Answers
10
alright this isn't a complete solution for your problem but it might help you find a good way to compare the two canvas elements.
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var pixels = imageData.data;
for(var i = 0, il = pixels.length; i < il; i++) {
// pixels[i]
}
This is a basic example how to loop through all the pixels on the canvas. Depending on what you want to do you can compare the pixels with each other or you might want to compare the two imageData
vars with each other

Manuel van Rijn
- 10,170
- 1
- 29
- 52
-
getImageData() was the function I was looking for... Thanks! – user1027167 Dec 02 '11 at 10:19
-
Most of the time this won't work ( except Webkit?), see http://stackoverflow.com/questions/4309364/ – NoBugs Dec 17 '14 at 07:44