|
1 | 1 | import p5 from '../../../src/app.js';
|
2 | 2 | import { server } from '@vitest/browser/context'
|
| 3 | +import pixelmatch from 'pixelmatch' |
3 | 4 | import { THRESHOLD, DIFFERENCE, ERODE } from '../../../src/core/constants.js';
|
4 | 5 | const { readFile, writeFile } = server.commands
|
5 | 6 |
|
@@ -104,39 +105,22 @@ export async function checkMatch(actual, expected, p5) {
|
104 | 105 | );
|
105 | 106 | }
|
106 | 107 |
|
107 |
| - const expectedWithBg = p5.createGraphics(expected.width, expected.height); |
108 |
| - expectedWithBg.pixelDensity(1); |
109 |
| - expectedWithBg.background(BG); |
110 |
| - expectedWithBg.image(expected, 0, 0); |
| 108 | + const diffData = actual.drawingContext.createImageData(actual.width, actual.height); |
| 109 | + const diffCount = pixelmatch( |
| 110 | + actual.drawingContext.getImageData(0, 0, actual.width, actual.height).data, |
| 111 | + expected.drawingContext.getImageData(0, 0, actual.width, actual.height).data, |
| 112 | + diffData.data, |
| 113 | + actual.width, |
| 114 | + actual.height, |
| 115 | + { threshold: 0.4 } |
| 116 | + ); |
111 | 117 |
|
112 | 118 | const cnv = p5.createGraphics(actual.width, actual.height);
|
113 |
| - cnv.pixelDensity(1); |
114 |
| - cnv.background(BG); |
115 |
| - cnv.image(actual, 0, 0); |
116 |
| - cnv.blendMode(DIFFERENCE); |
117 |
| - cnv.image(expectedWithBg, 0, 0); |
118 |
| - for (let i = 0; i < shiftThreshold; i++) { |
119 |
| - cnv.filter(ERODE, false); |
120 |
| - } |
121 |
| - const diff = cnv.get(); |
| 119 | + cnv.drawingContext.putImageData(diffData, 0, 0) |
| 120 | + const diff = cnv.get() |
122 | 121 | cnv.remove();
|
123 |
| - diff.loadPixels(); |
124 |
| - expectedWithBg.remove(); |
125 |
| - |
126 |
| - let ok = true; |
127 |
| - for (let i = 0; i < diff.pixels.length; i += 4) { |
128 |
| - let diffSum = 0; |
129 |
| - for (let off = 0; off < 3; off++) { |
130 |
| - diffSum += diff.pixels[i+off] |
131 |
| - } |
132 |
| - diffSum /= 3; |
133 |
| - if (diffSum > COLOR_THRESHOLD) { |
134 |
| - ok = false; |
135 |
| - break; |
136 |
| - } |
137 |
| - } |
138 | 122 |
|
139 |
| - return { ok, diff }; |
| 123 | + return { ok: diffCount === 0, diff }; |
140 | 124 | }
|
141 | 125 |
|
142 | 126 | /**
|
|
0 commit comments