hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Point, Stroke } from "./draw-stroke"; |
| 2 | |
| 3 | // Hit testing for the whiteboard's object eraser: a drag of the eraser is a |
| 4 | // chain of short segments, and a stroke is hit when any of them passes within |
| 5 | // the stroke's half-width (plus the eraser's own reach) of its polyline. |
| 6 | |
| 7 | type Box = { x0: number; y0: number; x1: number; y1: number }; |
| 8 | |
| 9 | // Stroke points never change once committed, so the box is computed once. |
| 10 | const boxes = new WeakMap<Stroke, Box>(); |
| 11 | |
| 12 | function boxOf(stroke: Stroke): Box { |
| 13 | let box = boxes.get(stroke); |
| 14 | if (!box) { |
| 15 | box = { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity }; |
| 16 | for (const p of stroke.points) { |
| 17 | box.x0 = Math.min(box.x0, p.x); |
| 18 | box.y0 = Math.min(box.y0, p.y); |
| 19 | box.x1 = Math.max(box.x1, p.x); |
| 20 | box.y1 = Math.max(box.y1, p.y); |
| 21 | } |
| 22 | boxes.set(stroke, box); |
| 23 | } |
| 24 | return box; |
| 25 | } |
| 26 | |
| 27 | function pointSegmentDist(p: Point, a: Point, b: Point): number { |
| 28 | const dx = b.x - a.x; |
| 29 | const dy = b.y - a.y; |
| 30 | const len2 = dx * dx + dy * dy; |
| 31 | const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)); |
| 32 | return Math.hypot(p.x - (a.x + dx * t), p.y - (a.y + dy * t)); |
| 33 | } |
| 34 | |
| 35 | function cross(o: Point, a: Point, b: Point): number { |
| 36 | return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); |
| 37 | } |
| 38 | |
| 39 | /** Shortest distance between segments ab and cd (0 when they cross). */ |
| 40 | export function segmentDist(a: Point, b: Point, c: Point, d: Point): number { |
| 41 | const d1 = cross(c, d, a); |
| 42 | const d2 = cross(c, d, b); |
| 43 | const d3 = cross(a, b, c); |
| 44 | const d4 = cross(a, b, d); |
| 45 | if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) return 0; |
| 46 | return Math.min(pointSegmentDist(a, c, d), pointSegmentDist(b, c, d), pointSegmentDist(c, a, b), pointSegmentDist(d, a, b)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Does the eraser move from `a` to `b` touch `stroke`? `radius` is the eraser's |
| 51 | * reach in board-space. Pixel-eraser strokes (older boards) are never targets: |
| 52 | * they carry no ink of their own. |
| 53 | */ |
| 54 | export function strokeHit(stroke: Stroke, a: Point, b: Point, radius: number): boolean { |
| 55 | const pts = stroke.points; |
| 56 | if (stroke.erase || pts.length === 0) return false; |
| 57 | const reach = stroke.width / 2 + radius; |
| 58 | const box = boxOf(stroke); |
| 59 | if ( |
| 60 | Math.max(a.x, b.x) < box.x0 - reach || |
| 61 | Math.min(a.x, b.x) > box.x1 + reach || |
| 62 | Math.max(a.y, b.y) < box.y0 - reach || |
| 63 | Math.min(a.y, b.y) > box.y1 + reach |
| 64 | ) { |
| 65 | return false; |
| 66 | } |
| 67 | if (pts.length === 1) return pointSegmentDist(pts[0], a, b) <= reach; |
| 68 | for (let i = 1; i < pts.length; i += 1) { |
| 69 | if (segmentDist(a, b, pts[i - 1], pts[i]) <= reach) return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |