autokast 0.0.0 → 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.
Files changed (79) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +165 -0
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +300 -0
  5. package/dist/index.d.ts +4 -0
  6. package/dist/index.js +4 -0
  7. package/dist/inspect.d.ts +26 -0
  8. package/dist/inspect.js +193 -0
  9. package/dist/record/animations.d.ts +7 -0
  10. package/dist/record/animations.js +34 -0
  11. package/dist/record/capture.d.ts +85 -0
  12. package/dist/record/capture.js +202 -0
  13. package/dist/record/cast.d.ts +142 -0
  14. package/dist/record/cast.js +680 -0
  15. package/dist/record/cursor-style.d.ts +33 -0
  16. package/dist/record/cursor-style.js +252 -0
  17. package/dist/record/defaults.d.ts +96 -0
  18. package/dist/record/defaults.js +125 -0
  19. package/dist/record/editor.d.ts +11 -0
  20. package/dist/record/editor.js +48 -0
  21. package/dist/record/frame-pump.d.ts +91 -0
  22. package/dist/record/frame-pump.js +306 -0
  23. package/dist/record/index.d.ts +11 -0
  24. package/dist/record/index.js +287 -0
  25. package/dist/record/motion.d.ts +48 -0
  26. package/dist/record/motion.js +112 -0
  27. package/dist/record/pace.d.ts +69 -0
  28. package/dist/record/pace.js +133 -0
  29. package/dist/record/random.d.ts +12 -0
  30. package/dist/record/random.js +24 -0
  31. package/dist/record/scroll.d.ts +71 -0
  32. package/dist/record/scroll.js +249 -0
  33. package/dist/record/timeline.d.ts +34 -0
  34. package/dist/record/timeline.js +44 -0
  35. package/dist/render/background.d.ts +12 -0
  36. package/dist/render/background.js +209 -0
  37. package/dist/render/browser.d.ts +46 -0
  38. package/dist/render/browser.js +223 -0
  39. package/dist/render/camera.d.ts +119 -0
  40. package/dist/render/camera.js +372 -0
  41. package/dist/render/captions.d.ts +114 -0
  42. package/dist/render/captions.js +254 -0
  43. package/dist/render/color.d.ts +23 -0
  44. package/dist/render/color.js +78 -0
  45. package/dist/render/compositor.d.ts +185 -0
  46. package/dist/render/compositor.js +656 -0
  47. package/dist/render/cursor.d.ts +85 -0
  48. package/dist/render/cursor.js +314 -0
  49. package/dist/render/cursors/shapes.d.ts +34 -0
  50. package/dist/render/cursors/shapes.js +268 -0
  51. package/dist/render/defaults.d.ts +91 -0
  52. package/dist/render/defaults.js +139 -0
  53. package/dist/render/easing.d.ts +13 -0
  54. package/dist/render/easing.js +40 -0
  55. package/dist/render/effects.d.ts +39 -0
  56. package/dist/render/effects.js +91 -0
  57. package/dist/render/encoder.d.ts +52 -0
  58. package/dist/render/encoder.js +226 -0
  59. package/dist/render/index.d.ts +19 -0
  60. package/dist/render/index.js +138 -0
  61. package/dist/render/keycaps.d.ts +67 -0
  62. package/dist/render/keycaps.js +270 -0
  63. package/dist/render/scene.d.ts +59 -0
  64. package/dist/render/scene.js +230 -0
  65. package/dist/render/source.d.ts +21 -0
  66. package/dist/render/source.js +57 -0
  67. package/dist/render/spring.d.ts +11 -0
  68. package/dist/render/spring.js +47 -0
  69. package/dist/render/stage.d.ts +59 -0
  70. package/dist/render/stage.js +91 -0
  71. package/dist/render/timeline.d.ts +70 -0
  72. package/dist/render/timeline.js +157 -0
  73. package/dist/session.d.ts +14 -0
  74. package/dist/session.js +33 -0
  75. package/dist/types.d.ts +708 -0
  76. package/dist/types.js +1 -0
  77. package/package.json +61 -3
  78. package/skills/autokast/SKILL.md +300 -0
  79. package/skills/autokast/reference/jupyterlab.md +100 -0
@@ -0,0 +1,85 @@
1
+ import type { CursorSample, CursorStyle, TimelineEvent } from '../types.ts';
2
+ import type { TimedEvent, TimeMap } from './timeline.ts';
3
+ export interface CursorTrack {
4
+ /**
5
+ * Drawn hotspot position in viewport CSS pixels.
6
+ */
7
+ x: Float64Array;
8
+ y: Float64Array;
9
+ /**
10
+ * 1 where the recorded cursor moved since the previous frame.
11
+ */
12
+ moving: Uint8Array;
13
+ opacity: Float64Array;
14
+ style: CursorStyle[];
15
+ previousStyle: CursorStyle[];
16
+ /**
17
+ * Progress of the change from `previousStyle` (0) to `style` (1).
18
+ */
19
+ styleMix: Float64Array;
20
+ /**
21
+ * Press amount: 1 fully pressed, 0 released (slightly negative during the release bounce).
22
+ */
23
+ press: Float64Array;
24
+ /**
25
+ * Lean of the cursor body around its hotspot, in radians (positive is clockwise).
26
+ */
27
+ angle: Float64Array;
28
+ }
29
+ export interface ClickMark {
30
+ out: number;
31
+ /**
32
+ * Record time of the press, which selects the frame for the adaptive color.
33
+ */
34
+ t: number;
35
+ x: number;
36
+ y: number;
37
+ }
38
+ /**
39
+ * Smooth, overshoot-free interpolation of cursor samples (Steffen's monotone cubic,
40
+ * per axis, with non-uniform times). Returns a function of record time.
41
+ */
42
+ export declare function cursorPath(samples: CursorSample[]): (t: number) => {
43
+ x: number;
44
+ y: number;
45
+ };
46
+ /**
47
+ * Maps any cursor value to a shape that the renderer can draw.
48
+ */
49
+ export declare function normalizeStyle(style: string | undefined): CursorStyle;
50
+ export interface CursorTrackOptions {
51
+ visible: boolean;
52
+ smoothing: number;
53
+ hideAfter: number;
54
+ /**
55
+ * Strength of the lean: 0 off, 1 the default look.
56
+ */
57
+ tilt?: number;
58
+ }
59
+ /**
60
+ * Cursor lean: a damped spring pulls the angle toward a target set by the horizontal
61
+ * speed of the hotspot on the page, `TILT_MAX * tanh(vx / TILT_SPEED)`. The body trails
62
+ * behind like a weight hanging from the tip; vertical travel gives no lean, as a body
63
+ * hanging in line with its pivot does not turn. Slightly underdamped: a soft build-up and
64
+ * a small, quick swing back when the cursor stops.
65
+ */
66
+ export declare const TILT_MAX: number;
67
+ export declare const TILT_SPEED = 1400;
68
+ export declare const TILT_OMEGA = 14;
69
+ export declare const TILT_DAMPING = 0.78;
70
+ export declare function tiltFactor(style: CursorStyle): number;
71
+ /**
72
+ * Lean angle per output frame from the hotspot positions, in radians. At a break (a cut)
73
+ * the jump is not motion, so it gives no push; the angle stays continuous.
74
+ */
75
+ export declare function tiltTrack(times: ArrayLike<number>, x: ArrayLike<number>, breaks: ArrayLike<boolean>, strength: number): Float64Array;
76
+ /**
77
+ * Computes the drawn cursor for every output frame: interpolated position, zero-lag
78
+ * spring smoothing (pinned to the recorded position while a button is down), style
79
+ * changes, press amount and idle fade.
80
+ */
81
+ export declare function computeCursor(times: ArrayLike<number>, samples: CursorSample[], rawEvents: TimelineEvent[], map: TimeMap, options: CursorTrackOptions): CursorTrack;
82
+ /**
83
+ * Positions of the click effects: one per mouseDown outside the cuts.
84
+ */
85
+ export declare function clickMarks(events: TimedEvent[]): ClickMark[];
@@ -0,0 +1,314 @@
1
+ import { clamp, smoothstep } from "./easing.js";
2
+ import { springSmooth, springStep } from "./spring.js";
3
+ const STYLES = new Set([
4
+ 'default',
5
+ 'pointer',
6
+ 'text',
7
+ 'grab',
8
+ 'grabbing',
9
+ 'crosshair',
10
+ 'move',
11
+ 'ew-resize',
12
+ 'ns-resize',
13
+ 'nesw-resize',
14
+ 'nwse-resize',
15
+ 'not-allowed',
16
+ 'none',
17
+ ]);
18
+ /**
19
+ * Durations of the cursor animations, in output milliseconds.
20
+ */
21
+ const STYLE_FADE = 140;
22
+ const PRESS_IN = 90;
23
+ const PRESS_OUT = 160;
24
+ const PIN_RAMP = 160;
25
+ const IDLE_FADE_OUT = 320;
26
+ const IDLE_FADE_IN = 160;
27
+ /**
28
+ * Smooth, overshoot-free interpolation of cursor samples (Steffen's monotone cubic,
29
+ * per axis, with non-uniform times). Returns a function of record time.
30
+ */
31
+ export function cursorPath(samples) {
32
+ const pts = [];
33
+ for (const s of [...samples].sort((a, b) => a.t - b.t)) {
34
+ if (pts.length && pts[pts.length - 1].t === s.t)
35
+ pts[pts.length - 1] = s;
36
+ else
37
+ pts.push(s);
38
+ }
39
+ const n = pts.length;
40
+ if (n === 0)
41
+ return () => ({ x: 0, y: 0 });
42
+ const t = pts.map((p) => p.t);
43
+ const tangents = (v) => {
44
+ const m = new Float64Array(n);
45
+ for (let i = 1; i < n - 1; i++) {
46
+ const h0 = t[i] - t[i - 1];
47
+ const h1 = t[i + 1] - t[i];
48
+ const d0 = (v[i] - v[i - 1]) / h0;
49
+ const d1 = (v[i + 1] - v[i]) / h1;
50
+ if (d0 * d1 <= 0)
51
+ continue;
52
+ const p = (d0 * h1 + d1 * h0) / (h0 + h1);
53
+ m[i] = Math.sign(d0) * Math.min(2 * Math.abs(d0), 2 * Math.abs(d1), Math.abs(p));
54
+ }
55
+ return m;
56
+ };
57
+ const xs = pts.map((p) => p.x);
58
+ const ys = pts.map((p) => p.y);
59
+ const mx = tangents(xs);
60
+ const my = tangents(ys);
61
+ return (time) => {
62
+ if (time <= t[0])
63
+ return { x: xs[0], y: ys[0] };
64
+ if (time >= t[n - 1])
65
+ return { x: xs[n - 1], y: ys[n - 1] };
66
+ let lo = 0;
67
+ let hi = n - 1;
68
+ while (hi - lo > 1) {
69
+ const mid = (lo + hi) >> 1;
70
+ if (t[mid] <= time)
71
+ lo = mid;
72
+ else
73
+ hi = mid;
74
+ }
75
+ const h = t[hi] - t[lo];
76
+ const u = (time - t[lo]) / h;
77
+ const u2 = u * u;
78
+ const u3 = u2 * u;
79
+ const h00 = 2 * u3 - 3 * u2 + 1;
80
+ const h10 = u3 - 2 * u2 + u;
81
+ const h01 = -2 * u3 + 3 * u2;
82
+ const h11 = u3 - u2;
83
+ return {
84
+ x: h00 * xs[lo] + h10 * h * mx[lo] + h01 * xs[hi] + h11 * h * mx[hi],
85
+ y: h00 * ys[lo] + h10 * h * my[lo] + h01 * ys[hi] + h11 * h * my[hi],
86
+ };
87
+ };
88
+ }
89
+ /**
90
+ * Maps any cursor value to a shape that the renderer can draw.
91
+ */
92
+ export function normalizeStyle(style) {
93
+ return style && STYLES.has(style) ? style : 'default';
94
+ }
95
+ function easeOutCubic(t) {
96
+ return 1 - (1 - t) ** 3;
97
+ }
98
+ function easeOutBack(t) {
99
+ const c = 1.4;
100
+ return 1 + (c + 1) * (t - 1) ** 3 + c * (t - 1) ** 2;
101
+ }
102
+ /**
103
+ * Cursor lean: a damped spring pulls the angle toward a target set by the horizontal
104
+ * speed of the hotspot on the page, `TILT_MAX * tanh(vx / TILT_SPEED)`. The body trails
105
+ * behind like a weight hanging from the tip; vertical travel gives no lean, as a body
106
+ * hanging in line with its pivot does not turn. Slightly underdamped: a soft build-up and
107
+ * a small, quick swing back when the cursor stops.
108
+ */
109
+ export const TILT_MAX = (13 * Math.PI) / 180;
110
+ export const TILT_SPEED = 1400;
111
+ export const TILT_OMEGA = 14;
112
+ export const TILT_DAMPING = 0.78;
113
+ /**
114
+ * How much each cursor shape leans. Shapes that read as axes or symbols stay upright.
115
+ */
116
+ const TILT_FACTOR = {
117
+ default: 1,
118
+ pointer: 0.85,
119
+ grab: 0.6,
120
+ grabbing: 0.6,
121
+ };
122
+ export function tiltFactor(style) {
123
+ return TILT_FACTOR[style] ?? 0;
124
+ }
125
+ /**
126
+ * Lean angle per output frame from the hotspot positions, in radians. At a break (a cut)
127
+ * the jump is not motion, so it gives no push; the angle stays continuous.
128
+ */
129
+ export function tiltTrack(times, x, breaks, strength) {
130
+ const n = times.length;
131
+ const out = new Float64Array(n);
132
+ if (strength <= 0)
133
+ return out;
134
+ let angle = 0;
135
+ let spin = 0;
136
+ for (let i = 1; i < n; i++) {
137
+ const dt = (times[i] - times[i - 1]) / 1000;
138
+ if (dt <= 0) {
139
+ out[i] = angle;
140
+ continue;
141
+ }
142
+ const vx = breaks[i] ? 0 : (x[i] - x[i - 1]) / dt;
143
+ const target = strength * TILT_MAX * Math.tanh(vx / TILT_SPEED);
144
+ const steps = Math.ceil(dt / 0.002);
145
+ const h = dt / steps;
146
+ for (let k = 0; k < steps; k++) {
147
+ spin +=
148
+ (TILT_OMEGA * TILT_OMEGA * (target - angle) - 2 * TILT_DAMPING * TILT_OMEGA * spin) * h;
149
+ angle += spin * h;
150
+ }
151
+ out[i] = angle;
152
+ }
153
+ return out;
154
+ }
155
+ /**
156
+ * Computes the drawn cursor for every output frame: interpolated position, zero-lag
157
+ * spring smoothing (pinned to the recorded position while a button is down), style
158
+ * changes, press amount and idle fade.
159
+ */
160
+ export function computeCursor(times, samples, rawEvents, map, options) {
161
+ const n = times.length;
162
+ const track = {
163
+ x: new Float64Array(n),
164
+ y: new Float64Array(n),
165
+ moving: new Uint8Array(n),
166
+ opacity: new Float64Array(n),
167
+ style: new Array(n),
168
+ previousStyle: new Array(n),
169
+ styleMix: new Float64Array(n),
170
+ press: new Float64Array(n),
171
+ angle: new Float64Array(n),
172
+ };
173
+ const path = cursorPath(samples);
174
+ const rec = new Float64Array(n);
175
+ const rawX = new Float64Array(n);
176
+ const rawY = new Float64Array(n);
177
+ const breaks = new Array(n).fill(false);
178
+ for (let i = 0; i < n; i++) {
179
+ rec[i] = map.toRecord(times[i]);
180
+ const p = path(rec[i]);
181
+ rawX[i] = p.x;
182
+ rawY[i] = p.y;
183
+ if (i > 0) {
184
+ breaks[i] = map.cutBetween(rec[i - 1], rec[i] + 1e-6);
185
+ track.moving[i] = Math.abs(p.x - rawX[i - 1]) + Math.abs(p.y - rawY[i - 1]) > 0.02 ? 1 : 0;
186
+ }
187
+ }
188
+ const sorted = [...rawEvents].sort((a, b) => a.t - b.t);
189
+ const mouse = sorted.filter((e) => e.type === 'mouseDown' || e.type === 'mouseUp');
190
+ const styles = sorted.filter((e) => e.type === 'cursorStyle');
191
+ const pressed = new Uint8Array(n);
192
+ const recStyle = new Array(n);
193
+ let mi = 0;
194
+ let si = 0;
195
+ let down = false;
196
+ let current = 'default';
197
+ for (let i = 0; i < n; i++) {
198
+ for (; mi < mouse.length && mouse[mi].t <= rec[i]; mi++)
199
+ down = mouse[mi].type === 'mouseDown';
200
+ for (; si < styles.length && styles[si].t <= rec[i]; si++)
201
+ current = normalizeStyle(styles[si].style);
202
+ pressed[i] = down ? 1 : 0;
203
+ recStyle[i] = current;
204
+ }
205
+ if (options.smoothing > 0 && n > 1) {
206
+ const omega = 1 / (0.15 * clamp(options.smoothing, 0.01, 1));
207
+ const sx = springSmooth(times, rawX, omega, breaks);
208
+ const sy = springSmooth(times, rawY, omega, breaks);
209
+ const pin = pinWeights(times, pressed);
210
+ for (let i = 0; i < n; i++) {
211
+ track.x[i] = sx[i] + (rawX[i] - sx[i]) * pin[i];
212
+ track.y[i] = sy[i] + (rawY[i] - sy[i]) * pin[i];
213
+ }
214
+ }
215
+ else {
216
+ track.x.set(rawX);
217
+ track.y.set(rawY);
218
+ }
219
+ let style = recStyle[0] ?? 'default';
220
+ let previous = style;
221
+ let changedAt = -Infinity;
222
+ let press = 0;
223
+ let pressFrom = 0;
224
+ let pressTarget = 0;
225
+ let pressAt = -Infinity;
226
+ for (let i = 0; i < n; i++) {
227
+ const t = times[i];
228
+ const s = recStyle[i];
229
+ if (s !== style) {
230
+ previous = track.styleMix[i - 1] < 0.5 ? previous : style;
231
+ style = s;
232
+ changedAt = t;
233
+ }
234
+ track.style[i] = style;
235
+ track.previousStyle[i] = previous;
236
+ track.styleMix[i] = smoothstep(0, 1, (t - changedAt) / STYLE_FADE);
237
+ const target = pressed[i];
238
+ if (target !== pressTarget) {
239
+ pressFrom = press;
240
+ pressTarget = target;
241
+ pressAt = t;
242
+ }
243
+ const k = (t - pressAt) / (pressTarget ? PRESS_IN : PRESS_OUT);
244
+ press =
245
+ k >= 1
246
+ ? pressTarget
247
+ : pressFrom + (pressTarget - pressFrom) * (pressTarget ? easeOutCubic(k) : easeOutBack(k));
248
+ track.press[i] = press;
249
+ }
250
+ const lean = tiltTrack(times, track.x, breaks, options.tilt ?? 0);
251
+ let factor = n > 0 ? tiltFactor(track.style[0]) : 0;
252
+ let rate = 0;
253
+ for (let i = 0; i < n; i++) {
254
+ if (i > 0) {
255
+ const target = tiltFactor(track.style[i]);
256
+ [factor, rate] = springStep(factor, rate, target, 28, (times[i] - times[i - 1]) / 1000);
257
+ }
258
+ track.angle[i] = lean[i] * factor;
259
+ }
260
+ const hasCursor = options.visible && samples.length > 0;
261
+ if (!hasCursor)
262
+ return track;
263
+ if (options.hideAfter <= 0) {
264
+ track.opacity.fill(1);
265
+ return track;
266
+ }
267
+ const active = new Uint8Array(n);
268
+ let lastActive = -Infinity;
269
+ for (let i = 0; i < n; i++) {
270
+ if (i === 0 || track.moving[i] || pressed[i])
271
+ lastActive = times[i];
272
+ active[i] = times[i] - lastActive < options.hideAfter ? 1 : 0;
273
+ }
274
+ const fade = new Float64Array(n);
275
+ for (let i = 0; i < n; i++) {
276
+ fade[i] = active[i]
277
+ ? 1
278
+ : Math.max(0, (i ? fade[i - 1] : 1) - (times[i] - times[i - 1]) / IDLE_FADE_OUT);
279
+ }
280
+ let ahead = 0;
281
+ for (let i = n - 1; i >= 0; i--) {
282
+ ahead = active[i] ? 1 : Math.max(0, ahead - (times[i + 1] - times[i]) / IDLE_FADE_IN);
283
+ track.opacity[i] = smoothstep(0, 1, Math.max(fade[i], ahead));
284
+ }
285
+ return track;
286
+ }
287
+ /**
288
+ * Weight 1 while a button is down, easing to 0 over PIN_RAMP before and after.
289
+ */
290
+ function pinWeights(times, pressed) {
291
+ const n = times.length;
292
+ const w = new Float64Array(n);
293
+ let last = -Infinity;
294
+ for (let i = 0; i < n; i++) {
295
+ if (pressed[i])
296
+ last = times[i];
297
+ w[i] = 1 - smoothstep(0, PIN_RAMP, times[i] - last);
298
+ }
299
+ let nextPress = Infinity;
300
+ for (let i = n - 1; i >= 0; i--) {
301
+ if (pressed[i])
302
+ nextPress = times[i];
303
+ w[i] = Math.max(w[i], 1 - smoothstep(0, PIN_RAMP, nextPress - times[i]));
304
+ }
305
+ return w;
306
+ }
307
+ /**
308
+ * Positions of the click effects: one per mouseDown outside the cuts.
309
+ */
310
+ export function clickMarks(events) {
311
+ return events
312
+ .filter((e) => e.type === 'mouseDown')
313
+ .map((e) => ({ out: e.out, t: e.t, x: e.x, y: e.y }));
314
+ }
@@ -0,0 +1,34 @@
1
+ import { Path2D, type SKRSContext2D } from '@napi-rs/canvas';
2
+ import type { CursorStyle } from '../../types.ts';
3
+ /**
4
+ * A vector cursor in design units (1 unit = 1 CSS pixel at size 1; the arrow is about
5
+ * 20 units tall). `body` is filled black over the white `outline` silhouette;
6
+ * `details` are white marks drawn on top of the body.
7
+ */
8
+ export interface CursorShape {
9
+ body: Path2D;
10
+ outline: Path2D;
11
+ details?: Path2D;
12
+ hotspot: {
13
+ x: number;
14
+ y: number;
15
+ };
16
+ }
17
+ /**
18
+ * The shape for a cursor style, built once. `none` has no shape.
19
+ */
20
+ export declare function cursorShape(style: CursorStyle): CursorShape | null;
21
+ /**
22
+ * Transform from design units to output pixels: scale, then rotate by `angle` (radians,
23
+ * clockwise) about the hotspot, which lands exactly on (x, y).
24
+ */
25
+ export declare function cursorMatrix(hotspot: {
26
+ x: number;
27
+ y: number;
28
+ }, x: number, y: number, scale: number, angle: number): [number, number, number, number, number, number];
29
+ /**
30
+ * Draws a cursor with its hotspot at (x, y) in output pixels. `scale` is output
31
+ * pixels per design unit; the body leans by `angle` about the hotspot. The drop shadow
32
+ * has a tight contact layer and a soft layer, offset straight down whatever the angle.
33
+ */
34
+ export declare function drawCursor(ctx: SKRSContext2D, style: CursorStyle, x: number, y: number, scale: number, alpha: number, angle?: number): void;
@@ -0,0 +1,268 @@
1
+ import { Path2D, PathOp, StrokeCap, StrokeJoin } from '@napi-rs/canvas';
2
+ const OUTLINE = 1.35;
3
+ const INK = '#101012';
4
+ /**
5
+ * Closed polygon with a rounding radius per corner.
6
+ */
7
+ function roundedPolygon(corners) {
8
+ const p = new Path2D();
9
+ const n = corners.length;
10
+ const [lx, ly] = corners[n - 1];
11
+ const [fx, fy] = corners[0];
12
+ p.moveTo((lx + fx) / 2, (ly + fy) / 2);
13
+ for (let i = 0; i < n; i++) {
14
+ const [x, y, r] = corners[i];
15
+ const [nx, ny] = corners[(i + 1) % n];
16
+ p.arcTo(x, y, nx, ny, r);
17
+ }
18
+ p.closePath();
19
+ return p;
20
+ }
21
+ function capsule(x, y, w, h) {
22
+ const p = new Path2D();
23
+ p.roundRect(x, y, w, h, Math.min(w, h) / 2);
24
+ return p;
25
+ }
26
+ /**
27
+ * Capsule of width `w` along the segment (x0, y0) to (x1, y1).
28
+ */
29
+ function limb(x0, y0, x1, y1, w) {
30
+ const line = new Path2D();
31
+ line.moveTo(x0, y0);
32
+ line.lineTo(x1, y1);
33
+ return strokeOf(line, w);
34
+ }
35
+ function union(...paths) {
36
+ let result = new Path2D(paths[0]);
37
+ for (const p of paths.slice(1))
38
+ result = result.op(p, PathOp.Union);
39
+ return result.simplify().asWinding();
40
+ }
41
+ /**
42
+ * Outline of a stroke as a fillable path. Path2D operations change the path in place,
43
+ * so this works on a copy.
44
+ */
45
+ function strokeOf(path, width, cap = StrokeCap.Round) {
46
+ return new Path2D(path).stroke({ width, cap, join: StrokeJoin.Round });
47
+ }
48
+ /**
49
+ * Builds the white silhouette: the body grown by the outline width.
50
+ */
51
+ function shape(body, hotspot, details, outline = union(body, strokeOf(body, OUTLINE * 2))) {
52
+ return { body, outline, details, hotspot };
53
+ }
54
+ function arrow() {
55
+ const body = roundedPolygon([
56
+ [0, 0, 0.35],
57
+ [0, 15.9, 0.55],
58
+ [3.75, 12.45, 0.35],
59
+ [6.3, 18.25, 0.6],
60
+ [8.95, 17.1, 0.6],
61
+ [6.45, 11.45, 0.35],
62
+ [11.55, 11.45, 0.55],
63
+ ]);
64
+ return shape(body, { x: 0, y: 0 });
65
+ }
66
+ function pointingHand() {
67
+ const palm = roundedPolygon([
68
+ [4.7, 11.2, 1],
69
+ [17.2, 11.2, 1.2],
70
+ [17.2, 16.2, 2.6],
71
+ [15.5, 19.8, 0.8],
72
+ [15.5, 22.2, 0.5],
73
+ [7.1, 22.2, 0.5],
74
+ [7.1, 20.6, 1.6],
75
+ [4.7, 17.4, 1.6],
76
+ ]);
77
+ const body = union(capsule(4.7, 0, 3.6, 14), capsule(8.3, 7.2, 3.3, 7.8), capsule(11.6, 8.3, 3.05, 7.2), capsule(14.65, 9.8, 2.55, 6.2), palm, limb(6.9, 18.3, 1.95, 12.9, 3.3))
78
+ .round(0.5)
79
+ .asWinding();
80
+ const lines = new Path2D();
81
+ lines.moveTo(8.4, 9.9);
82
+ lines.lineTo(8.4, 12.2);
83
+ lines.moveTo(11.6, 10.9);
84
+ lines.lineTo(11.6, 12.9);
85
+ lines.moveTo(14.65, 12.1);
86
+ lines.lineTo(14.65, 13.5);
87
+ return shape(body, { x: 6.5, y: 0.6 }, strokeOf(lines, 0.8));
88
+ }
89
+ function openHand() {
90
+ const palm = roundedPolygon([
91
+ [4.9, 10.4, 1],
92
+ [16.6, 10.4, 1],
93
+ [16.6, 15.6, 2.8],
94
+ [15.0, 19.4, 0.8],
95
+ [15.0, 21.8, 0.5],
96
+ [7.1, 21.8, 0.5],
97
+ [7.1, 19.9, 1.4],
98
+ [4.9, 16.2, 1.5],
99
+ ]);
100
+ const body = union(capsule(4.9, 2.4, 2.9, 11), capsule(8.2, 0.8, 2.9, 12), capsule(11.5, 1.8, 2.8, 11), capsule(14.6, 4.4, 2.5, 9), palm, limb(6.9, 17.6, 1.7, 11.0, 3.1))
101
+ .round(0.5)
102
+ .asWinding();
103
+ return shape(body, { x: 10.6, y: 11.5 });
104
+ }
105
+ function closedHand() {
106
+ const palm = roundedPolygon([
107
+ [4.9, 9.6, 1.2],
108
+ [16.6, 9.6, 1.2],
109
+ [16.6, 15.0, 2.8],
110
+ [15.0, 18.9, 0.8],
111
+ [15.0, 21.0, 0.5],
112
+ [7.1, 21.0, 0.5],
113
+ [7.1, 19.2, 1.4],
114
+ [4.9, 15.6, 1.5],
115
+ ]);
116
+ const body = union(capsule(4.9, 6.6, 2.95, 6), capsule(8.2, 5.8, 2.95, 6), capsule(11.5, 6.2, 2.85, 6), capsule(14.55, 7.4, 2.5, 5), palm, limb(6.9, 15.6, 3.6, 11.6, 3.1))
117
+ .round(0.5)
118
+ .asWinding();
119
+ const lines = new Path2D();
120
+ for (const [x, y] of [
121
+ [8.05, 7.9],
122
+ [11.35, 7.9],
123
+ [14.4, 8.8],
124
+ ]) {
125
+ lines.moveTo(x, y);
126
+ lines.lineTo(x, y + 2.2);
127
+ }
128
+ return shape(body, { x: 10.6, y: 12.5 }, strokeOf(lines, 0.75));
129
+ }
130
+ function iBeam() {
131
+ const line = new Path2D();
132
+ for (const s of [-1, 1]) {
133
+ line.moveTo(-3.3, s * 9.4);
134
+ line.bezierCurveTo(-1.3, s * 9.4, 0, s * 8.9, 0, s * 7.3);
135
+ line.bezierCurveTo(0, s * 8.9, 1.3, s * 9.4, 3.3, s * 9.4);
136
+ }
137
+ line.moveTo(0, -7.3);
138
+ line.lineTo(0, 7.3);
139
+ return shape(strokeOf(line, 1.45), { x: 0, y: 0 });
140
+ }
141
+ function crosshair() {
142
+ const line = new Path2D();
143
+ for (const [dx, dy] of [
144
+ [1, 0],
145
+ [-1, 0],
146
+ [0, 1],
147
+ [0, -1],
148
+ ]) {
149
+ line.moveTo(dx * 2.6, dy * 2.6);
150
+ line.lineTo(dx * 9, dy * 9);
151
+ }
152
+ const dot = new Path2D();
153
+ dot.arc(0, 0, 0.95, 0, Math.PI * 2);
154
+ return shape(union(strokeOf(line, 1.3), dot), { x: 0, y: 0 });
155
+ }
156
+ /**
157
+ * Horizontal double arrow centered on the origin, `half` units from center to tip.
158
+ */
159
+ function doubleArrow(half) {
160
+ const head = 4.6;
161
+ const wing = 3.7;
162
+ const shaft = 0.95;
163
+ return roundedPolygon([
164
+ [-half, 0, 0.45],
165
+ [-half + head, -wing, 0.45],
166
+ [-half + head, -shaft, 0.2],
167
+ [half - head, -shaft, 0.2],
168
+ [half - head, -wing, 0.45],
169
+ [half, 0, 0.45],
170
+ [half - head, wing, 0.45],
171
+ [half - head, shaft, 0.2],
172
+ [-half + head, shaft, 0.2],
173
+ [-half + head, wing, 0.45],
174
+ ]);
175
+ }
176
+ function rotated(path, degrees) {
177
+ const a = (degrees * Math.PI) / 180;
178
+ const c = Math.cos(a);
179
+ const s = Math.sin(a);
180
+ return new Path2D(path).transform({ a: c, b: s, c: -s, d: c, e: 0, f: 0 });
181
+ }
182
+ function resize(degrees) {
183
+ return shape(rotated(doubleArrow(9.6), degrees), { x: 0, y: 0 });
184
+ }
185
+ function move() {
186
+ const body = union(doubleArrow(10.2), rotated(doubleArrow(10.2), 90));
187
+ return shape(body, { x: 0, y: 0 });
188
+ }
189
+ function notAllowed() {
190
+ const disc = (r) => {
191
+ const p = new Path2D();
192
+ p.arc(0, 0, r, 0, Math.PI * 2);
193
+ p.closePath();
194
+ return p;
195
+ };
196
+ const ring = disc(8.15).op(disc(5.85), PathOp.Difference).asWinding();
197
+ const bar = rotated(capsule(-7, -1.15, 14, 2.3), 45).op(disc(7), PathOp.Intersect);
198
+ return shape(union(ring, bar), { x: 0, y: 0 }, undefined, disc(8.15 + OUTLINE));
199
+ }
200
+ const BUILDERS = {
201
+ default: arrow,
202
+ pointer: pointingHand,
203
+ text: iBeam,
204
+ grab: openHand,
205
+ grabbing: closedHand,
206
+ crosshair,
207
+ move,
208
+ 'ew-resize': () => resize(0),
209
+ 'ns-resize': () => resize(90),
210
+ 'nwse-resize': () => resize(45),
211
+ 'nesw-resize': () => resize(-45),
212
+ 'not-allowed': notAllowed,
213
+ };
214
+ const cache = new Map();
215
+ /**
216
+ * The shape for a cursor style, built once. `none` has no shape.
217
+ */
218
+ export function cursorShape(style) {
219
+ if (style === 'none')
220
+ return null;
221
+ let s = cache.get(style);
222
+ if (!s) {
223
+ s = (BUILDERS[style] ?? arrow)();
224
+ cache.set(style, s);
225
+ }
226
+ return s;
227
+ }
228
+ /**
229
+ * Transform from design units to output pixels: scale, then rotate by `angle` (radians,
230
+ * clockwise) about the hotspot, which lands exactly on (x, y).
231
+ */
232
+ export function cursorMatrix(hotspot, x, y, scale, angle) {
233
+ if (angle === 0)
234
+ return [scale, 0, 0, scale, x - hotspot.x * scale, y - hotspot.y * scale];
235
+ const a = scale * Math.cos(angle);
236
+ const b = scale * Math.sin(angle);
237
+ return [a, b, -b, a, x - (a * hotspot.x - b * hotspot.y), y - (b * hotspot.x + a * hotspot.y)];
238
+ }
239
+ /**
240
+ * Draws a cursor with its hotspot at (x, y) in output pixels. `scale` is output
241
+ * pixels per design unit; the body leans by `angle` about the hotspot. The drop shadow
242
+ * has a tight contact layer and a soft layer, offset straight down whatever the angle.
243
+ */
244
+ export function drawCursor(ctx, style, x, y, scale, alpha, angle = 0) {
245
+ const s = cursorShape(style);
246
+ if (!s || alpha <= 0.002 || scale <= 0)
247
+ return;
248
+ ctx.save();
249
+ ctx.globalAlpha = alpha;
250
+ ctx.setTransform(...cursorMatrix(s.hotspot, x, y, scale, angle));
251
+ ctx.fillStyle = '#ffffff';
252
+ ctx.shadowColor = 'rgba(0, 0, 0, 0.22)';
253
+ ctx.shadowBlur = 5 * scale;
254
+ ctx.shadowOffsetY = 1.6 * scale;
255
+ ctx.fill(s.outline);
256
+ ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
257
+ ctx.shadowBlur = 1.1 * scale;
258
+ ctx.shadowOffsetY = 0.45 * scale;
259
+ ctx.fill(s.outline);
260
+ ctx.shadowColor = 'transparent';
261
+ ctx.fillStyle = INK;
262
+ ctx.fill(s.body);
263
+ if (s.details) {
264
+ ctx.fillStyle = '#ffffff';
265
+ ctx.fill(s.details);
266
+ }
267
+ ctx.restore();
268
+ }