I installed node-canvas and imagediff in a local directory and wrote up a test example that compares two already existing image files. It runs just fine with the code below:
var Canvas = require('canvas')
, imagediff = require('../lib/imagediff.js')
, fs = require('fs')
, path = require('path');
var goodFilePath = path.join(__dirname + '/../diffs/single/test_1_good.png')
, newFilePath = path.join(__dirname + '/../diffs/single/test_1_new.png')
, diffFilePath = path.join(__dirname + '/../diffs/single/test_1_diff.png');
var test = function () {
var img_good = new Canvas.Image()
, img_new = new Canvas.Image()
, img_good_data = null
, img_new_data = null
, tolerance = 10
, equal = false
, diff_result = null;
fs.readFile(goodFilePath, function (err, squid_good) {
if (err) throw err;
img_good.src = squid_good;
});
fs.readFile(newFilePath, function (err, squid_new) {
if (err) throw err;
img_new.src = squid_new;
});
img_new.onload = function () {
// convert to image data
img_good_data = imagediff.toImageData(img_good);
img_new_data = imagediff.toImageData(img_new);
//compare and save diffences in new image file
equal = imagediff.equal(img_good, img_new, tolerance);
console.log('Image Good === Image New: ' + equal);
diff_result = imagediff.diff(img_good_data, img_new_data, tolerance);
imagediff.imageDataToPNG(diff_result, diffFilePath);
}
}
test();
but if you noticed the require statement at the top I am pulling in the actual imagediff.js file that I copied into a lib folder that I created. My problem is that when I try to use the regular require('imagediff') statement the programs gives the following Image or Canvas expected error:
/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
context.drawImage(image, 0, 0);
^
TypeError: Image or Canvas expected
at toImageDataFromImage (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120:13)
at toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:108:35)
at Object.imagediff.toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:365:14)
at img_new.onload (/Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:30:45)
at Image.inspect [as src] (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/canvas/lib/image.js:29:17)
at /Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:25:29
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
I have even tried to install the same version of canvas as imagediff uses and it resulted in the following seg fault error:
/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
context.drawImage(image, 0, 0);
^
[1] 34636 segmentation fault node simple.js
My Question:
Why can't I use this module with the normal require? Or why is it working with the file path require statement but failing otherwise?