From d9250ade064f7510dbb51336520401b63f5e0f67 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Thu, 20 May 2021 08:18:50 +0300 Subject: [PATCH] Update toEqualImageData to verify size (#11) --- src/matchers.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/matchers.js b/src/matchers.js index 23f7f1c..c398408 100644 --- a/src/matchers.js +++ b/src/matchers.js @@ -41,7 +41,9 @@ function buildPixelMatchPreview(actual, expected, diff, threshold, tolerance, co var item = document.createElement('div'); item.style.cssText = 'text-align: center; font: 12px monospace; line-height: 1.4; margin: 8px'; item.innerHTML = '
' + values.label + '
'; - item.appendChild(canvasFromImageData(values.data)); + var canvas = canvasFromImageData(values.data); + canvas.style.cssText = 'border: 1px dashed red'; + item.appendChild(canvas); wrapper.appendChild(item); }); @@ -178,7 +180,7 @@ function toEqualImageData() { var debug = opts.debug || false; var tolerance = opts.tolerance === undefined ? 0.001 : opts.tolerance; var threshold = opts.threshold === undefined ? 0.1 : opts.threshold; - var ctx, idata, ddata, w, h, count, ratio; + var ctx, idata, ddata, w, h, aw, ah, count, ratio; if (actual instanceof Chart) { ctx = actual.ctx; @@ -191,9 +193,15 @@ function toEqualImageData() { if (ctx) { h = expected.height; w = expected.width; - idata = ctx.getImageData(0, 0, w, h); + aw = ctx.canvas.width; + ah = ctx.canvas.height; + idata = ctx.getImageData(0, 0, aw, ah); ddata = createImageData(w, h); - count = pixelmatch(idata.data, expected.data, ddata.data, w, h, {threshold: threshold}); + if (aw === w && ah === h) { + count = pixelmatch(idata.data, expected.data, ddata.data, w, h, {threshold: threshold}); + } else { + count = Math.abs(aw * ah - w * h); + } ratio = count / (w * h); if ((ratio > tolerance) || debug) {