@zsherman/canvas 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +373 -0
- package/README.md +15 -2
- package/THIRD_PARTY_NOTICES.md +117 -0
- package/dist/dom/canvas/assets.js +89 -0
- package/dist/dom/canvas/board-store.js +56 -0
- package/dist/dom/canvas/canvas-actions.js +78 -0
- package/dist/dom/canvas/canvas-board.js +557 -0
- package/dist/dom/canvas/canvas-board.styles.js +110 -0
- package/dist/dom/canvas/canvas-chrome.js +1167 -0
- package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
- package/dist/dom/canvas/canvas-comments.js +950 -0
- package/dist/dom/canvas/canvas-comments.styles.js +231 -0
- package/dist/dom/canvas/canvas-context.js +196 -0
- package/dist/dom/canvas/canvas-controller.js +697 -0
- package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
- package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
- package/dist/dom/canvas/canvas-menus.js +276 -0
- package/dist/dom/canvas/canvas-menus.styles.js +7 -0
- package/dist/dom/canvas/canvas-style-bar.js +930 -0
- package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
- package/dist/dom/canvas/canvas-surface.js +1354 -0
- package/dist/dom/canvas/canvas-surface.styles.js +345 -0
- package/dist/dom/canvas/canvas-tool-rail.js +406 -0
- package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
- package/dist/dom/canvas/comments.js +58 -0
- package/dist/dom/canvas/equality.js +13 -0
- package/dist/dom/canvas/export-png.js +496 -0
- package/dist/dom/canvas/gesture-machine.js +91 -0
- package/dist/dom/canvas/handlers.js +18 -0
- package/dist/dom/canvas/index.js +9 -0
- package/dist/dom/canvas/input.js +85 -0
- package/dist/dom/canvas/instant.js +28 -0
- package/dist/dom/canvas/kernel.js +39 -0
- package/dist/dom/canvas/motion.js +686 -0
- package/dist/dom/canvas/palette.js +165 -0
- package/dist/dom/canvas/random-id.js +15 -0
- package/dist/dom/canvas/scene-renderer.js +559 -0
- package/dist/dom/canvas/scene.styles.js +7 -0
- package/dist/dom/canvas/surface-model.js +324 -0
- package/dist/dom/canvas/uploads.js +294 -0
- package/dist/dom/index.js +9 -0
- package/dist/dom/kernel/actions.js +455 -0
- package/dist/dom/kernel/board.js +856 -0
- package/dist/dom/kernel/claims.js +40 -0
- package/dist/dom/kernel/clipboard.js +30 -0
- package/dist/dom/kernel/constants.js +210 -0
- package/dist/dom/kernel/contract.js +0 -0
- package/dist/dom/kernel/decode.js +85 -0
- package/dist/dom/kernel/editor.js +2077 -0
- package/dist/dom/kernel/errors.js +82 -0
- package/dist/dom/kernel/fonts/inter.js +7924 -0
- package/dist/dom/kernel/fractional-index.js +54 -0
- package/dist/dom/kernel/geometry.js +123 -0
- package/dist/dom/kernel/hit-test.js +156 -0
- package/dist/dom/kernel/index.js +22 -0
- package/dist/dom/kernel/inspect.js +116 -0
- package/dist/dom/kernel/kernel.js +251 -0
- package/dist/dom/kernel/outline.js +64 -0
- package/dist/dom/kernel/plan.js +1370 -0
- package/dist/dom/kernel/rev.js +173 -0
- package/dist/dom/kernel/scene.js +580 -0
- package/dist/dom/kernel/schema.js +1109 -0
- package/dist/dom/kernel/snap.js +258 -0
- package/dist/dom/kernel/svg.js +148 -0
- package/dist/dom/kernel/text.js +142 -0
- package/dist/solid/canvas/assets.jsx +89 -0
- package/dist/solid/canvas/board-store.jsx +56 -0
- package/dist/solid/canvas/canvas-actions.jsx +78 -0
- package/dist/solid/canvas/canvas-board.jsx +326 -0
- package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
- package/dist/solid/canvas/canvas-chrome.jsx +612 -0
- package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
- package/dist/solid/canvas/canvas-comments.jsx +435 -0
- package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
- package/dist/solid/canvas/canvas-context.jsx +174 -0
- package/dist/solid/canvas/canvas-controller.jsx +697 -0
- package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
- package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
- package/dist/solid/canvas/canvas-menus.jsx +223 -0
- package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
- package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
- package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
- package/dist/solid/canvas/canvas-surface.jsx +681 -0
- package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
- package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
- package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
- package/dist/solid/canvas/comments.jsx +58 -0
- package/dist/solid/canvas/equality.jsx +13 -0
- package/dist/solid/canvas/export-png.jsx +496 -0
- package/dist/solid/canvas/gesture-machine.jsx +91 -0
- package/dist/solid/canvas/handlers.jsx +18 -0
- package/dist/solid/canvas/index.jsx +9 -0
- package/dist/solid/canvas/input.jsx +85 -0
- package/dist/solid/canvas/instant.jsx +28 -0
- package/dist/solid/canvas/kernel.jsx +39 -0
- package/dist/solid/canvas/motion.jsx +686 -0
- package/dist/solid/canvas/palette.jsx +165 -0
- package/dist/solid/canvas/random-id.jsx +15 -0
- package/dist/solid/canvas/scene-renderer.jsx +559 -0
- package/dist/solid/canvas/scene.styles.jsx +7 -0
- package/dist/solid/canvas/surface-model.jsx +324 -0
- package/dist/solid/canvas/uploads.jsx +294 -0
- package/dist/solid/index.jsx +9 -0
- package/dist/solid/kernel/actions.jsx +455 -0
- package/dist/solid/kernel/board.jsx +856 -0
- package/dist/solid/kernel/claims.jsx +40 -0
- package/dist/solid/kernel/clipboard.jsx +30 -0
- package/dist/solid/kernel/constants.jsx +210 -0
- package/dist/solid/kernel/contract.jsx +0 -0
- package/dist/solid/kernel/decode.jsx +85 -0
- package/dist/solid/kernel/editor.jsx +2077 -0
- package/dist/solid/kernel/errors.jsx +82 -0
- package/dist/solid/kernel/fonts/inter.jsx +7924 -0
- package/dist/solid/kernel/fractional-index.jsx +54 -0
- package/dist/solid/kernel/geometry.jsx +123 -0
- package/dist/solid/kernel/hit-test.jsx +156 -0
- package/dist/solid/kernel/index.jsx +22 -0
- package/dist/solid/kernel/inspect.jsx +116 -0
- package/dist/solid/kernel/kernel.jsx +251 -0
- package/dist/solid/kernel/outline.jsx +64 -0
- package/dist/solid/kernel/plan.jsx +1370 -0
- package/dist/solid/kernel/rev.jsx +173 -0
- package/dist/solid/kernel/scene.jsx +580 -0
- package/dist/solid/kernel/schema.jsx +1109 -0
- package/dist/solid/kernel/snap.jsx +258 -0
- package/dist/solid/kernel/svg.jsx +148 -0
- package/dist/solid/kernel/text.jsx +142 -0
- package/dist/styles.css +1 -0
- package/dist/types/canvas/assets.d.ts +111 -0
- package/dist/types/canvas/board-store.d.ts +86 -0
- package/dist/types/canvas/canvas-actions.d.ts +21 -0
- package/dist/types/canvas/canvas-board.d.ts +53 -0
- package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
- package/dist/types/canvas/canvas-chrome.d.ts +109 -0
- package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
- package/dist/types/canvas/canvas-comments.d.ts +20 -0
- package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
- package/dist/types/canvas/canvas-context.d.ts +104 -0
- package/dist/types/canvas/canvas-controller.d.ts +159 -0
- package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
- package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
- package/dist/types/canvas/canvas-menus.d.ts +21 -0
- package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
- package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
- package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
- package/dist/types/canvas/canvas-surface.d.ts +15 -0
- package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
- package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
- package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
- package/dist/types/canvas/comments.d.ts +106 -0
- package/dist/types/canvas/equality.d.ts +4 -0
- package/dist/types/canvas/export-png.d.ts +77 -0
- package/dist/types/canvas/gesture-machine.d.ts +46 -0
- package/dist/types/canvas/handlers.d.ts +7 -0
- package/dist/types/canvas/index.d.ts +18 -0
- package/dist/types/canvas/input.d.ts +63 -0
- package/dist/types/canvas/instant.d.ts +13 -0
- package/dist/types/canvas/kernel.d.ts +27 -0
- package/dist/types/canvas/motion.d.ts +72 -0
- package/dist/types/canvas/palette.d.ts +46 -0
- package/dist/types/canvas/random-id.d.ts +6 -0
- package/dist/types/canvas/scene-renderer.d.ts +27 -0
- package/dist/types/canvas/scene.styles.d.ts +4 -0
- package/dist/types/canvas/surface-model.d.ts +113 -0
- package/dist/types/canvas/uploads.d.ts +58 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/kernel/actions.d.ts +19 -0
- package/dist/types/kernel/board.d.ts +137 -0
- package/dist/types/kernel/claims.d.ts +29 -0
- package/dist/types/kernel/clipboard.d.ts +10 -0
- package/dist/types/kernel/constants.d.ts +195 -0
- package/dist/types/kernel/contract.d.ts +1533 -0
- package/dist/types/kernel/decode.d.ts +60 -0
- package/dist/types/kernel/editor.d.ts +12 -0
- package/dist/types/kernel/errors.d.ts +11 -0
- package/dist/types/kernel/fractional-index.d.ts +7 -0
- package/dist/types/kernel/geometry.d.ts +42 -0
- package/dist/types/kernel/hit-test.d.ts +16 -0
- package/dist/types/kernel/index.d.ts +8 -0
- package/dist/types/kernel/inspect.d.ts +6 -0
- package/dist/types/kernel/kernel.d.ts +6 -0
- package/dist/types/kernel/outline.d.ts +4 -0
- package/dist/types/kernel/plan.d.ts +20 -0
- package/dist/types/kernel/rev.d.ts +21 -0
- package/dist/types/kernel/scene.d.ts +16 -0
- package/dist/types/kernel/schema.d.ts +3436 -0
- package/dist/types/kernel/snap.d.ts +41 -0
- package/dist/types/kernel/svg.d.ts +3 -0
- package/dist/types/kernel/text.d.ts +21 -0
- package/docs/api.json +207 -0
- package/docs/canvas.md +444 -0
- package/docs/kernel.md +108 -0
- package/docs/llms.txt +41 -0
- package/docs/search-index.json +52 -0
- package/package.json +60 -4
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/kernel/fractional-index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Fractional indexes for z-order. A key is a base-62 fraction written with
|
|
4
|
+
* the digits below (ascending in code unit order), with no trailing "0": the
|
|
5
|
+
* key "V" is 31/62. Keys compare by code unit, so `a < b` orders them.
|
|
6
|
+
* Concurrent writers can make equal keys; shapes then order by id.
|
|
7
|
+
*
|
|
8
|
+
* Appending and prepending step in buckets, so keys grow logarithmically: a
|
|
9
|
+
* key with c leading "z" digits (or "0" digits, going down) is cut to 2c + 2
|
|
10
|
+
* digits and stepped by one unit in the last. Inserting into one gap again
|
|
11
|
+
* and again still grows keys; the planner then re-keys (`rekey` in plan.ts).
|
|
12
|
+
*/
|
|
13
|
+
const DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
14
|
+
const KEY = /^[0-9A-Za-z]*[1-9A-Za-z]$/;
|
|
15
|
+
const isKey = (key) => KEY.test(key);
|
|
16
|
+
const digit = (key, index) => {
|
|
17
|
+
const char = key[index];
|
|
18
|
+
return char === void 0 ? 0 : DIGITS.indexOf(char);
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A key strictly between `a` and `b`, where `""` stands for the start and
|
|
22
|
+
* `null` for the end. Requires `a < b` when `b` is not null.
|
|
23
|
+
*/
|
|
24
|
+
const midpoint = (a, b) => {
|
|
25
|
+
if (b !== null) {
|
|
26
|
+
let n = 0;
|
|
27
|
+
while (n < b.length && (a[n] ?? "0") === b[n]) n++;
|
|
28
|
+
if (n > 0) return b.slice(0, n) + midpoint(a.slice(n), b.slice(n));
|
|
29
|
+
}
|
|
30
|
+
const low = a === "" ? 0 : digit(a, 0);
|
|
31
|
+
const high = b === null ? 62 : digit(b, 0);
|
|
32
|
+
if (high - low > 1) return DIGITS[Math.round((low + high) / 2)] ?? "";
|
|
33
|
+
if (b !== null && b.length > 1) return b.slice(0, 1);
|
|
34
|
+
return (DIGITS[low] ?? "") + midpoint(a.slice(1), null);
|
|
35
|
+
};
|
|
36
|
+
/** `key` cut to 2c + 2 digits, c its leading `edge` digits, stepped `by` one unit in the last. */
|
|
37
|
+
const step = (key, edge, by) => {
|
|
38
|
+
let c = 0;
|
|
39
|
+
while (key[c] === edge) c++;
|
|
40
|
+
const digits = Array.from({ length: 2 * c + 2 }, (_, i) => digit(key, i));
|
|
41
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
42
|
+
const next = (digits[i] ?? 0) + by;
|
|
43
|
+
digits[i] = (next + 62) % 62;
|
|
44
|
+
if (next >= 0 && next < 62) break;
|
|
45
|
+
}
|
|
46
|
+
return digits.map((d) => DIGITS[d]).join("").replace(/0+$/, "");
|
|
47
|
+
};
|
|
48
|
+
/** A key after `a`, or "V" when `a` is null. */
|
|
49
|
+
const keyAfter = (a) => a === null ? "V" : step(a, "z", 1);
|
|
50
|
+
/** A key before `b`, or "V" when `b` is null. */
|
|
51
|
+
const keyBefore = (b) => b === null ? "V" : step(b, "0", -1);
|
|
52
|
+
/** A key strictly between `a` and `b`; null stands for the start or the end. Requires `a < b`. */
|
|
53
|
+
const keyBetween = (a, b) => midpoint(a ?? "", b);
|
|
54
|
+
export { isKey, keyAfter, keyBefore, keyBetween };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// src/kernel/geometry.ts
|
|
2
|
+
/**
|
|
3
|
+
* Rounds to hundredths, halves toward +∞ (`Math.round`). Every coordinate the
|
|
4
|
+
* kernel stores or draws passes through this.
|
|
5
|
+
*/
|
|
6
|
+
const round = (value) => {
|
|
7
|
+
const rounded = Math.round(value * 100) / 100;
|
|
8
|
+
return rounded === 0 ? 0 : rounded;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Rounds up to hundredths, so a stored size never falls short of what it must
|
|
12
|
+
* hold: an auto-width text rounded down would be narrower than its own line
|
|
13
|
+
* and wrap.
|
|
14
|
+
*/
|
|
15
|
+
const roundUp = (value) => {
|
|
16
|
+
const rounded = round(value);
|
|
17
|
+
return rounded < value ? round(rounded + .01) : rounded;
|
|
18
|
+
};
|
|
19
|
+
/** Formats a number for text output: rounded, no exponent, no trailing zeros. */
|
|
20
|
+
const formatNumber = (value) => String(round(value));
|
|
21
|
+
/**
|
|
22
|
+
* An instant (integer epoch milliseconds, 0 to 8.64e15) as ISO 8601 in UTC,
|
|
23
|
+
* such as `2026-11-20T15:04:00.000Z`; years past 9999 take the `+275760`
|
|
24
|
+
* form. Never a local time zone.
|
|
25
|
+
*/
|
|
26
|
+
const isoInstant = (epochMs) => new Date(epochMs).toISOString();
|
|
27
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
28
|
+
const center = (b) => ({
|
|
29
|
+
x: b.x + b.w / 2,
|
|
30
|
+
y: b.y + b.h / 2
|
|
31
|
+
});
|
|
32
|
+
/** The box spanned by two corners. */
|
|
33
|
+
const boxFromPoints = (a, b) => ({
|
|
34
|
+
x: Math.min(a.x, b.x),
|
|
35
|
+
y: Math.min(a.y, b.y),
|
|
36
|
+
w: Math.abs(b.x - a.x),
|
|
37
|
+
h: Math.abs(b.y - a.y)
|
|
38
|
+
});
|
|
39
|
+
const roundBox = (b) => ({
|
|
40
|
+
x: round(b.x),
|
|
41
|
+
y: round(b.y),
|
|
42
|
+
w: round(b.w),
|
|
43
|
+
h: round(b.h)
|
|
44
|
+
});
|
|
45
|
+
const roundPoint = (p) => ({
|
|
46
|
+
x: round(p.x),
|
|
47
|
+
y: round(p.y)
|
|
48
|
+
});
|
|
49
|
+
const inflate = (b, by) => ({
|
|
50
|
+
x: b.x - by,
|
|
51
|
+
y: b.y - by,
|
|
52
|
+
w: b.w + by * 2,
|
|
53
|
+
h: b.h + by * 2
|
|
54
|
+
});
|
|
55
|
+
const union = (boxes) => {
|
|
56
|
+
const [first, ...rest] = boxes;
|
|
57
|
+
if (first === void 0) return null;
|
|
58
|
+
let minX = first.x;
|
|
59
|
+
let minY = first.y;
|
|
60
|
+
let maxX = first.x + first.w;
|
|
61
|
+
let maxY = first.y + first.h;
|
|
62
|
+
for (const b of rest) {
|
|
63
|
+
minX = Math.min(minX, b.x);
|
|
64
|
+
minY = Math.min(minY, b.y);
|
|
65
|
+
maxX = Math.max(maxX, b.x + b.w);
|
|
66
|
+
maxY = Math.max(maxY, b.y + b.h);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
x: minX,
|
|
70
|
+
y: minY,
|
|
71
|
+
w: maxX - minX,
|
|
72
|
+
h: maxY - minY
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
/** Whether two boxes share area or touch. */
|
|
76
|
+
const intersects = (a, b) => a.x <= b.x + b.w && b.x <= a.x + a.w && a.y <= b.y + b.h && b.y <= a.y + a.h;
|
|
77
|
+
/** Whether two boxes share area; touching edges do not overlap. */
|
|
78
|
+
const overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
|
|
79
|
+
const distanceToSegment = (p, a, b) => {
|
|
80
|
+
const dx = b.x - a.x;
|
|
81
|
+
const dy = b.y - a.y;
|
|
82
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
83
|
+
const t = lengthSquared === 0 ? 0 : clamp(((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared, 0, 1);
|
|
84
|
+
const ex = a.x + t * dx - p.x;
|
|
85
|
+
const ey = a.y + t * dy - p.y;
|
|
86
|
+
return Math.sqrt(ex * ex + ey * ey);
|
|
87
|
+
};
|
|
88
|
+
const distance = (a, b) => {
|
|
89
|
+
const dx = b.x - a.x;
|
|
90
|
+
const dy = b.y - a.y;
|
|
91
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
92
|
+
};
|
|
93
|
+
/** Where a ray from the center of `b` toward `toward` leaves the rectangle `b`. */
|
|
94
|
+
const rectExit = (b, toward) => {
|
|
95
|
+
const c = center(b);
|
|
96
|
+
const dx = toward.x - c.x;
|
|
97
|
+
const dy = toward.y - c.y;
|
|
98
|
+
if (dx === 0 && dy === 0) return c;
|
|
99
|
+
const sx = dx === 0 ? Number.POSITIVE_INFINITY : b.w / 2 / Math.abs(dx);
|
|
100
|
+
const sy = dy === 0 ? Number.POSITIVE_INFINITY : b.h / 2 / Math.abs(dy);
|
|
101
|
+
const s = Math.min(sx, sy);
|
|
102
|
+
return {
|
|
103
|
+
x: c.x + dx * s,
|
|
104
|
+
y: c.y + dy * s
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
/** Where a ray from the center of `b` toward `toward` leaves the ellipse inscribed in `b`. */
|
|
108
|
+
const ellipseExit = (b, toward) => {
|
|
109
|
+
const c = center(b);
|
|
110
|
+
const dx = toward.x - c.x;
|
|
111
|
+
const dy = toward.y - c.y;
|
|
112
|
+
const rx = b.w / 2;
|
|
113
|
+
const ry = b.h / 2;
|
|
114
|
+
if (dx === 0 && dy === 0 || rx === 0 || ry === 0) return c;
|
|
115
|
+
const nx = dx / rx;
|
|
116
|
+
const ny = dy / ry;
|
|
117
|
+
const s = 1 / Math.sqrt(nx * nx + ny * ny);
|
|
118
|
+
return {
|
|
119
|
+
x: c.x + dx * s,
|
|
120
|
+
y: c.y + dy * s
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
export { boxFromPoints, center, clamp, distance, distanceToSegment, ellipseExit, formatNumber, inflate, intersects, isoInstant, overlaps, rectExit, round, roundBox, roundPoint, roundUp, union };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { INTERACTION, STROKE_WIDTH } from "./constants.js";
|
|
2
|
+
import { boundsOf, inkPoints, inkStroke, route, routeEnd, routeStart, strokeCurve } from "./board.js";
|
|
3
|
+
import { center, distance, distanceToSegment, inflate } from "./geometry.js";
|
|
4
|
+
// src/kernel/hit-test.ts
|
|
5
|
+
/**
|
|
6
|
+
* Hit-testing, front to back: the selection's connector ends and resize
|
|
7
|
+
* handles first, then shapes from the top. Tolerances are in screen pixels
|
|
8
|
+
* and depend on the pointer: touch is widest.
|
|
9
|
+
*/
|
|
10
|
+
const RESIZE_HANDLES = [
|
|
11
|
+
"nw",
|
|
12
|
+
"n",
|
|
13
|
+
"ne",
|
|
14
|
+
"e",
|
|
15
|
+
"se",
|
|
16
|
+
"s",
|
|
17
|
+
"sw",
|
|
18
|
+
"w"
|
|
19
|
+
];
|
|
20
|
+
/** Where a resize handle sits on a box. */
|
|
21
|
+
const handlePoint = (b, handle) => {
|
|
22
|
+
return {
|
|
23
|
+
x: handle.includes("w") ? b.x : handle.includes("e") ? b.x + b.w : b.x + b.w / 2,
|
|
24
|
+
y: handle.includes("n") ? b.y : handle.includes("s") ? b.y + b.h : b.y + b.h / 2
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/** The shapes a selection's handles act on: unlocked, existing, not only connectors. */
|
|
28
|
+
const resizable = (shapes, selection) => {
|
|
29
|
+
const out = [];
|
|
30
|
+
for (const id of selection) {
|
|
31
|
+
const shape = shapes.get(id);
|
|
32
|
+
if (shape !== void 0 && !shape.locked) out.push(id);
|
|
33
|
+
}
|
|
34
|
+
return out.every((id) => shapes.get(id)?.kind === "Arrow") ? [] : out;
|
|
35
|
+
};
|
|
36
|
+
/** The span of a→b inside the closed box (Liang–Barsky). */
|
|
37
|
+
const inBox = (a, b, box) => {
|
|
38
|
+
let t0 = 0;
|
|
39
|
+
let t1 = 1;
|
|
40
|
+
for (const [p, q] of [
|
|
41
|
+
[a.x - b.x, a.x - box.x],
|
|
42
|
+
[b.x - a.x, box.x + box.w - a.x],
|
|
43
|
+
[a.y - b.y, a.y - box.y],
|
|
44
|
+
[b.y - a.y, box.y + box.h - a.y]
|
|
45
|
+
]) if (p === 0) {
|
|
46
|
+
if (q < 0) return null;
|
|
47
|
+
} else if (p < 0) t0 = Math.max(t0, q / p);
|
|
48
|
+
else t1 = Math.min(t1, q / p);
|
|
49
|
+
return t0 <= t1 ? [t0, t1] : null;
|
|
50
|
+
};
|
|
51
|
+
/** The span of a→b inside the ellipse inscribed in `box`, grown by `by` on each side. */
|
|
52
|
+
const inOval = (a, b, box, by) => {
|
|
53
|
+
const rx = box.w / 2 + by;
|
|
54
|
+
const ry = box.h / 2 + by;
|
|
55
|
+
if (rx <= 0 || ry <= 0) return null;
|
|
56
|
+
const c = center(box);
|
|
57
|
+
const ux = (a.x - c.x) / rx;
|
|
58
|
+
const uy = (a.y - c.y) / ry;
|
|
59
|
+
const dx = (b.x - a.x) / rx;
|
|
60
|
+
const dy = (b.y - a.y) / ry;
|
|
61
|
+
const qa = dx * dx + dy * dy;
|
|
62
|
+
const qb = ux * dx + uy * dy;
|
|
63
|
+
const qc = ux * ux + uy * uy;
|
|
64
|
+
if (qa === 0) return qc <= 1 ? [0, 1] : null;
|
|
65
|
+
const disc = qb * qb - qa * (qc - 1);
|
|
66
|
+
if (disc < 0) return null;
|
|
67
|
+
const root = Math.sqrt(disc);
|
|
68
|
+
const t0 = Math.max(0, (-qb - root) / qa);
|
|
69
|
+
const t1 = Math.min(1, (-qb + root) / qa);
|
|
70
|
+
return t0 <= t1 ? [t0, t1] : null;
|
|
71
|
+
};
|
|
72
|
+
const cross = (o, p, q) => (p.x - o.x) * (q.y - o.y) - (p.y - o.y) * (q.x - o.x);
|
|
73
|
+
/** Whether segment a→b passes within `reach` of a polyline. */
|
|
74
|
+
const nearPolyline = (a, b, points, reach) => {
|
|
75
|
+
for (let i = points.length === 1 ? 0 : 1; i < points.length; i++) {
|
|
76
|
+
const d = points[i];
|
|
77
|
+
const c = points[i - 1] ?? d;
|
|
78
|
+
if (c === void 0 || d === void 0) continue;
|
|
79
|
+
const ab = cross(c, d, a) * cross(c, d, b);
|
|
80
|
+
const cd = cross(a, b, c) * cross(a, b, d);
|
|
81
|
+
if (ab < 0 && cd < 0) return true;
|
|
82
|
+
if (Math.min(distanceToSegment(a, c, d), distanceToSegment(b, c, d), distanceToSegment(c, a, b), distanceToSegment(d, a, b)) <= reach) return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Whether segment a→b (a point when `b` is left out) hits `shape`, exactly:
|
|
88
|
+
* no sampling, so one long eraser move costs one test per shape.
|
|
89
|
+
* `tolerance` is in board units.
|
|
90
|
+
*/
|
|
91
|
+
const hitsShape = (shapes, shape, a, tolerance, selected, b = a) => {
|
|
92
|
+
switch (shape.kind) {
|
|
93
|
+
case "Arrow": return nearPolyline(a, b, route(shapes, shape).points, tolerance + STROKE_WIDTH.arrow / 2);
|
|
94
|
+
case "Ink": {
|
|
95
|
+
const width = inkStroke(shape.style).width;
|
|
96
|
+
return inBox(a, b, inflate(shape, tolerance + width)) !== null && nearPolyline(a, b, strokeCurve(inkPoints(shape)), tolerance + width / 2);
|
|
97
|
+
}
|
|
98
|
+
case "Box": {
|
|
99
|
+
const ellipse = shape.style.form === "ellipse";
|
|
100
|
+
const span = (by) => ellipse ? inOval(a, b, shape, by) : inBox(a, b, inflate(shape, by));
|
|
101
|
+
const outer = span(tolerance);
|
|
102
|
+
if (outer === null) return false;
|
|
103
|
+
if (shape.style.fill || selected || shape.text !== "") return true;
|
|
104
|
+
const inner = span(-(tolerance + STROKE_WIDTH.box));
|
|
105
|
+
return inner === null || outer[0] < inner[0] || outer[1] > inner[1];
|
|
106
|
+
}
|
|
107
|
+
case "Sticky":
|
|
108
|
+
case "Text":
|
|
109
|
+
case "Image":
|
|
110
|
+
case "Timestamp": return inBox(a, b, inflate(shape, tolerance)) !== null;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const hitTestIndex = (index, probe) => {
|
|
114
|
+
const p = probe.point;
|
|
115
|
+
const tolerance = INTERACTION.hitTolerance[probe.pointer] / probe.zoom;
|
|
116
|
+
const handleReach = (INTERACTION.handleSize / 2 + INTERACTION.handleTolerance[probe.pointer]) / probe.zoom;
|
|
117
|
+
const selected = new Set(probe.selection);
|
|
118
|
+
if (probe.selection.length === 1) {
|
|
119
|
+
const only = index.shapes.get(probe.selection[0] ?? "");
|
|
120
|
+
if (only?.kind === "Arrow" && !only.locked) {
|
|
121
|
+
const r = route(index.shapes, only);
|
|
122
|
+
if (distance(p, routeEnd(r)) <= handleReach) return {
|
|
123
|
+
_tag: "End",
|
|
124
|
+
id: only.id,
|
|
125
|
+
end: "to"
|
|
126
|
+
};
|
|
127
|
+
if (distance(p, routeStart(r)) <= handleReach) return {
|
|
128
|
+
_tag: "End",
|
|
129
|
+
id: only.id,
|
|
130
|
+
end: "from"
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const handled = resizable(index.shapes, probe.selection);
|
|
135
|
+
const box = boundsOf(index.shapes, handled);
|
|
136
|
+
if (box !== null) {
|
|
137
|
+
for (const handle of RESIZE_HANDLES) if (distance(p, handlePoint(box, handle)) <= handleReach) return {
|
|
138
|
+
_tag: "Handle",
|
|
139
|
+
handle
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
for (let i = index.ordered.length - 1; i >= 0; i--) {
|
|
143
|
+
const shape = index.ordered[i];
|
|
144
|
+
if (shape !== void 0 && hitsShape(index.shapes, shape, p, tolerance, selected.has(shape.id))) return {
|
|
145
|
+
_tag: "Shape",
|
|
146
|
+
id: shape.id
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return { _tag: "Nothing" };
|
|
150
|
+
};
|
|
151
|
+
/** Shapes whose geometry passes within `reach` of the segment a→b (the eraser). */
|
|
152
|
+
const touchedBySegment = (index, a, b, reach, pointer, zoom) => {
|
|
153
|
+
const tolerance = reach + INTERACTION.hitTolerance[pointer] / zoom;
|
|
154
|
+
return index.ordered.filter((shape) => !shape.locked && hitsShape(index.shapes, shape, a, tolerance, false, b)).map((shape) => shape.id);
|
|
155
|
+
};
|
|
156
|
+
export { RESIZE_HANDLES, handlePoint, hitTestIndex, hitsShape, resizable, touchedBySegment };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { HISTORY } from "./constants.js";
|
|
2
|
+
import { decodeClipboard } from "./clipboard.js";
|
|
3
|
+
import { loadKernel } from "./kernel.js";
|
|
4
|
+
// src/kernel/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* @hypervideo/canvas-kernel: the headless Hyper Canvas board kernel.
|
|
7
|
+
*
|
|
8
|
+
* `loadKernel(fonts)` is the one asynchronous call; `decodeClipboard(text)`
|
|
9
|
+
* reads clipboard text without a kernel; `EMPTY_BOARD` and `BOARD_HISTORY`
|
|
10
|
+
* are the values. Every kernel function is pure, synchronous, takes and returns plain
|
|
11
|
+
* JSON, and returns `{ ok }` or `{ error }` instead of throwing. Every type is
|
|
12
|
+
* in `./contract`.
|
|
13
|
+
*/
|
|
14
|
+
/** A board with no shapes and no history: where every new board starts. */
|
|
15
|
+
const EMPTY_BOARD = Object.freeze({
|
|
16
|
+
shapes: Object.freeze([]),
|
|
17
|
+
head: 0,
|
|
18
|
+
transactions: Object.freeze([])
|
|
19
|
+
});
|
|
20
|
+
/** The bound on every board's history, which `apply` keeps. */
|
|
21
|
+
const BOARD_HISTORY = HISTORY;
|
|
22
|
+
export { BOARD_HISTORY, EMPTY_BOARD, decodeClipboard, loadKernel };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { hasText, history, pendingProposals } from "./board.js";
|
|
2
|
+
import { isoInstant } from "./geometry.js";
|
|
3
|
+
// src/kernel/inspect.ts
|
|
4
|
+
const shared = (values) => {
|
|
5
|
+
const [first, ...rest] = values;
|
|
6
|
+
if (first === void 0) return null;
|
|
7
|
+
return rest.every((value) => value === first) ? first : "mixed";
|
|
8
|
+
};
|
|
9
|
+
/** A shape's style; images and timestamps have none. */
|
|
10
|
+
const styleOf = (shape) => shape.kind === "Image" || shape.kind === "Timestamp" ? null : shape.style;
|
|
11
|
+
const collect = (shapes, read) => {
|
|
12
|
+
const values = [];
|
|
13
|
+
for (const shape of shapes) {
|
|
14
|
+
const value = read(shape);
|
|
15
|
+
if (value !== null) values.push(value);
|
|
16
|
+
}
|
|
17
|
+
return shared(values);
|
|
18
|
+
};
|
|
19
|
+
const inspectShapes = (index, ids) => {
|
|
20
|
+
const shapes = [];
|
|
21
|
+
const seen = /* @__PURE__ */ new Set();
|
|
22
|
+
for (const id of ids) {
|
|
23
|
+
const shape = index.shapes.get(id);
|
|
24
|
+
if (shape === void 0 || seen.has(id)) continue;
|
|
25
|
+
seen.add(id);
|
|
26
|
+
shapes.push(shape);
|
|
27
|
+
}
|
|
28
|
+
const kinds = [];
|
|
29
|
+
for (const shape of shapes) if (!kinds.includes(shape.kind)) kinds.push(shape.kind);
|
|
30
|
+
const unlocked = shapes.filter((shape) => !shape.locked);
|
|
31
|
+
const bindable = shapes.filter((shape) => shape.kind !== "Arrow");
|
|
32
|
+
return {
|
|
33
|
+
ids: shapes.map((shape) => shape.id),
|
|
34
|
+
kinds,
|
|
35
|
+
style: {
|
|
36
|
+
color: collect(shapes, (s) => styleOf(s)?.color ?? null),
|
|
37
|
+
size: collect(shapes, (s) => {
|
|
38
|
+
const style = styleOf(s);
|
|
39
|
+
return style !== null && "size" in style ? style.size : null;
|
|
40
|
+
}),
|
|
41
|
+
align: collect(shapes, (s) => {
|
|
42
|
+
const style = styleOf(s);
|
|
43
|
+
return style !== null && "align" in style ? style.align : null;
|
|
44
|
+
}),
|
|
45
|
+
form: collect(shapes, (s) => s.kind === "Box" ? s.style.form : null),
|
|
46
|
+
fill: collect(shapes, (s) => s.kind === "Box" ? s.style.fill : null),
|
|
47
|
+
dash: collect(shapes, (s) => s.kind === "Box" || s.kind === "Arrow" ? s.style.dash : null),
|
|
48
|
+
brush: collect(shapes, (s) => s.kind === "Ink" ? s.style.brush : null),
|
|
49
|
+
start: collect(shapes, (s) => s.kind === "Arrow" ? s.style.start : null),
|
|
50
|
+
end: collect(shapes, (s) => s.kind === "Arrow" ? s.style.end : null),
|
|
51
|
+
route: collect(shapes, (s) => s.kind === "Arrow" ? s.style.route : null)
|
|
52
|
+
},
|
|
53
|
+
can: {
|
|
54
|
+
restyle: shapes.length > 0 && unlocked.length === shapes.length,
|
|
55
|
+
editText: shapes.length === 1 && unlocked.length === 1 && shapes.every(hasText),
|
|
56
|
+
delete: shapes.length > 0 && unlocked.length === shapes.length,
|
|
57
|
+
duplicate: shapes.length > 0,
|
|
58
|
+
group: unlocked.length >= 2 && unlocked.length === shapes.length && !(shapes[0]?.group !== null && shapes.every((s) => s.group === shapes[0]?.group)),
|
|
59
|
+
ungroup: unlocked.some((shape) => shape.group !== null),
|
|
60
|
+
lock: unlocked.length > 0,
|
|
61
|
+
unlock: shapes.some((shape) => shape.locked),
|
|
62
|
+
align: unlocked.length >= 2 && unlocked.length === shapes.length,
|
|
63
|
+
distribute: unlocked.length >= 3 && unlocked.length === shapes.length,
|
|
64
|
+
reorder: shapes.length > 0 && unlocked.length === shapes.length,
|
|
65
|
+
connect: shapes.length === 2 && bindable.length === 2
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const quote = (text) => {
|
|
70
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
71
|
+
return flat.length > 60 ? `“${flat.slice(0, 57)}…”` : `“${flat}”`;
|
|
72
|
+
};
|
|
73
|
+
const kindName = (shape) => {
|
|
74
|
+
switch (shape.kind) {
|
|
75
|
+
case "Sticky": return "sticky note";
|
|
76
|
+
case "Box": return shape.style.form;
|
|
77
|
+
case "Text": return "text";
|
|
78
|
+
case "Arrow": return "connector";
|
|
79
|
+
case "Ink": return shape.style.brush === "pen" ? "drawing" : `${shape.style.brush} stroke`;
|
|
80
|
+
case "Image": return "image";
|
|
81
|
+
case "Timestamp": return `timestamp ${isoInstant(shape.at)}`;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const label = (shape) => {
|
|
85
|
+
const text = shape.kind === "Image" ? shape.alt : hasText(shape) ? shape.text : "";
|
|
86
|
+
return text.trim() === "" ? kindName(shape) : `${kindName(shape)} ${quote(text)}`;
|
|
87
|
+
};
|
|
88
|
+
const describeShape = (index, shape) => {
|
|
89
|
+
const end = (anchor) => {
|
|
90
|
+
if (anchor._tag === "Point") return "a point";
|
|
91
|
+
const target = index.shapes.get(anchor.id);
|
|
92
|
+
return target === void 0 ? "a removed shape" : label(target);
|
|
93
|
+
};
|
|
94
|
+
const base = shape.kind === "Image" ? `image: ${shape.alt.trim() === "" ? "no description" : quote(shape.alt)}` : shape.kind === "Timestamp" ? label(shape) : shape.kind === "Arrow" ? `${shape.style.color} connector from ${end(shape.from)} to ${end(shape.to)}` : `${shape.style.color} ${label(shape)}`;
|
|
95
|
+
const flags = [...shape.locked ? ["locked"] : [], ...shape.group === null ? [] : ["grouped"]];
|
|
96
|
+
const sentence = flags.length === 0 ? base : `${base}, ${flags.join(", ")}`;
|
|
97
|
+
return sentence.charAt(0).toUpperCase() + sentence.slice(1);
|
|
98
|
+
};
|
|
99
|
+
const listProposals = (board, index) => pendingProposals(board, history(board)).map((record) => {
|
|
100
|
+
let added = 0;
|
|
101
|
+
let changed = 0;
|
|
102
|
+
let removed = 0;
|
|
103
|
+
for (const change of record.changes) if (change.op === "remove") removed++;
|
|
104
|
+
else if (change.op === "patch" || index.shapes.has(change.shape.id)) changed++;
|
|
105
|
+
else added++;
|
|
106
|
+
return {
|
|
107
|
+
transaction: record.id,
|
|
108
|
+
author: record.author,
|
|
109
|
+
reason: record.reason,
|
|
110
|
+
shapes: record.shapes,
|
|
111
|
+
added,
|
|
112
|
+
changed,
|
|
113
|
+
removed
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
export { describeShape, inspectShapes, listProposals };
|