summaryrefslogtreecommitdiff
path: root/tools/shapes_web.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/shapes_web.mjs')
-rw-r--r--tools/shapes_web.mjs41
1 files changed, 41 insertions, 0 deletions
diff --git a/tools/shapes_web.mjs b/tools/shapes_web.mjs
new file mode 100644
index 0000000..913bc8b
--- /dev/null
+++ b/tools/shapes_web.mjs
@@ -0,0 +1,41 @@
+// Draws test/shapes.zig in headless Chrome (software Canvas2D, no GPU
+// needed) and compares it with the native host's screenshot of the same
+// frame: equal within a small tolerance everywhere except next to edges,
+// which both antialias, differently. Then times 5000
+// circles a frame, one shape each and batched into one.
+// usage: node tools/shapes_web.mjs <web dir> <native.png> [chrome]
+import {withPage, compareScript, report} from './chrome.mjs';
+import assert from 'node:assert/strict';
+
+const [dir, nativePng, chromePath] = process.argv.slice(2);
+await withPage({dir, extra: {'/native.png': nativePng}, width: 400, height: 400, chromePath}, async evaluate => {
+ // One frame drawn now, then both pictures compared in the page.
+ const points = {circle: [60, 80], overlap: [110, 80], polygon: [300, 40], notch: [300, 120], join: [60, 228], ring: [300, 212], hole: [300, 240], line: [300, 300], above: [300, 299]};
+ const result = await evaluate(`(async () => { sketch.frame(performance.now()); return await ${compareScript(400, 400, 3, points)}; })()`);
+ report(result, 3);
+ // The 1 px line at full intensity, as natively (a body thinned for an
+ // antialiasing fringe would give half).
+ assert.deepEqual(result.sampled.line.web, [0, 0, 0]);
+ assert.deepEqual(result.sampled.above.web, [255, 255, 255]);
+
+ // Frame time with 5000 circles, raster included (getImageData waits for it).
+ const timing = await evaluate(`(() => {
+ const ctx = document.querySelector('canvas').getContext('2d');
+ const time = line => {
+ sketch.command(line);
+ const times = [];
+ for (let i = 0; i < 12; i++) {
+ const t0 = performance.now();
+ sketch.frame(t0);
+ ctx.getImageData(0, 0, 1, 1);
+ times.push(performance.now() - t0);
+ }
+ times.sort((a, b) => a - b);
+ return times[times.length >> 1];
+ };
+ return {separate: time('circles 5000'), batched: time('batched 5000')};
+ })()`);
+ console.log(`5000 circles a frame, median: ${timing.separate.toFixed(1)} ms as 5000 shapes, ${timing.batched.toFixed(1)} ms batched into one`);
+ const budget = Number(process.env.SHAPES_BUDGET_MS ?? 16);
+ for (const [how, ms] of Object.entries(timing)) assert.ok(ms < budget, `5000 circles (${how}) took ${ms.toFixed(1)} ms, over ${budget} ms`);
+});