MCPcopy
hub / github.com/processing/p5.js / checkMatch

Function checkMatch

test/unit/visual/visualTest.js:131–261  ·  view source on GitHub ↗
(actual, expected, p5)

Source from the content-addressed store, hash-verified

129 */
130
131export function checkMatch(actual, expected, p5) {
132 let scale = Math.min(MAX_SIDE/expected.width, MAX_SIDE/expected.height);
133 const ratio = expected.width / expected.height;
134 const narrow = ratio !== 1;
135 if (narrow) {
136 scale *= 2;
137 }
138
139 for (const img of [actual, expected]) {
140 img.resize(
141 Math.ceil(img.width * scale),
142 Math.ceil(img.height * scale)
143 );
144 }
145
146 // Ensure both images have the same dimensions
147 const width = expected.width;
148 const height = expected.height;
149
150 // Create canvases with background color
151 const actualCanvas = p5.createGraphics(width, height);
152 const expectedCanvas = p5.createGraphics(width, height);
153 actualCanvas.pixelDensity(1);
154 expectedCanvas.pixelDensity(1);
155
156 actualCanvas.background(BG);
157 expectedCanvas.background(BG);
158
159 actualCanvas.image(actual, 0, 0);
160 expectedCanvas.image(expected, 0, 0);
161
162 // Load pixel data
163 actualCanvas.loadPixels();
164 expectedCanvas.loadPixels();
165
166 // Create diff output canvas
167 const diffCanvas = p5.createGraphics(width, height);
168 diffCanvas.pixelDensity(1);
169 diffCanvas.loadPixels();
170
171 // Run pixelmatch
172 const diffCount = pixelmatch(
173 actualCanvas.pixels,
174 expectedCanvas.pixels,
175 diffCanvas.pixels,
176 width,
177 height,
178 {
179 threshold: 0.5,
180 includeAA: false,
181 alpha: 0.1
182 }
183 );
184
185 // If no differences, return early
186 if (diffCount === 0) {
187 actualCanvas.remove();
188 expectedCanvas.remove();

Callers 1

visualTestFunction · 0.85

Calls 10

findClusterSizeFunction · 0.85
imageMethod · 0.80
resizeMethod · 0.45
pixelDensityMethod · 0.45
backgroundMethod · 0.45
loadPixelsMethod · 0.45
removeMethod · 0.45
updatePixelsMethod · 0.45
pushMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected