fix(anatomy): V1-2.2 refuse zones without a usable interior

A polygon or path zone or trigger must fill a disc of 4 CSS px on the
320 x 568 preview under the layer's nonzero rule, stroke left out:
conservative widest-disc search on the flattened outline, caps and a
per-sheet budget, closed codes interior_too_small and
marker_interior_too_small. Browser probe on six projects.
This commit is contained in:
bchanot committed 2026-11-01 12:19:08 +01:00
1 parent de2dfb392e
commit 8dd2d78d38
18 files changed
+1852 -38

No files matched your search

+219
View File
@@ -0,0 +1,219 @@
{
"generatedBy": "crates/core/tests/interior_probe.rs",
"frame": {
"width": 286,
"height": 310
},
"cases": [
{
"name": "square-6",
"accepted": true,
"codes": [],
"widest": {
"x": 43.0,
"y": 43.0,
"r": 3.0
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,40 46,40 46,46 40,46\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "flat-triangle",
"accepted": true,
"codes": [],
"widest": {
"x": 70.0,
"y": 42.287,
"r": 2.287
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,40 100,40 70,44.6\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "arc-disc-6",
"accepted": true,
"codes": [],
"widest": {
"x": 43.0,
"y": 50.0,
"r": 2.998
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 50a3 3 0 1 0 6 0a3 3 0 1 0 -6 0Z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "cubic-lens",
"accepted": true,
"codes": [],
"widest": {
"x": 59.999,
"y": 50.0,
"r": 4.498
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 50C50 44 70 44 80 50C70 56 50 56 40 50Z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "ell-3.6",
"accepted": true,
"codes": [],
"widest": {
"x": 42.109,
"y": 42.109,
"r": 2.109
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,40 80,40 80,43.6 43.6,43.6 43.6,80 40,80\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "pentagram-evenodd-root",
"accepted": true,
"codes": [],
"widest": {
"x": 59.999,
"y": 60.0,
"r": 3.09
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g fill-rule=\"evenodd\"><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"60,50 65.87785252292473,68.09016994374947 50.489434837048464,56.90983005625053 69.51056516295154,56.90983005625053 54.12214747707527,68.09016994374947\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "forth-and-back",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": null,
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 40L90 90L40 40\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "square-twice-opposite",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": null,
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 40h40v40h-40zM40 40v40h40v-40z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "diagonal-sliver",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": {
"x": 89.507,
"y": 88.809,
"r": 0.493
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,40 90,90 90,88.6\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "cubic-there-and-back",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": null,
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 80C50 40 80 40 90 80C80 40 50 40 40 80Z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "arc-there-and-back",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": null,
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 80A25 25 0 0 1 90 80A25 25 0 0 0 40 80Z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "flat-ellipse-45",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": {
"x": 65.0,
"y": 65.0,
"r": 1.0
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><path d=\"M40 40A35 1 45 0 1 90 90A35 1 45 0 1 40 40Z\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></path></g></svg>"
},
{
"name": "ell-3.3",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": {
"x": 41.933,
"y": 41.933,
"r": 1.933
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,40 80,40 80,43.3 43.3,43.3 43.3,80 40,80\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
},
{
"name": "triangle-6.6",
"accepted": false,
"codes": [
"annotation.geometry.interior_too_small"
],
"widest": {
"x": 43.3,
"y": 43.811,
"r": 1.905
},
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 286 310\"><g><path d=\"M0 0h1\"/></g><g data-layer=\"annotations\" data-view-id=\"main\"><polygon points=\"40,45.716 46.6,45.716 43.3,40\" fill=\"#888888\" stroke=\"#888888\" fill-opacity=\"0.3\" stroke-width=\"2\" pointer-events=\"all\" style=\"cursor:pointer\" data-anno-id=\"shape\" data-anno-kind=\"zone\" data-anno-lateral=\"unpaired\" data-anno-name-fr=\"Jarret\" data-anno-name-en=\"Jarret\"><title>Jarret / Jarret</title></polygon></g></svg>"
}
],
"equine": [
{
"id": "caudal",
"view": "main",
"widest": {
"x": 1692.151,
"y": 554.007,
"r": 17.95
}
},
{
"id": "sacral",
"view": "main",
"widest": {
"x": 1470.781,
"y": 505.112,
"r": 40.963
}
},
{
"id": "lumbar",
"view": "main",
"widest": {
"x": 1341.641,
"y": 510.702,
"r": 51.297
}
},
{
"id": "thoracic",
"view": "main",
"widest": {
"x": 872.953,
"y": 484.677,
"r": 41.579
}
},
{
"id": "cervical",
"view": "main",
"widest": {
"x": 521.605,
"y": 279.763,
"r": 44.126
}
},
{
"id": "head-trigger",
"view": "main",
"widest": {
"x": 341.041,
"y": 303.749,
"r": 105.59
}
}
]
}
+12
View File
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Interior harness</title>
</head>
<body style="margin: 0">
<div id="root"></div>
<script type="module" src="./interior.ts"></script>
</body>
</html>
+33
View File
@@ -0,0 +1,33 @@
/**
* Test page for the interior rule in a real browser (e2e/ui/interior-clicks.spec.ts): the
* canvas the core composes for one probe case (e2e/fixtures/interior-probe.json, generated by
* crates/core/tests/interior_probe.rs) in the phone preview's 286 × 310 box, inserted as the
* editor inserts it (DOMParser, importNode), every click on a marker recorded. Served by the
* Vite dev server only (`/e2e/harness/interior.html?case=`); the build never ships it.
*/
import probe from '../fixtures/interior-probe.json'
const name = new URLSearchParams(location.search).get('case')
const found = probe.cases.find((c) => c.name === name)
if (!found) throw new Error(`no probe case ${String(name)}`)
const root = document.getElementById('root') as HTMLElement
const clicks = document.createElement('output')
clicks.dataset.testid = 'clicks'
clicks.style.cssText = 'display:block;height:24px;overflow:hidden'
const box = document.createElement('div')
box.dataset.testid = 'preview'
box.style.cssText = `width:${probe.frame.width}px;height:${probe.frame.height}px`
const parsed = new DOMParser().parseFromString(found.svg, 'image/svg+xml')
const svg = document.importNode(
parsed.documentElement,
true,
) as unknown as SVGSVGElement
svg.style.cssText = 'display:block;width:100%;height:100%'
box.append(svg)
root.append(clicks, box)
box.addEventListener('click', (event) => {
const marker = (event.target as Element).closest('[data-anno-id]')
const id = marker?.getAttribute('data-anno-id')
if (id) clicks.textContent = `${clicks.textContent ?? ''} ${id}`.trim()
})
+3 -2
View File
@@ -41,13 +41,14 @@ export const PROJECTS: Project[] = [
]
/**
* The canvas click proof (V1-2.2, e2e/ui/canvas-clicks.spec.ts) also runs on Firefox and
* The canvas click proofs (V1-2.2, e2e/ui/canvas-clicks.spec.ts and interior-clicks.spec.ts)
* also run on Firefox and
* WebKit, desktop, and on the WebKit phone, when the pinned engine is installed, and on a
* 320 px Chromium phone
* (`playwright install firefox webkit`, no sudo, never `--with-deps`). A missing engine is
* named in the run's output, never passed silently.
*/
const CANVAS_CLICKS = /canvas-clicks\.spec\.ts$/
const CANVAS_CLICKS = /(canvas|interior)-clicks\.spec\.ts$/
function installed(name: string, executable: string): boolean {
const present = existsSync(executable)
+295
View File
@@ -0,0 +1,295 @@
import { expect, test } from '../support/browser'
import type { Page } from '@playwright/test'
import probe from '../fixtures/interior-probe.json'
// Owner brief « V1-2.2 : fermeture du dernier MEDIUM » (2026-11-01): a zone or a trigger drawn
// as a polygon or a path must fill a disc of 4 CSS px on the phone preview, its stroke left
// out. The core's verdict (e2e/fixtures/interior-probe.json, crates/core/tests/interior_probe.rs)
// against the browser's own hit test, in the preview's 286 × 310 box (one unit per CSS px):
// an accepted shape takes real presses in the disc the core found and its fill alone holds a
// 4 px disc; a refused one fills none. Then the equine draft's zones, in the editor canvas.
// Run on Chromium, Firefox and WebKit when installed.
type Point = { x: number; y: number }
type Disc = { x: number; y: number; r: number }
/** The threshold: a disc of 4 CSS px across. */
const MIN_RADIUS_PX = 2
/** The browser's mask is sampled every quarter pixel: its radius is within that of the fill. */
const MASK_STEP_PX = 0.25
async function press(page: Page, at: Point, touch: boolean) {
if (touch) await page.touchscreen.tap(at.x, at.y)
else await page.mouse.click(at.x, at.y)
}
async function recorded(page: Page): Promise<string[]> {
const text = (await page.getByTestId('clicks').textContent()) ?? ''
return text.split(' ').filter(Boolean)
}
async function clickRecords(page: Page, at: Point, touch: boolean) {
const before = (await recorded(page)).length
await press(page, at, touch)
await page.waitForTimeout(50)
return (await recorded(page)).slice(before)
}
test.describe('interior clicks', () => {
for (const c of probe.cases) {
test(`${c.accepted ? 'accepted' : 'refused'}: ${c.name}`, async ({
page,
}, info) => {
await page.goto(`/e2e/harness/interior.html?case=${c.name}`)
await expect(page.locator('[data-anno-id="shape"]')).toBeAttached()
const browser = await page.evaluate(widestFill, MASK_STEP_PX)
if (c.widest) {
// The core never claims more than the browser fills (it may claim less: its disc
// crosses no edge, where a nonzero fill may run on past one).
expect(browser.r, JSON.stringify(browser)).toBeGreaterThan(
c.widest.r - 0.5,
)
}
if (!c.accepted) {
expect(browser.r, JSON.stringify(browser)).toBeLessThan(MIN_RADIUS_PX)
return
}
if (!c.widest) throw new Error(`${c.name} accepted without a disc`)
expect(browser.r).toBeGreaterThanOrEqual(MIN_RADIUS_PX)
const disc = c.widest
for (const fillOnly of [true, false]) {
const ring = await page.evaluate(ringHits, { disc, fillOnly })
expect(ring, `fillOnly ${String(fillOnly)}`).toEqual({
missed: [],
covered: 0,
})
}
const centre = await page.evaluate(toScreen, { x: disc.x, y: disc.y })
const touch = info.project.use.hasTouch === true
expect(await clickRecords(page, centre, touch)).toEqual(['shape'])
})
}
test('every equine zone holds the disc the core found and takes a real press', async ({
page,
}, info) => {
await page.goto('/e2e/harness/canvas.html')
await expect(
page.locator('[data-testid="anatomy-canvas"] [data-anno-id="caudal"]'),
).toBeAttached()
const touch = info.project.use.hasTouch === true
for (const zone of probe.equine) {
if (!zone.widest) throw new Error(`${zone.id} without a disc`)
const disc = zone.widest
// A point painted over the zone takes its taps (« the visible wins »): the samples under
// a visible disc are left out, at most a quarter of them.
const { missed, covered } = await page.evaluate(ringHits, {
disc,
fillOnly: false,
id: zone.id,
clearOfDiscs: true,
})
expect(missed, zone.id).toEqual([])
expect(covered, zone.id).toBeLessThanOrEqual(8)
// Chromium's touch adjustment gives a point the taps up to about 10 px around its disc
// (measured 2026-11-01): the real press goes where the zone's fill lies farthest from
// every disc, inside its edges.
const at = await page.evaluate(clearestFill, { id: zone.id, disc })
expect(await clickRecords(page, at, touch), zone.id).toEqual([zone.id])
}
})
})
/**
* In the page: the point of the shape's fill, on a half CSS px grid, its own on the hit test and
* inside its edges by 2 px or 60 % of the core's disc radius if less (the caudal zone's disc is
* 4.95 px across at 320), farthest from every visible disc.
*/
function clearestFill(args: { id: string; disc: Disc }): Point {
const { id, disc } = args
const svg = document.querySelector(
'[data-testid="anatomy-canvas"] svg',
) as SVGSVGElement
const ctm = svg.getScreenCTM()
if (!ctm) throw new Error('no CTM')
const shape = svg.querySelector(
`[data-anno-id="${id}"]`,
) as SVGGeometryElement
const twins = (
Array.from(
svg.querySelectorAll('circle[pointer-events="visiblePainted"]'),
) as SVGCircleElement[]
).map((t) => ({
centre: new DOMPoint(
t.cx.baseVal.value,
t.cy.baseVal.value,
).matrixTransform(ctm),
r: t.r.baseVal.value * ctm.a,
}))
const inverse = ctm.inverse()
const filled = (x: number, y: number) =>
shape.isPointInFill(new DOMPoint(x, y).matrixTransform(inverse))
const margin = Math.min(2, 0.6 * disc.r * ctm.a)
const box = shape.getBoundingClientRect()
let best = { x: 0, y: 0, d: -1 }
for (let x = Math.ceil(box.left); x <= box.right; x += 0.5) {
for (let y = Math.ceil(box.top); y <= box.bottom; y += 0.5) {
const steps = [-margin, 0, margin]
const inside = steps.every((dx) =>
steps.every((dy) => filled(x + dx, y + dy)),
)
if (!inside) continue
const hit = document.elementFromPoint(x, y)?.closest('[data-anno-id]')
if (hit?.getAttribute('data-anno-id') !== id) continue
const d = Math.min(
...twins.map((t) => Math.hypot(x - t.centre.x, y - t.centre.y) - t.r),
)
if (d > best.d) best = { x, y, d }
}
}
if (best.d < 0) throw new Error(`${id}: no fill clear of the discs`)
return { x: best.x, y: best.y }
}
/** In the page: a point of the layer's user space on screen. */
function toScreen(at: Point): Point {
const svg = document.querySelector('svg') as SVGSVGElement
const ctm = svg.getScreenCTM()
if (!ctm) throw new Error('no CTM')
const p = new DOMPoint(at.x, at.y).matrixTransform(ctm)
return { x: p.x, y: p.y }
}
/**
* In the page: the browser's hit test at the disc's centre and 32 points at 90 % of its radius.
* With `fillOnly` the shape takes `pointer-events="fill"` meanwhile, its stroke out of the test;
* with `clearOfDiscs` the points within a visible disc (its stroke and half a pixel included)
* are counted apart. Answers the points that hit another element, and the points left out.
*/
function ringHits(args: {
disc: Disc
fillOnly: boolean
id?: string
clearOfDiscs?: boolean
}): { missed: string[]; covered: number } {
const { disc, fillOnly } = args
const id = args.id ?? 'shape'
const svg = document.querySelector('svg') as SVGSVGElement
const ctm = svg.getScreenCTM()
if (!ctm) throw new Error('no CTM')
const shapes = Array.from(svg.querySelectorAll(`[data-anno-id="${id}"]`))
const kept = shapes.map((s) => s.getAttribute('pointer-events'))
if (fillOnly) shapes.forEach((s) => s.setAttribute('pointer-events', 'fill'))
const reach = disc.r * 0.9
const points: Point[] = [{ x: disc.x, y: disc.y }]
for (let k = 0; k < 32; k++) {
const a = (k * 2 * Math.PI) / 32
points.push({
x: disc.x + reach * Math.cos(a),
y: disc.y + reach * Math.sin(a),
})
}
const twins = Array.from(
svg.querySelectorAll('circle[pointer-events="visiblePainted"]'),
) as SVGCircleElement[]
const underDisc = (at: Point) =>
twins.some(
(t) =>
Math.hypot(at.x - t.cx.baseVal.value, at.y - t.cy.baseVal.value) <=
t.r.baseVal.value + 0.75 + 0.5 / ctm.a,
)
const missed: string[] = []
let covered = 0
for (const at of points) {
if (args.clearOfDiscs && underDisc(at)) {
covered++
continue
}
const p = new DOMPoint(at.x, at.y).matrixTransform(ctm)
const hit = document
.elementFromPoint(p.x, p.y)
?.closest('[data-anno-id]')
?.getAttribute('data-anno-id')
if (hit !== id)
missed.push(`${at.x.toFixed(2)},${at.y.toFixed(2)}: ${String(hit)}`)
}
shapes.forEach((s, i) => {
const value = kept[i]
if (value === null || value === undefined)
s.removeAttribute('pointer-events')
else s.setAttribute('pointer-events', value)
})
return { missed, covered }
}
/**
* In the page: the widest disc of the shape's fill as the browser hits it (`pointer-events`
* `fill`, the stroke out), from a mask sampled every `step` CSS px over its box and 3 px around
* it, by an exact Euclidean distance transform (Felzenszwalb and Huttenlocher). The radius is
* the distance to the nearest sample outside the fill, less half a step.
*/
function widestFill(step: number): Disc {
const svg = document.querySelector('svg') as SVGSVGElement
const shape = svg.querySelector(
'[data-anno-id="shape"]',
) as SVGGraphicsElement
const kept = shape.getAttribute('pointer-events')
shape.setAttribute('pointer-events', 'fill')
const box = shape.getBoundingClientRect()
const x0 = box.left - 3
const y0 = box.top - 3
const w = Math.ceil((box.width + 6) / step) + 1
const h = Math.ceil((box.height + 6) / step) + 1
const far = 1e20
const grid = new Float64Array(w * h)
for (let j = 0; j < h; j++) {
for (let i = 0; i < w; i++) {
const hit = document.elementFromPoint(x0 + i * step, y0 + j * step)
grid[j * w + i] = hit === shape ? far : 0
}
}
if (kept === null) shape.removeAttribute('pointer-events')
else shape.setAttribute('pointer-events', kept)
const line = (f: Float64Array): Float64Array => {
const n = f.length
const d = new Float64Array(n)
const v = new Int32Array(n)
const z = new Float64Array(n + 1)
let k = 0
v[0] = 0
z[0] = -Infinity
z[1] = Infinity
for (let q = 1; q < n; q++) {
let s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k])
while (s <= z[k]) {
k--
s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k])
}
k++
v[k] = q
z[k] = s
z[k + 1] = Infinity
}
k = 0
for (let q = 0; q < n; q++) {
while (z[k + 1] < q) k++
d[q] = (q - v[k]) ** 2 + f[v[k]]
}
return d
}
for (let i = 0; i < w; i++) {
const column = new Float64Array(h)
for (let j = 0; j < h; j++) column[j] = grid[j * w + i]
const done = line(column)
for (let j = 0; j < h; j++) grid[j * w + i] = done[j]
}
let best = { x: 0, y: 0, r: 0 }
for (let j = 0; j < h; j++) {
const done = line(grid.subarray(j * w, (j + 1) * w).slice())
for (let i = 0; i < w; i++) {
const r = Math.sqrt(done[i]) * step - step / 2
if (r > best.r) best = { x: x0 + i * step, y: y0 + j * step, r }
}
}
return best
}