-
Notifications
You must be signed in to change notification settings - Fork 326
Description
Hello! New user of pixelmatch here -- thanks for making such a nifty library! It's making all my dreams come true! π π¦ β¨ π
But as a new user, I made a silly mistake when working with data from the Canvas API. Instead of passing in img1.data as an argument to pixelmatch(), I passed in img1 directly, which is an ImageData object.
The problem: I got back a return value of 0 and a blank canvas, which made me think there was something wrong with my canvas data itself or other parts of my codebase!
It would be much more helpful if this resulted in an error.
Cause of the problem:
The pixelmatch() function tries to access, for example, img1[pos], but if img1 is an object it will simply give undefined because it has no such property pos. And then pixelmatch() just goes happily about its usual routine, comparing undefined vs undefined many many times and deciding that there are no differences. (Which is technically true, but not the desired behavior.)
Code to replicate the bug:
// First assuming you already have some canvas / context objects
var img1 = ctx.getImageData(0, 0, width, height),
img2 = ctx.getImageData(0, 0, width, height),
diff = ctx.createImageData(width, height);
// Here's the only part different from the example in the README:
// I pass in img1, img2, diff instead of img1.data, img2.data, diff.data
var numPixels = pixelmatch(img1, img2, diff, width, height, {threshold: 0.1});
console.log(numDiffPixels); // returns 0
ctx.putImageData(diff, 0, 0); // draws a blank (white) canvasI may just make a PR myself to offer a first attempt at fixing this. :)
Thanks!
~ Liz