summaryrefslogtreecommitdiff
path: root/tools/shapes_web.mjs
blob: 913bc8bd6dcde243a1e280439faa2af5340a9d0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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`);
});