| 1 | // box-spot.mjs: where the round button and the box's menu go. The |
| 2 | // button covers no resize handle and stays inside the stage, near its |
| 3 | // box, whatever the box; the menu stays inside the window. |
| 4 | import test from 'node:test' |
| 5 | import assert from 'node:assert/strict' |
| 6 | import { buttonSpot, menuSpot, clip } from './box-spot.mjs' |
| 7 | |
| 8 | const rect = (left, top, width, height) => ({ left, top, width, height, right: left + width, bottom: top + height }) |
| 9 | const overlaps = (a, b) => a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom |
| 10 | // The eight handles of a box, as canvas.mjs draws them: 24px squares |
| 11 | // centred on its corners and the middles of its edges. |
| 12 | const handlesOf = b => [ |
| 13 | [b.left, b.top], [(b.left + b.right) / 2, b.top], [b.right, b.top], [b.right, (b.top + b.bottom) / 2], |
| 14 | [b.right, b.bottom], [(b.left + b.right) / 2, b.bottom], [b.left, b.bottom], [b.left, (b.top + b.bottom) / 2] |
| 15 | ].map(([x, y]) => rect(x - 12, y - 12, 24, 24)) |
| 16 | |
| 17 | test('buttonSpot: at the top end corner, inside the stage, clear of every handle', () => { |
| 18 | const stage = rect(0, 0, 1000, 600) |
| 19 | const size = 28 |
| 20 | const check = (box, where) => { |
| 21 | const handles = handlesOf(box) |
| 22 | const at = buttonSpot({ box, handles, bound: stage, size }) |
| 23 | const r = rect(at.left, at.top, size, size) |
| 24 | 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`) |
| 25 | for (const h of handles) assert.ok(!overlaps(r, h), `${where}: ${JSON.stringify(r)} covers a handle ${JSON.stringify(h)}`) |
| 26 | return r |
| 27 | } |
| 28 | // Room above: above the top end corner, its end edge on the box's. |
| 29 | const r = check(rect(200, 200, 300, 200), 'a box in the middle') |
| 30 | assert.equal(r.right, 500) |
| 31 | assert.ok(r.bottom <= 200 - 12) |
| 32 | // At the slide's edges and corners, and filling it. |
| 33 | check(rect(600, 0, 400, 200), 'a box at the top end corner') |
| 34 | check(rect(0, 0, 300, 200), 'a box at the top start corner') |
| 35 | check(rect(0, 400, 1000, 200), 'a box along the foot') |
| 36 | check(rect(0, 0, 1000, 600), 'a box filling the slide') |
| 37 | check(rect(960, 300, 40, 40), 'a small box at the end edge') |
| 38 | // Right to left: the end is the left. |
| 39 | const rtl = buttonSpot({ box: rect(200, 200, 300, 200), handles: [], bound: stage, size, rtl: true }) |
| 40 | assert.equal(rtl.left, 200) |
| 41 | }) |
| 42 | |
| 43 | test('menuSpot: under its anchor, flipped and kept inside the window', () => { |
| 44 | const win = { vw: 1000, vh: 800 } |
| 45 | let s = menuSpot({ anchor: rect(100, 100, 28, 28), width: 200, height: 300, ...win }) |
| 46 | assert.deepEqual([s.left, s.top], [100, 132]) |
| 47 | // Near the end edge: its end on the anchor's. |
| 48 | s = menuSpot({ anchor: rect(950, 100, 28, 28), width: 200, height: 300, ...win }) |
| 49 | assert.equal(s.left, 978 - 200) |
| 50 | // Near the foot: above. |
| 51 | s = menuSpot({ anchor: rect(100, 700, 28, 28), width: 200, height: 300, ...win }) |
| 52 | assert.equal(s.top, 700 - 4 - 300) |
| 53 | // A window too small either way: inside, and it scrolls. |
| 54 | s = menuSpot({ anchor: { left: 300, right: 300, top: 250, bottom: 250 }, width: 200, height: 600, vw: 320, vh: 500 }) |
| 55 | assert.ok(s.left >= 8 && s.left + 200 <= 312, `left ${s.left}`) |
| 56 | assert.ok(s.top >= 8 && s.top + s.maxHeight <= 492, `top ${s.top}, ${s.maxHeight} tall`) |
| 57 | }) |
| 58 | |
| 59 | // The rectangle returned, whatever the box: inside the stage, over no |
| 60 | // handle, and within 48px of the box (fix round 1, R1). |
| 61 | test('buttonSpot: any box, a tiny one in a corner included, gets a spot that covers no handle', () => { |
| 62 | const size = 28 |
| 63 | 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)) |
| 64 | const check = (stage, box, where) => { |
| 65 | const handles = handlesOf(box) |
| 66 | const at = buttonSpot({ box, handles, bound: stage, size }) |
| 67 | const r = rect(at.left, at.top, size, size) |
| 68 | 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`) |
| 69 | for (const h of handles) assert.ok(!overlaps(r, h), `${where}: ${JSON.stringify(r)} covers a handle ${JSON.stringify(h)}`) |
| 70 | assert.ok(gapTo(r, box) <= 48, `${where}: ${JSON.stringify(r)} is ${gapTo(r, box)}px from the box`) |
| 71 | } |
| 72 | // The reviewer's case. |
| 73 | check(rect(0, 0, 320, 180), rect(0, 160, 10, 10), 'a 10x10 box at (0,160) of a 320x180 stage') |
| 74 | for (const [W, H] of [[320, 180], [1280, 720]]) { |
| 75 | const stage = rect(0, 0, W, H) |
| 76 | for (const [w, h] of [[10, 10], [40, 40], [200, 120]]) { |
| 77 | 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]]) { |
| 78 | check(stage, rect((W - w) * fx, (H - h) * fy, w, h), `${w}x${h} at ${fx},${fy} of ${W}x${H}`) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | // Fix round 2: nothing returned unchecked. Every answer is inside the |
| 85 | // stage and over no handle; null only when nothing anywhere is. |
| 86 | const fitsIn = (stage, handles, size) => p => { |
| 87 | const r = rect(p.left, p.top, size, size) |
| 88 | return r.left >= stage.left && r.top >= stage.top && r.right <= stage.right && r.bottom <= stage.bottom && !handles.some(h => overlaps(r, h)) |
| 89 | } |
| 90 | // An independent search, every half pixel of the stage. |
| 91 | function bruteForce (stage, handles, size) { |
| 92 | const ok = fitsIn(stage, handles, size) |
| 93 | for (let top = stage.top; top + size <= stage.bottom; top += 0.5) { |
| 94 | for (let left = stage.left; left + size <= stage.right; left += 0.5) if (ok({ left, top })) return { left, top } |
| 95 | } |
| 96 | return null |
| 97 | } |
| 98 | function checked (stage, box, where, size = 28) { |
| 99 | const handles = handlesOf(box) |
| 100 | const at = buttonSpot({ box, handles, bound: stage, size }) |
| 101 | if (at) assert.ok(fitsIn(stage, handles, size)(at), `${where}: ${JSON.stringify(at)} covers a handle or leaves the stage`) |
| 102 | else assert.equal(bruteForce(stage, handles, size), null, `${where}: null, but there is room`) |
| 103 | return at |
| 104 | } |
| 105 | |
| 106 | test('buttonSpot: the reviewer\'s box filling a small stage, and boxes filling stages', () => { |
| 107 | // A 100x52 box at (0.5, 0.5) filling its stage: (12.5, 12.5) is free. |
| 108 | assert.notEqual(checked(rect(0.5, 0.5, 100, 52), rect(0.5, 0.5, 100, 52), 'the reviewer\'s 100x52 box'), null) |
| 109 | for (const [w, h] of [[160, 90], [320, 180], [1280, 720], [100, 52], [60, 34], [40, 40]]) { |
| 110 | checked(rect(0, 0, w, h), rect(0, 0, w, h), `a box filling a ${w}x${h} stage`) |
| 111 | } |
| 112 | // Fractional positions and sizes. |
| 113 | checked(rect(0.25, 0.75, 319.5, 179.25), rect(10.3, 20.7, 33.3, 12.9), 'fractions') |
| 114 | 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') |
| 115 | }) |
| 116 | |
| 117 | test('buttonSpot: thousands of boxes, every answer fits, null only when nothing does', () => { |
| 118 | // A seeded generator (mulberry32), so a failure can be run again. |
| 119 | let seed = 20261117 |
| 120 | const random = () => { |
| 121 | seed = (seed + 0x6D2B79F5) | 0 |
| 122 | let t = Math.imul(seed ^ (seed >>> 15), 1 | seed) |
| 123 | t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t |
| 124 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296 |
| 125 | } |
| 126 | let nulls = 0 |
| 127 | for (let i = 0; i < 3000; i++) { |
| 128 | const W = 160 + random() * (1920 - 160) |
| 129 | const H = W * 9 / 16 |
| 130 | const stage = rect(random() * 20, random() * 20, W, H) |
| 131 | // Sometimes the whole stage, sometimes tiny, mostly anything. |
| 132 | const kind = random() |
| 133 | const w = kind < 0.1 ? W : kind < 0.3 ? 2 + random() * 30 : 2 + random() * (W - 2) |
| 134 | const h = kind < 0.1 ? H : kind < 0.3 ? 2 + random() * 30 : 2 + random() * (H - 2) |
| 135 | const box = rect(stage.left + random() * (W - w), stage.top + random() * (H - h), w, h) |
| 136 | if (checked(stage, box, `box ${i}: ${JSON.stringify(box)} in ${JSON.stringify(stage)}`) === null) nulls++ |
| 137 | } |
| 138 | assert.ok(nulls < 3000) |
| 139 | }) |
| 140 | |
| 141 | test('clip: what can be seen of a rect through a frame; the rect itself where the frame has no box', () => { |
| 142 | const r = (left, top, right, bottom) => ({ left, top, right, bottom, width: right - left, height: bottom - top }) |
| 143 | assert.deepEqual(clip(r(100, -200, 490, 1000), r(100, 100, 490, 500)), { left: 100, top: 100, right: 490, bottom: 500 }) |
| 144 | const stage = r(10, 10, 20, 20) |
| 145 | assert.equal(clip(stage, r(0, 0, 0, 0)), stage) |
| 146 | assert.equal(clip(stage, null), stage) |
| 147 | }) |
| 148 | |
| 149 | // Phone view put the button over the picture beside the selected shape |
| 150 | // (review): other boxes are places to avoid where a spot clear of them |
| 151 | // exists. The handles and the stage stay hard limits. |
| 152 | test('buttonSpot: clear of the other boxes where it can be, over one only where nothing else fits', () => { |
| 153 | const size = 28 |
| 154 | const stage = rect(0, 0, 360, 90) |
| 155 | // A shape against the stage's top end corner, a picture beside it: |
| 156 | // no room above, past the end or under it. |
| 157 | const box = rect(250, 10, 100, 60) |
| 158 | const picture = rect(20, 10, 220, 70) |
| 159 | const handles = handlesOf(box) |
| 160 | const at = buttonSpot({ box, handles, avoid: [picture], bound: stage, size }) |
| 161 | const r = rect(at.left, at.top, size, size) |
| 162 | assert.ok(!overlaps(r, picture), `${JSON.stringify(r)} is over the picture`) |
| 163 | for (const h of handles) assert.ok(!overlaps(r, h), `${JSON.stringify(r)} covers a handle`) |
| 164 | assert.ok(r.left >= 0 && r.top >= 0 && r.right <= 360 && r.bottom <= 90, 'inside the stage') |
| 165 | // Where the other boxes leave no room, a box is covered rather than |
| 166 | // the button docked: it is still on the stage, clear of the handles. |
| 167 | const tight = rect(0, 0, 60, 60) |
| 168 | const fill = rect(0, 0, 60, 60) |
| 169 | const spot = buttonSpot({ box: rect(20, 20, 20, 20), handles: [], avoid: [fill], bound: tight, size }) |
| 170 | assert.ok(spot, 'a spot over the other box, rather than none') |
| 171 | }) |
| 172 | |