// box-spot.mjs: where the round button and the box's menu go. The // button covers no resize handle and stays inside the stage, near its // box, whatever the box; the menu stays inside the window. import test from 'node:test' import assert from 'node:assert/strict' import { buttonSpot, menuSpot, clip } from './box-spot.mjs' const rect = (left, top, width, height) => ({ left, top, width, height, right: left + width, bottom: top + height }) const overlaps = (a, b) => a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom // The eight handles of a box, as canvas.mjs draws them: 24px squares // centred on its corners and the middles of its edges. const handlesOf = b => [ [b.left, b.top], [(b.left + b.right) / 2, b.top], [b.right, b.top], [b.right, (b.top + b.bottom) / 2], [b.right, b.bottom], [(b.left + b.right) / 2, b.bottom], [b.left, b.bottom], [b.left, (b.top + b.bottom) / 2] ].map(([x, y]) => rect(x - 12, y - 12, 24, 24)) test('buttonSpot: at the top end corner, inside the stage, clear of every handle', () => { const stage = rect(0, 0, 1000, 600) const size = 28 const check = (box, where) => { const handles = handlesOf(box) const at = buttonSpot({ box, handles, bound: stage, size }) const r = rect(at.left, at.top, size, size) assert.ok(r.left >= stage.left && r.top >= stage.top && r.right <= stage.right && r.bottom <= stage.bottom, `${where}: ${JSON.stringify(r)} is outside the stage`) for (const h of handles) assert.ok(!overlaps(r, h), `${where}: ${JSON.stringify(r)} covers a handle ${JSON.stringify(h)}`) return r } // Room above: above the top end corner, its end edge on the box's. const r = check(rect(200, 200, 300, 200), 'a box in the middle') assert.equal(r.right, 500) assert.ok(r.bottom <= 200 - 12) // At the slide's edges and corners, and filling it. check(rect(600, 0, 400, 200), 'a box at the top end corner') check(rect(0, 0, 300, 200), 'a box at the top start corner') check(rect(0, 400, 1000, 200), 'a box along the foot') check(rect(0, 0, 1000, 600), 'a box filling the slide') check(rect(960, 300, 40, 40), 'a small box at the end edge') // Right to left: the end is the left. const rtl = buttonSpot({ box: rect(200, 200, 300, 200), handles: [], bound: stage, size, rtl: true }) assert.equal(rtl.left, 200) }) test('menuSpot: under its anchor, flipped and kept inside the window', () => { const win = { vw: 1000, vh: 800 } let s = menuSpot({ anchor: rect(100, 100, 28, 28), width: 200, height: 300, ...win }) assert.deepEqual([s.left, s.top], [100, 132]) // Near the end edge: its end on the anchor's. s = menuSpot({ anchor: rect(950, 100, 28, 28), width: 200, height: 300, ...win }) assert.equal(s.left, 978 - 200) // Near the foot: above. s = menuSpot({ anchor: rect(100, 700, 28, 28), width: 200, height: 300, ...win }) assert.equal(s.top, 700 - 4 - 300) // A window too small either way: inside, and it scrolls. s = menuSpot({ anchor: { left: 300, right: 300, top: 250, bottom: 250 }, width: 200, height: 600, vw: 320, vh: 500 }) assert.ok(s.left >= 8 && s.left + 200 <= 312, `left ${s.left}`) assert.ok(s.top >= 8 && s.top + s.maxHeight <= 492, `top ${s.top}, ${s.maxHeight} tall`) }) // The rectangle returned, whatever the box: inside the stage, over no // handle, and within 48px of the box (fix round 1, R1). test('buttonSpot: any box, a tiny one in a corner included, gets a spot that covers no handle', () => { const size = 28 const gapTo = (r, b) => Math.hypot(Math.max(0, b.left - r.right, r.left - b.right), Math.max(0, b.top - r.bottom, r.top - b.bottom)) const check = (stage, box, where) => { const handles = handlesOf(box) const at = buttonSpot({ box, handles, bound: stage, size }) const r = rect(at.left, at.top, size, size) assert.ok(r.left >= stage.left && r.top >= stage.top && r.right <= stage.right && r.bottom <= stage.bottom, `${where}: ${JSON.stringify(r)} is outside the stage`) for (const h of handles) assert.ok(!overlaps(r, h), `${where}: ${JSON.stringify(r)} covers a handle ${JSON.stringify(h)}`) assert.ok(gapTo(r, box) <= 48, `${where}: ${JSON.stringify(r)} is ${gapTo(r, box)}px from the box`) } // The reviewer's case. check(rect(0, 0, 320, 180), rect(0, 160, 10, 10), 'a 10x10 box at (0,160) of a 320x180 stage') for (const [W, H] of [[320, 180], [1280, 720]]) { const stage = rect(0, 0, W, H) for (const [w, h] of [[10, 10], [40, 40], [200, 120]]) { for (const [fx, fy] of [[0, 0], [0.5, 0], [1, 0], [1, 0.5], [1, 1], [0.5, 1], [0, 1], [0, 0.5]]) { check(stage, rect((W - w) * fx, (H - h) * fy, w, h), `${w}x${h} at ${fx},${fy} of ${W}x${H}`) } } } }) // Fix round 2: nothing returned unchecked. Every answer is inside the // stage and over no handle; null only when nothing anywhere is. const fitsIn = (stage, handles, size) => p => { const r = rect(p.left, p.top, size, size) return r.left >= stage.left && r.top >= stage.top && r.right <= stage.right && r.bottom <= stage.bottom && !handles.some(h => overlaps(r, h)) } // An independent search, every half pixel of the stage. function bruteForce (stage, handles, size) { const ok = fitsIn(stage, handles, size) for (let top = stage.top; top + size <= stage.bottom; top += 0.5) { for (let left = stage.left; left + size <= stage.right; left += 0.5) if (ok({ left, top })) return { left, top } } return null } function checked (stage, box, where, size = 28) { const handles = handlesOf(box) const at = buttonSpot({ box, handles, bound: stage, size }) if (at) assert.ok(fitsIn(stage, handles, size)(at), `${where}: ${JSON.stringify(at)} covers a handle or leaves the stage`) else assert.equal(bruteForce(stage, handles, size), null, `${where}: null, but there is room`) return at } test('buttonSpot: the reviewer\'s box filling a small stage, and boxes filling stages', () => { // A 100x52 box at (0.5, 0.5) filling its stage: (12.5, 12.5) is free. assert.notEqual(checked(rect(0.5, 0.5, 100, 52), rect(0.5, 0.5, 100, 52), 'the reviewer\'s 100x52 box'), null) for (const [w, h] of [[160, 90], [320, 180], [1280, 720], [100, 52], [60, 34], [40, 40]]) { checked(rect(0, 0, w, h), rect(0, 0, w, h), `a box filling a ${w}x${h} stage`) } // Fractional positions and sizes. checked(rect(0.25, 0.75, 319.5, 179.25), rect(10.3, 20.7, 33.3, 12.9), 'fractions') checked(rect(0.25, 0.75, 319.5, 179.25), rect(0.25, 0.75, 319.5, 179.25), 'a fractional box filling its stage') }) test('buttonSpot: thousands of boxes, every answer fits, null only when nothing does', () => { // A seeded generator (mulberry32), so a failure can be run again. let seed = 20261117 const random = () => { seed = (seed + 0x6D2B79F5) | 0 let t = Math.imul(seed ^ (seed >>> 15), 1 | seed) t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t return ((t ^ (t >>> 14)) >>> 0) / 4294967296 } let nulls = 0 for (let i = 0; i < 3000; i++) { const W = 160 + random() * (1920 - 160) const H = W * 9 / 16 const stage = rect(random() * 20, random() * 20, W, H) // Sometimes the whole stage, sometimes tiny, mostly anything. const kind = random() const w = kind < 0.1 ? W : kind < 0.3 ? 2 + random() * 30 : 2 + random() * (W - 2) const h = kind < 0.1 ? H : kind < 0.3 ? 2 + random() * 30 : 2 + random() * (H - 2) const box = rect(stage.left + random() * (W - w), stage.top + random() * (H - h), w, h) if (checked(stage, box, `box ${i}: ${JSON.stringify(box)} in ${JSON.stringify(stage)}`) === null) nulls++ } assert.ok(nulls < 3000) }) test('clip: what can be seen of a rect through a frame; the rect itself where the frame has no box', () => { const r = (left, top, right, bottom) => ({ left, top, right, bottom, width: right - left, height: bottom - top }) assert.deepEqual(clip(r(100, -200, 490, 1000), r(100, 100, 490, 500)), { left: 100, top: 100, right: 490, bottom: 500 }) const stage = r(10, 10, 20, 20) assert.equal(clip(stage, r(0, 0, 0, 0)), stage) assert.equal(clip(stage, null), stage) }) // Phone view put the button over the picture beside the selected shape // (review): other boxes are places to avoid where a spot clear of them // exists. The handles and the stage stay hard limits. test('buttonSpot: clear of the other boxes where it can be, over one only where nothing else fits', () => { const size = 28 const stage = rect(0, 0, 360, 90) // A shape against the stage's top end corner, a picture beside it: // no room above, past the end or under it. const box = rect(250, 10, 100, 60) const picture = rect(20, 10, 220, 70) const handles = handlesOf(box) const at = buttonSpot({ box, handles, avoid: [picture], bound: stage, size }) const r = rect(at.left, at.top, size, size) assert.ok(!overlaps(r, picture), `${JSON.stringify(r)} is over the picture`) for (const h of handles) assert.ok(!overlaps(r, h), `${JSON.stringify(r)} covers a handle`) assert.ok(r.left >= 0 && r.top >= 0 && r.right <= 360 && r.bottom <= 90, 'inside the stage') // Where the other boxes leave no room, a box is covered rather than // the button docked: it is still on the stage, clear of the handles. const tight = rect(0, 0, 60, 60) const fill = rect(0, 0, 60, 60) const spot = buttonSpot({ box: rect(20, 20, 20, 20), handles: [], avoid: [fill], bound: tight, size }) assert.ok(spot, 'a spot over the other box, rather than none') })