// Painted pixels of one disc (r = span / 4, centre 0.537, 0.463 of the frame) in a 286 x 286 box, // for frame spans from 1 000 down to 2^-20: area and centroid against the f64 expectation. import { createRequire } from 'node:module' const require = createRequire(process.argv[2] + '/apps/app/package.json') const pw = require('@playwright/test') const spans = [[1000, '1000'], [10, '10'], [1, '1'], [0.125, '2^-3'], [0.03125, '2^-5'], [1 / 128, '2^-7'], [1 / 1024, '2^-10'], [1 / 16384, '2^-14'], [1 / 1048576, '2^-20']] const want = { area: Math.PI * 71.5 * 71.5, cx: 0.537 * 286, cy: 0.463 * 286 } for (const engine of process.argv[3].split(',')) { const browser = await pw[engine].launch() const page = await browser.newPage({ viewport: { width: 400, height: 400 } }) console.log(`== ${engine} ${browser.version()} (expected area ${want.area.toFixed(0)} px, centroid ${want.cx.toFixed(2)}, ${want.cy.toFixed(2)})`) for (const [s, label] of spans) { await page.setContent(`
`) const png = (await page.locator('#b').screenshot()).toString('base64') const m = await page.evaluate(async (b64) => { const img = new Image(); img.src = 'data:image/png;base64,' + b64; await img.decode() const c = document.createElement('canvas'); c.width = img.width; c.height = img.height; const x = c.getContext('2d'); x.drawImage(img, 0, 0) const d = x.getImageData(0, 0, c.width, c.height).data; let a = 0, sx = 0, sy = 0 for (let i = 0; i < d.length; i += 4) { const w = (255 - d[i + 1]) / 255; if (d[i] > 200 && d[i + 2] > 200 && w > 0) { const p = i / 4; a += w; sx += w * (p % c.width); sy += w * Math.floor(p / c.width) } } return { area: a, cx: sx / a + 0.5, cy: sy / a + 0.5 } }, png) console.log(label.padEnd(6), JSON.stringify({ area_gap_rel: +((m.area - want.area) / want.area).toPrecision(2), centroid_gap_px: +Math.hypot(m.cx - want.cx, m.cy - want.cy).toPrecision(2) })) } await browser.close() }