forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

board-shapes.ts194 lines, 8.4 KiBHistoryRawDownload
1import type { Point, Stroke } from "./draw-stroke";
2
3// Whiteboard shapes (Excalidraw-style rectangle, diamond, ellipse, line and
4// arrow) are baked into ordinary freehand strokes before they leave the
5// client. The board backend only stores stroke payloads, so a shape travels,
6// persists and replays exactly like a pen line — no new wire kind, and every
7// history replay draws it without knowing it was ever a shape.
8
9export type ShapeKind = "rectangle" | "diamond" | "ellipse" | "line" | "arrow";
10/** 0 architect (clean), 1 artist (a gentle hand wobble), 2 cartoonist (two loose passes). */
11export type Sloppiness = 0 | 1 | 2;
12export type ShapeStyle = Pick<Stroke, "color" | "width" | "dash">;
13
14/** Below this drag distance (world px) a shape is a stray click, not a shape. */
15const MIN_SIZE = 2;
16/** Distance of the guard points placed beside every sharp corner. */
17const CORNER_GUARD = 1.5;
18const ELLIPSE_SEGMENTS = 72;
19
20/** Small deterministic PRNG (mulberry32) so a seed always yields the same wobble. */
21function random(seed: number): () => number {
22 let a = seed >>> 0;
23 return () => {
24 a = (a + 0x6d2b79f5) >>> 0;
25 let t = a;
26 t = Math.imul(t ^ (t >>> 15), t | 1);
27 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
28 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
29 };
30}
31
32/** Shift-drag: equal sides for boxes, a 45° snap for lines and arrows. */
33export function constrainEnd(kind: ShapeKind, a: Point, b: Point): Point {
34 const dx = b.x - a.x;
35 const dy = b.y - a.y;
36 if (kind === "line" || kind === "arrow") {
37 const step = Math.PI / 4;
38 const angle = Math.round(Math.atan2(dy, dx) / step) * step;
39 const len = Math.hypot(dx, dy);
40 return { x: a.x + Math.cos(angle) * len, y: a.y + Math.sin(angle) * len };
41 }
42 const side = Math.max(Math.abs(dx), Math.abs(dy));
43 return { x: a.x + (dx < 0 ? -side : side), y: a.y + (dy < 0 ? -side : side) };
44}
45
46/**
47 * Points from `a` (inclusive) toward `b` (exclusive). The renderer smooths a
48 * polyline with midpoint quadratics, which would cut a bare corner to the
49 * midpoints of its edges — so a guard point sits just inside each end and the
50 * corner only rounds by a pixel. A sloppy edge bows sideways along a half sine.
51 */
52function edge(a: Point, b: Point, bow: number, rand: () => number, jitter: number): Point[] {
53 const dx = b.x - a.x;
54 const dy = b.y - a.y;
55 const len = Math.hypot(dx, dy);
56 if (len === 0) return [a];
57 const nx = -dy / len;
58 const ny = dx / len;
59 const guard = Math.min(CORNER_GUARD, len / 4) / len;
60 const inner = bow === 0 ? 0 : Math.min(16, Math.max(2, Math.ceil(len / 24)));
61 const ts = [0, guard];
62 for (let i = 1; i < inner; i += 1) ts.push(i / inner);
63 ts.push(1 - guard);
64 const out: Point[] = [];
65 for (const t of ts) {
66 const off = Math.sin(Math.PI * t) * bow + (t === 0 ? (rand() * 2 - 1) * jitter : 0);
67 out.push({ x: a.x + dx * t + nx * off, y: a.y + dy * t + ny * off });
68 }
69 return out;
70}
71
72/** One closed or open pass through `vertices`, each edge bowed by the sloppiness. */
73function polygonPass(vertices: Point[], closed: boolean, slop: Sloppiness, rand: () => number): Point[] {
74 const last = closed ? vertices.length : vertices.length - 1;
75 const out: Point[] = [];
76 for (let i = 0; i < last; i += 1) {
77 const a = vertices[i];
78 const b = vertices[(i + 1) % vertices.length];
79 const len = Math.hypot(b.x - a.x, b.y - a.y);
80 const bow = slop === 0 ? 0 : (rand() * 2 - 1) * Math.min(len * 0.015 * slop, 1.6 * slop);
81 out.push(...edge(a, b, bow, rand, slop * 0.6));
82 }
83 const end = closed ? vertices[0] : vertices[vertices.length - 1];
84 out.push(end);
85 // A loose hand overshoots where a closed outline meets itself.
86 if (closed && slop > 0 && out.length > 2) {
87 const next = out[1];
88 const f = 0.35 + rand() * 0.25;
89 out.push({ x: end.x + (next.x - end.x) * f * slop, y: end.y + (next.y - end.y) * f * slop });
90 }
91 return out;
92}
93
94/** Rounded-corner rectangle (Excalidraw's default edges) as one outline pass. */
95function rectanglePass(x0: number, y0: number, x1: number, y1: number, width: number, slop: Sloppiness, rand: () => number): Point[] {
96 const w = x1 - x0;
97 const h = y1 - y0;
98 const r = Math.min(32, Math.min(w, h) * 0.25);
99 if (r < 2) return polygonPass([{ x: x0, y: y0 }, { x: x1, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y1 }], true, slop, rand);
100 const corners = [
101 { cx: x1 - r, cy: y0 + r, from: -Math.PI / 2 },
102 { cx: x1 - r, cy: y1 - r, from: 0 },
103 { cx: x0 + r, cy: y1 - r, from: Math.PI / 2 },
104 { cx: x0 + r, cy: y0 + r, from: Math.PI },
105 ];
106 const arcSteps = Math.max(4, Math.ceil(r / 3));
107 const out: Point[] = [];
108 let prev: Point = { x: x0 + r, y: y0 };
109 for (const c of corners) {
110 const start = { x: c.cx + Math.cos(c.from) * r, y: c.cy + Math.sin(c.from) * r };
111 const len = Math.hypot(start.x - prev.x, start.y - prev.y);
112 const bow = slop === 0 ? 0 : (rand() * 2 - 1) * Math.min(len * 0.012 * slop, 1.4 * slop);
113 out.push(...edge(prev, start, bow, rand, slop * 0.5));
114 for (let i = 0; i < arcSteps; i += 1) {
115 const a = c.from + (Math.PI / 2) * (i / arcSteps);
116 out.push({ x: c.cx + Math.cos(a) * r, y: c.cy + Math.sin(a) * r });
117 }
118 prev = { x: c.cx + Math.cos(c.from + Math.PI / 2) * r, y: c.cy + Math.sin(c.from + Math.PI / 2) * r };
119 }
120 out.push(prev);
121 if (slop > 0) {
122 const reach = Math.min(w - 2 * r, 6 + width * 2) * (0.4 + rand() * 0.4) * slop;
123 out.push({ x: prev.x + Math.max(0, reach), y: prev.y + (rand() * 2 - 1) * slop });
124 }
125 return out;
126}
127
128function ellipsePass(cx: number, cy: number, rx: number, ry: number, slop: Sloppiness, rand: () => number): Point[] {
129 const start = slop === 0 ? -Math.PI / 2 : rand() * Math.PI * 2;
130 // The wobble is a slow radial wave, so the outline stays smooth, and a loose
131 // pass runs a little past its start instead of closing exactly.
132 const amp = 0.012 * slop;
133 const phase = rand() * Math.PI * 2;
134 const sweep = Math.PI * 2 * (1 + 0.04 * slop);
135 const steps = Math.ceil(ELLIPSE_SEGMENTS * (1 + 0.04 * slop));
136 const out: Point[] = [];
137 for (let i = 0; i <= steps; i += 1) {
138 const a = start + (sweep * i) / steps;
139 const k = 1 + amp * Math.sin(2 * a + phase);
140 out.push({ x: cx + Math.cos(a) * rx * k, y: cy + Math.sin(a) * ry * k });
141 }
142 return out;
143}
144
145function onePass(kind: ShapeKind, a: Point, b: Point, width: number, slop: Sloppiness, rand: () => number): Point[][] {
146 const x0 = Math.min(a.x, b.x);
147 const y0 = Math.min(a.y, b.y);
148 const x1 = Math.max(a.x, b.x);
149 const y1 = Math.max(a.y, b.y);
150 switch (kind) {
151 case "rectangle":
152 return [rectanglePass(x0, y0, x1, y1, width, slop, rand)];
153 case "diamond": {
154 const mx = (x0 + x1) / 2;
155 const my = (y0 + y1) / 2;
156 return [polygonPass([{ x: mx, y: y0 }, { x: x1, y: my }, { x: mx, y: y1 }, { x: x0, y: my }], true, slop, rand)];
157 }
158 case "ellipse":
159 return [ellipsePass((x0 + x1) / 2, (y0 + y1) / 2, (x1 - x0) / 2, (y1 - y0) / 2, slop, rand)];
160 case "line":
161 return [polygonPass([a, b], false, slop, rand)];
162 case "arrow": {
163 const len = Math.hypot(b.x - a.x, b.y - a.y);
164 const angle = Math.atan2(b.y - a.y, b.x - a.x);
165 const head = Math.min(len / 2.5, 14 + width * 2.5);
166 const spread = Math.PI / 7;
167 const left = { x: b.x - Math.cos(angle - spread) * head, y: b.y - Math.sin(angle - spread) * head };
168 const right = { x: b.x - Math.cos(angle + spread) * head, y: b.y - Math.sin(angle + spread) * head };
169 return [polygonPass([a, b], false, slop, rand), polygonPass([left, b, right], false, slop, rand)];
170 }
171 }
172}
173
174/**
175 * Bake a dragged shape from `a` to `b` (board-space) into strokes ready to send.
176 * An arrow is two strokes (shaft, head); a cartoonist shape is drawn twice with
177 * a fresh wobble. `seed` fixes the wobble so the live preview and the committed
178 * shape match. Returns [] for a click that never became a shape.
179 */
180export function shapeStrokes(kind: ShapeKind, a: Point, b: Point, style: ShapeStyle, slop: Sloppiness, seed: number): Stroke[] {
181 const dx = Math.abs(b.x - a.x);
182 const dy = Math.abs(b.y - a.y);
183 const tooSmall = kind === "line" || kind === "arrow" ? Math.hypot(dx, dy) < MIN_SIZE : dx < MIN_SIZE || dy < MIN_SIZE;
184 if (tooSmall) return [];
185 const rand = random(seed);
186 const passes = slop === 2 ? 2 : 1;
187 const out: Stroke[] = [];
188 for (let p = 0; p < passes; p += 1) {
189 for (const points of onePass(kind, a, b, style.width, slop, rand)) {
190 out.push({ color: style.color, width: style.width, erase: false, ...(style.dash ? { dash: style.dash } : {}), points });
191 }
192 }
193 return out;
194}