|
1 | 1 | import p5 from '../../../src/app.js';
|
2 | 2 | import { server } from '@vitest/browser/context'
|
3 |
| -import pixelmatch from 'pixelmatch' |
4 | 3 | import { THRESHOLD, DIFFERENCE, ERODE } from '../../../src/core/constants.js';
|
5 | 4 | const { readFile, writeFile } = server.commands
|
6 | 5 |
|
@@ -105,22 +104,39 @@ export async function checkMatch(actual, expected, p5) {
|
105 | 104 | );
|
106 | 105 | }
|
107 | 106 |
|
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 |
| - ); |
| 107 | + const expectedWithBg = p5.createGraphics(expected.width, expected.height); |
| 108 | + expectedWithBg.pixelDensity(1); |
| 109 | + expectedWithBg.background(BG); |
| 110 | + expectedWithBg.image(expected, 0, 0); |
117 | 111 |
|
118 | 112 | const cnv = p5.createGraphics(actual.width, actual.height);
|
119 |
| - cnv.drawingContext.putImageData(diffData, 0, 0) |
120 |
| - const diff = cnv.get() |
| 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(); |
121 | 122 | 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 | + } |
122 | 138 |
|
123 |
| - return { ok: diffCount === 0, diff }; |
| 139 | + return { ok, diff }; |
124 | 140 | }
|
125 | 141 |
|
126 | 142 | /**
|
|
0 commit comments