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,656 @@
1
+ import { createCanvas } from '@napi-rs/canvas';
2
+ import { luminance, parseColor, rgba } from "./color.js";
3
+ import { CAPTION_WIDTH, captionMeasure, drawCaption, layoutCaption, LINE_HEIGHT, } from "./captions.js";
4
+ import { drawCursor } from "./cursors/shapes.js";
5
+ import { adaptiveClickColor, drawClick, drawSpotlight, parseClickColor, } from "./effects.js";
6
+ import { smoothstep } from "./easing.js";
7
+ import { capLayouts, capsWidth, DARK_CAPS, drawKeycaps, LIGHT_CAPS, } from "./keycaps.js";
8
+ import { contentMap, IDENTITY } from "./stage.js";
9
+ import { BROWSER_BAR, drawAddressLens, drawBrowserBar } from "./browser.js";
10
+ const BAR_HEIGHT = 38;
11
+ function gcd(a, b) {
12
+ return b ? gcd(b, a % b) : a;
13
+ }
14
+ /**
15
+ * The content box is the largest box with the viewport aspect ratio inside the padded
16
+ * output. Its width snaps to whole pixels so that, when the ratio allows, both edges land
17
+ * on pixel boundaries.
18
+ */
19
+ export function computeLayout(options, viewport) {
20
+ const { width: w, height: h } = options.size;
21
+ const unit = options.unit;
22
+ const p = options.frame.padding;
23
+ const pad = p < 1 ? p * Math.min(w, h) : p * unit;
24
+ const chrome = options.frame.chrome;
25
+ const bar = (chrome === 'browser' ? BROWSER_BAR : chrome === 'window' ? BAR_HEIGHT : 0) * unit;
26
+ const aspect = viewport.width / viewport.height;
27
+ let cw = Math.max(16, Math.min(w - 2 * pad, (h - 2 * pad - bar) * aspect));
28
+ const vw = Math.round(viewport.width);
29
+ const vh = Math.round(viewport.height);
30
+ const step = vw / gcd(vw, vh);
31
+ if (Number.isInteger(step) && step <= 64 && cw >= step)
32
+ cw = Math.floor(cw / step) * step;
33
+ else
34
+ cw = Math.floor(cw);
35
+ const ch = cw / aspect;
36
+ const x = Math.round((w - cw) / 2);
37
+ const y = Math.round((h - ch - bar) / 2);
38
+ return {
39
+ size: options.size,
40
+ unit,
41
+ viewport,
42
+ window: { x, y, width: cw, height: ch + bar },
43
+ content: { x, y: y + bar, width: cw, height: ch },
44
+ bar,
45
+ radius: Math.min(options.frame.radius * unit, cw / 4),
46
+ padding: pad,
47
+ k: cw / viewport.width,
48
+ };
49
+ }
50
+ /**
51
+ * Width of the soft edge where the wallpaper covers a zoomed frame in the caption band.
52
+ */
53
+ const FEATHER = 56;
54
+ /**
55
+ * Shadow layers as fractions of the configured blur and offset, with their share of the
56
+ * shadow color's alpha: a tight contact shadow, two mid layers and a broad key shadow.
57
+ */
58
+ const SHADOW_LAYERS = [
59
+ { f: 0.03, a: 0.24 },
60
+ { f: 0.14, a: 0.2 },
61
+ { f: 0.4, a: 0.2 },
62
+ { f: 1, a: 0.34 },
63
+ ];
64
+ const CHROME = {
65
+ light: {
66
+ bar: '#f3f3f2',
67
+ barShade: '#ebebea',
68
+ separator: 'rgba(0, 0, 0, 0.09)',
69
+ title: '#5b5b5f',
70
+ dotBorder: 'rgba(0, 0, 0, 0.12)',
71
+ },
72
+ dark: {
73
+ bar: '#2b2b2e',
74
+ barShade: '#252528',
75
+ separator: 'rgba(0, 0, 0, 0.55)',
76
+ title: '#b4b4b9',
77
+ dotBorder: 'rgba(0, 0, 0, 0.25)',
78
+ },
79
+ };
80
+ const DOTS = ['#ff5f57', '#febc2e', '#28c840'];
81
+ function floatCanvas(width, height) {
82
+ const canvas = createCanvas(width, height);
83
+ canvas.getContext('2d', { colorType: 'float16' });
84
+ return canvas;
85
+ }
86
+ /**
87
+ * Draws the cursor of one frame. A style change swaps the shape at the midpoint of a
88
+ * small scale dip, so the cursor never shows two half-transparent shapes; a change to or
89
+ * from `none` fades.
90
+ */
91
+ function drawCursorFrame(ctx, cur, x, y, scale, weight) {
92
+ const alpha = cur.opacity * weight;
93
+ const mix = cur.styleMix;
94
+ if (mix >= 1 || cur.previousStyle === cur.style) {
95
+ drawCursor(ctx, cur.style, x, y, scale, alpha, cur.angle);
96
+ }
97
+ else if (cur.previousStyle === 'none' || cur.style === 'none') {
98
+ drawCursor(ctx, cur.previousStyle, x, y, scale, alpha * (1 - mix), cur.angle);
99
+ drawCursor(ctx, cur.style, x, y, scale, alpha * mix, cur.angle);
100
+ }
101
+ else {
102
+ const style = mix < 0.5 ? cur.previousStyle : cur.style;
103
+ drawCursor(ctx, style, x, y, scale * (1 - 0.1 * Math.sin(Math.PI * mix)), alpha, cur.angle);
104
+ }
105
+ }
106
+ /**
107
+ * Draws output frames. The background and the frame shadow are rendered once; the
108
+ * camera transform then moves the frame, while the background stays fixed.
109
+ */
110
+ export class Compositor {
111
+ canvas;
112
+ layout;
113
+ options;
114
+ ctx;
115
+ background;
116
+ base;
117
+ shadow;
118
+ click;
119
+ clickColors = new Map();
120
+ probe = createCanvas(4, 4);
121
+ accumulator = null;
122
+ cursorLayer = null;
123
+ sprite = null;
124
+ spriteKey = '';
125
+ band;
126
+ moving = null;
127
+ movingKey = '';
128
+ lastKey = '';
129
+ overlay = null;
130
+ overlayKey = '';
131
+ captionStyle;
132
+ captionLines = new Map();
133
+ keyThemes = new Map();
134
+ constructor(options, layout, background, band = null) {
135
+ this.options = options;
136
+ this.layout = layout;
137
+ this.background = background;
138
+ this.band = band;
139
+ const { width, height } = layout.size;
140
+ this.canvas = createCanvas(width, height);
141
+ this.ctx = this.canvas.getContext('2d');
142
+ this.shadow = options.frame.shadow ? this.renderShadow() : null;
143
+ this.base = createCanvas(width, height);
144
+ const b = this.base.getContext('2d');
145
+ b.drawImage(background, 0, 0);
146
+ if (this.shadow)
147
+ b.drawImage(this.shadow, 0, 0);
148
+ const click = options.cursor.click;
149
+ this.click = {
150
+ type: click.type,
151
+ color: parseClickColor(click.color),
152
+ radius: click.size,
153
+ duration: Math.max(1, click.duration),
154
+ };
155
+ const u = layout.unit;
156
+ const { width: W, height: H } = layout.size;
157
+ const textArea = band
158
+ ? {
159
+ x: band.window.x,
160
+ y: band.center - band.height / 3,
161
+ w: band.window.width * 0.7,
162
+ h: (band.height * 2) / 3,
163
+ }
164
+ : { x: 0, y: H * 0.85, w: W, h: H * 0.15 };
165
+ const dark = this.backgroundLuminance(textArea) < 0.4;
166
+ this.captionStyle = {
167
+ family: options.captions.fontFamily,
168
+ fontSize: options.captions.fontSize * u,
169
+ color: dark ? { r: 246, g: 244, b: 240, a: 0.97 } : { r: 23, g: 24, b: 28, a: 0.94 },
170
+ unit: u,
171
+ };
172
+ }
173
+ /**
174
+ * Mean luminance of the background in a rectangle.
175
+ */
176
+ backgroundLuminance(r) {
177
+ const probe = createCanvas(16, 4);
178
+ const p = probe.getContext('2d');
179
+ p.imageSmoothingEnabled = true;
180
+ p.imageSmoothingQuality = 'medium';
181
+ p.drawImage(this.background, r.x, r.y, Math.max(1, r.w), Math.max(1, r.h), 0, 0, 16, 4);
182
+ const d = p.getImageData(0, 0, 16, 4).data;
183
+ let sum = 0;
184
+ for (let i = 0; i < d.length; i += 4)
185
+ sum += luminance({ r: d[i], g: d[i + 1], b: d[i + 2], a: 1 });
186
+ return sum / (d.length / 4);
187
+ }
188
+ renderShadow() {
189
+ const { width, height } = this.layout.size;
190
+ const canvas = createCanvas(width, height);
191
+ const ctx = canvas.getContext('2d');
192
+ const shadow = this.options.frame.shadow;
193
+ if (!shadow)
194
+ return canvas;
195
+ const color = parseColor(shadow.color) ?? { r: 0, g: 0, b: 0, a: 0.5 };
196
+ const u = this.layout.unit;
197
+ const r = this.layout.window;
198
+ const away = width + height + shadow.blur * u * 4;
199
+ ctx.fillStyle = '#000000';
200
+ for (const layer of SHADOW_LAYERS) {
201
+ ctx.shadowColor = rgba(color, color.a * layer.a);
202
+ ctx.shadowBlur = shadow.blur * layer.f * u;
203
+ ctx.shadowOffsetX = away;
204
+ ctx.shadowOffsetY = shadow.offsetY * layer.f * u;
205
+ ctx.beginPath();
206
+ ctx.roundRect(r.x - away, r.y, r.width, r.height, this.layout.radius);
207
+ ctx.fill();
208
+ }
209
+ return canvas;
210
+ }
211
+ /**
212
+ * True when the color of a click still needs `prepareClick`.
213
+ */
214
+ needsClickColor(mark) {
215
+ return !this.click.color && !this.clickColors.has(mark);
216
+ }
217
+ /**
218
+ * Picks the adaptive click color from the page brightness under the click, in the
219
+ * frame captured when the button went down.
220
+ */
221
+ prepareClick(mark, image) {
222
+ let lum = 1;
223
+ if (image) {
224
+ const s = image.width / this.layout.viewport.width;
225
+ const half = 18 * s;
226
+ const p = this.probe.getContext('2d');
227
+ p.imageSmoothingEnabled = true;
228
+ p.drawImage(image, mark.x * s - half, mark.y * s - half, half * 2, half * 2, 0, 0, 4, 4);
229
+ const d = p.getImageData(0, 0, 4, 4).data;
230
+ lum = 0;
231
+ for (let i = 0; i < d.length; i += 4) {
232
+ lum += luminance({ r: d[i], g: d[i + 1], b: d[i + 2], a: 1 });
233
+ }
234
+ lum /= d.length / 4;
235
+ }
236
+ this.clickColors.set(mark, adaptiveClickColor(lum));
237
+ }
238
+ clickColor(mark) {
239
+ return this.click.color ?? this.clickColors.get(mark) ?? adaptiveClickColor(1);
240
+ }
241
+ /**
242
+ * Renders one frame into `canvas`. The stage transform (caption band) and the camera
243
+ * combine into one frame matrix; content points map with the matching content matrix.
244
+ */
245
+ draw(input) {
246
+ const ctx = this.ctx;
247
+ const L = this.layout;
248
+ const H = L.size.height;
249
+ const st = input.stage ?? IDENTITY;
250
+ const s = input.camera.scale;
251
+ const { ks, ex, ey, fa, fe, ff } = this.matrices(input);
252
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
253
+ ctx.imageSmoothingEnabled = true;
254
+ ctx.imageSmoothingQuality = 'high';
255
+ this.drawBase(ctx, fa, fe, ff);
256
+ ctx.setTransform(fa, 0, 0, fa, fe, ff);
257
+ if (L.bar > 0)
258
+ this.drawChrome(ctx, input.address);
259
+ ctx.save();
260
+ const c = L.content;
261
+ const rr = L.radius;
262
+ ctx.beginPath();
263
+ ctx.roundRect(c.x, c.y, c.width, c.height, L.bar > 0 ? [0, 0, rr, rr] : rr);
264
+ ctx.clip();
265
+ if (input.image) {
266
+ const k = (L.viewport.width / input.image.width) * ks;
267
+ ctx.setTransform(k, 0, 0, k, ex, ey);
268
+ ctx.drawImage(input.image, 0, 0);
269
+ }
270
+ else {
271
+ ctx.fillStyle = '#ffffff';
272
+ ctx.fill();
273
+ }
274
+ if (input.spotlights.length) {
275
+ ctx.setTransform(ks, 0, 0, ks, ex, ey);
276
+ const vp = { x: 0, y: 0, ...L.viewport };
277
+ for (const sp of input.spotlights)
278
+ drawSpotlight(ctx, sp.event, sp.amount, vp, L.unit);
279
+ }
280
+ for (const { mark, age } of input.clicks) {
281
+ const color = this.clickColor(mark);
282
+ const scale = s * st.scale * L.unit;
283
+ drawClick(ctx, this.click, color, ex + mark.x * ks, ey + mark.y * ks, age, scale);
284
+ }
285
+ const cur = input.cursor;
286
+ if (cur && cur.opacity > 0) {
287
+ const press = 1 - (1 - this.options.cursor.pressScale) * cur.press;
288
+ const scale = this.options.cursor.size * ks * press;
289
+ const trail = input.cursorTrail ?? [cur];
290
+ const points = trail.map((p) => ({ x: ex + p.x * ks, y: ey + p.y * ks }));
291
+ if (points.length > 1)
292
+ this.drawCursorTrail(ctx, cur, points, scale);
293
+ else
294
+ drawCursorFrame(ctx, cur, points[0].x, points[0].y, scale, 1);
295
+ }
296
+ ctx.restore();
297
+ ctx.setTransform(fa, 0, 0, fa, fe, ff);
298
+ this.drawEdges(ctx);
299
+ if (input.address && this.options.frame.chrome === 'browser') {
300
+ const dark = this.options.frame.chromeTheme === 'dark';
301
+ ctx.setTransform(fa, 0, 0, fa, fe, ff);
302
+ drawAddressLens(ctx, L.window, L.bar, L.unit, dark, this.barLeft(), input.address);
303
+ }
304
+ // The band cover, the captions and the keycaps are in output pixels.
305
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
306
+ if (input.band > 0 && this.band)
307
+ this.drawBandCover(ctx, input.band, fa, ff, H);
308
+ for (const cue of input.captions)
309
+ this.drawCaptionCue(ctx, cue, input.t);
310
+ for (const cue of input.keys)
311
+ this.drawKeys(ctx, cue, input.band, input.t);
312
+ }
313
+ /**
314
+ * The content matrix (CSS pixels to output: scale `ks`, offset `ex`, `ey`) and the frame
315
+ * matrix (rest layout to output: scale `fa`, offset `fe`, `ff`): plain affine maps of the
316
+ * camera state and the stage.
317
+ */
318
+ matrices(input) {
319
+ const L = this.layout;
320
+ const st = input.stage ?? IDENTITY;
321
+ const { ks, ex, ey } = contentMap(L, st, input.camera);
322
+ const camera = this.options.zoom.mode === 'camera';
323
+ const fa = camera ? st.scale * input.camera.scale : st.scale;
324
+ return {
325
+ ks,
326
+ ex,
327
+ ey,
328
+ fa,
329
+ fe: camera ? ex - fa * L.content.x : st.x,
330
+ ff: camera ? ey - fa * L.content.y : st.y,
331
+ };
332
+ }
333
+ /**
334
+ * Background and frame shadow. At rest this is one cached image; for a frame matrix
335
+ * that repeats (a still camera with the band open) it is cached on its second use.
336
+ */
337
+ drawBase(ctx, fa, fe, ff) {
338
+ const L = this.layout;
339
+ const { width: W, height: H } = L.size;
340
+ if (Math.abs(fa - 1) < 1e-9 && Math.abs(fe) < 1e-6 && Math.abs(ff) < 1e-6) {
341
+ ctx.drawImage(this.base, 0, 0);
342
+ this.lastKey = '';
343
+ return;
344
+ }
345
+ const w = L.window;
346
+ const r = L.radius * fa;
347
+ const left = w.x * fa + fe;
348
+ const top = w.y * fa + ff;
349
+ if (left <= -r && top <= -r && left + w.width * fa >= W + r && top + w.height * fa >= H + r) {
350
+ return;
351
+ }
352
+ const key = `${fa.toFixed(6)}:${fe.toFixed(3)}:${ff.toFixed(3)}`;
353
+ const target = key === this.lastKey ? (this.moving ??= createCanvas(W, H)) : null;
354
+ this.lastKey = key;
355
+ if (target && this.movingKey === key) {
356
+ ctx.drawImage(target, 0, 0);
357
+ return;
358
+ }
359
+ const out = target ? target.getContext('2d') : ctx;
360
+ out.setTransform(1, 0, 0, 1, 0, 0);
361
+ out.drawImage(this.background, 0, 0);
362
+ if (this.shadow) {
363
+ out.imageSmoothingQuality = 'medium';
364
+ out.setTransform(fa, 0, 0, fa, fe, ff);
365
+ out.drawImage(this.shadow, 0, 0);
366
+ out.imageSmoothingQuality = 'high';
367
+ out.setTransform(1, 0, 0, 1, 0, 0);
368
+ }
369
+ if (target) {
370
+ this.movingKey = key;
371
+ ctx.drawImage(target, 0, 0);
372
+ }
373
+ }
374
+ /**
375
+ * Redraws the wallpaper over the band where a zoomed frame overflows into it, with a
376
+ * feathered edge. At rest the frame never overflows, so its shadow stays visible.
377
+ */
378
+ drawBandCover(ctx, open, fa, ff, H) {
379
+ const band = this.band;
380
+ const L = this.layout;
381
+ const u = L.unit;
382
+ const feather = FEATHER * u;
383
+ const rest = band.window;
384
+ const bottom = band.side === 'bottom';
385
+ const edge = bottom
386
+ ? H + feather / 2 + (rest.y + rest.height - H - feather / 2) * open
387
+ : -feather / 2 + (rest.y + feather / 2) * open;
388
+ const frameEdge = bottom ? fa * (L.window.y + L.window.height) + ff : fa * L.window.y + ff;
389
+ const intrusion = bottom ? frameEdge - edge : edge - frameEdge;
390
+ const alpha = smoothstep(0, feather, intrusion);
391
+ if (alpha <= 0)
392
+ return;
393
+ const { width: W } = L.size;
394
+ const y0 = bottom ? Math.max(0, Math.floor(edge - feather / 2)) : 0;
395
+ const y1 = bottom ? H : Math.min(H, Math.ceil(edge + feather / 2));
396
+ if (y1 <= y0)
397
+ return;
398
+ const key = `${band.side}:${edge.toFixed(2)}`;
399
+ if (this.overlayKey !== key) {
400
+ this.overlay ??= createCanvas(W, H);
401
+ const o = this.overlay.getContext('2d');
402
+ o.setTransform(1, 0, 0, 1, 0, 0);
403
+ o.globalCompositeOperation = 'copy';
404
+ o.drawImage(this.background, 0, y0, W, y1 - y0, 0, y0, W, y1 - y0);
405
+ o.globalCompositeOperation = 'destination-in';
406
+ const g = o.createLinearGradient(0, edge - feather / 2, 0, edge + feather / 2);
407
+ for (let i = 0; i <= 16; i++) {
408
+ const v = i / 16;
409
+ const a = v * v * v * (v * (6 * v - 15) + 10);
410
+ g.addColorStop(v, `rgba(0, 0, 0, ${(bottom ? a : 1 - a).toFixed(4)})`);
411
+ }
412
+ o.fillStyle = g;
413
+ o.fillRect(0, y0, W, y1 - y0);
414
+ o.globalCompositeOperation = 'source-over';
415
+ this.overlayKey = key;
416
+ }
417
+ ctx.save();
418
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
419
+ ctx.globalAlpha = alpha;
420
+ ctx.drawImage(this.overlay, 0, y0, W, y1 - y0, 0, y0, W, y1 - y0);
421
+ ctx.restore();
422
+ }
423
+ captionLayout(cue) {
424
+ let line = this.captionLines.get(cue);
425
+ if (!line) {
426
+ const style = this.captionStyle;
427
+ const width = this.band ? this.band.window.width : this.layout.window.width;
428
+ line = layoutCaption(cue.text, captionMeasure(style.family), width * CAPTION_WIDTH, style.fontSize);
429
+ this.captionLines.set(cue, line);
430
+ }
431
+ return line;
432
+ }
433
+ drawCaptionCue(ctx, cue, t) {
434
+ const band = this.band;
435
+ if (!band)
436
+ return;
437
+ const line = this.captionLayout(cue);
438
+ const top = band.center - (line.fontSize * LINE_HEIGHT) / 2;
439
+ drawCaption(ctx, cue, line, this.captionStyle, band.window.x, top, t);
440
+ }
441
+ /**
442
+ * Keycaps sit centered in the padding at rest; with the caption band open on their side
443
+ * they move into the band, right-aligned with the frame.
444
+ */
445
+ keysBox(cue, open) {
446
+ const L = this.layout;
447
+ const u = L.unit;
448
+ const caps = capLayouts(cue.keys, u, this.options.keystrokes.labels);
449
+ const width = capsWidth(caps, u);
450
+ const h = 40 * u;
451
+ const top = this.options.keystrokes.position === 'top';
452
+ let x = (L.size.width - width) / 2;
453
+ let y = top
454
+ ? L.window.y - L.padding / 2 - h / 2
455
+ : L.window.y + L.window.height + L.padding / 2 - h / 2;
456
+ const band = this.band;
457
+ if (band && (band.side === 'top') === top && open > 0) {
458
+ x += (band.window.x + band.window.width - width - x) * open;
459
+ y += (band.center - h / 2 - y) * open;
460
+ }
461
+ return { caps, x, y, width, height: h };
462
+ }
463
+ needsKeyTheme(cue) {
464
+ return !this.keyThemes.has(cue);
465
+ }
466
+ /**
467
+ * Picks light or dark caps from what is behind them when they appear: the page when
468
+ * the zoomed frame covers their place, the wallpaper otherwise.
469
+ */
470
+ prepareKeys(cue, input) {
471
+ const L = this.layout;
472
+ const box = this.keysBox(cue, input.band);
473
+ const area = { x: box.x, y: box.y, w: box.width, h: box.height };
474
+ const { ks, ex, ey, fa, fe, ff } = this.matrices(input);
475
+ const cx = box.x + box.width / 2;
476
+ const cy = box.y + box.height / 2;
477
+ const c = L.content;
478
+ const inside = cx >= fa * c.x + fe &&
479
+ cx <= fa * (c.x + c.width) + fe &&
480
+ cy >= fa * c.y + ff &&
481
+ cy <= fa * (c.y + c.height) + ff;
482
+ const band = this.band;
483
+ const covered = band !== null &&
484
+ input.band > 0.5 &&
485
+ (band.side === 'bottom' ? cy > band.window.y + band.window.height : cy < band.window.y);
486
+ let lum;
487
+ if (inside && !covered && input.image) {
488
+ const s = input.image.width / L.viewport.width;
489
+ const p = this.probe.getContext('2d');
490
+ p.imageSmoothingEnabled = true;
491
+ p.drawImage(input.image, ((box.x - ex) / ks) * s, ((box.y - ey) / ks) * s, (box.width / ks) * s, (box.height / ks) * s, 0, 0, 4, 4);
492
+ const d = p.getImageData(0, 0, 4, 4).data;
493
+ lum = 0;
494
+ for (let i = 0; i < d.length; i += 4) {
495
+ lum += luminance({ r: d[i], g: d[i + 1], b: d[i + 2], a: 1 });
496
+ }
497
+ lum /= d.length / 4;
498
+ }
499
+ else {
500
+ lum = this.backgroundLuminance(area);
501
+ }
502
+ this.keyThemes.set(cue, lum < 0.4 ? LIGHT_CAPS : DARK_CAPS);
503
+ }
504
+ drawKeys(ctx, cue, open, t) {
505
+ const box = this.keysBox(cue, open);
506
+ const theme = this.keyThemes.get(cue) ??
507
+ (this.backgroundLuminance({ x: box.x, y: box.y, w: box.width, h: box.height }) < 0.4
508
+ ? LIGHT_CAPS
509
+ : DARK_CAPS);
510
+ drawKeycaps(ctx, cue, box.caps, theme, box.x, box.y, this.layout.unit, t);
511
+ }
512
+ /**
513
+ * Renders several sub-frame inputs across the shutter and averages them (camera
514
+ * motion blur). The average runs in a float16 canvas, so it adds no banding.
515
+ */
516
+ drawBlurred(inputs) {
517
+ if (inputs.length < 2) {
518
+ this.draw(inputs[0]);
519
+ return;
520
+ }
521
+ const { width, height } = this.layout.size;
522
+ this.accumulator ??= floatCanvas(width, height);
523
+ const acc = this.accumulator.getContext('2d');
524
+ inputs.forEach((input, k) => {
525
+ this.draw(input);
526
+ acc.globalAlpha = 1 / (k + 1);
527
+ acc.drawImage(this.canvas, 0, 0);
528
+ });
529
+ this.ctx.setTransform(1, 0, 0, 1, 0, 0);
530
+ this.ctx.drawImage(this.accumulator, 0, 0);
531
+ }
532
+ /**
533
+ * Cursor motion blur: the average of the cursor at its positions across the shutter.
534
+ * The cursor (fill, outline, shadow) is drawn once into a sprite, which is added with
535
+ * premultiplied weights 1/N into a float16 layer, so the smear keeps its brightness.
536
+ */
537
+ drawCursorTrail(ctx, cur, points, scale) {
538
+ const { width, height } = this.layout.size;
539
+ const m = Math.ceil(30 * scale);
540
+ const key = [
541
+ cur.style,
542
+ cur.previousStyle,
543
+ cur.styleMix,
544
+ cur.press,
545
+ cur.opacity,
546
+ cur.angle,
547
+ scale,
548
+ ].join();
549
+ if (!this.sprite || this.sprite.width < 2 * m)
550
+ this.sprite = createCanvas(2 * m, 2 * m);
551
+ if (key !== this.spriteKey) {
552
+ const sprite = this.sprite.getContext('2d');
553
+ sprite.setTransform(1, 0, 0, 1, 0, 0);
554
+ sprite.clearRect(0, 0, this.sprite.width, this.sprite.height);
555
+ drawCursorFrame(sprite, cur, m, m, scale, 1);
556
+ this.spriteKey = key;
557
+ }
558
+ const xs = points.map((p) => p.x);
559
+ const ys = points.map((p) => p.y);
560
+ const x0 = Math.max(0, Math.floor(Math.min(...xs) - m));
561
+ const y0 = Math.max(0, Math.floor(Math.min(...ys) - m));
562
+ const x1 = Math.min(width, Math.ceil(Math.max(...xs) + m));
563
+ const y1 = Math.min(height, Math.ceil(Math.max(...ys) + m));
564
+ if (x1 <= x0 || y1 <= y0)
565
+ return;
566
+ const w = x1 - x0;
567
+ const h = y1 - y0;
568
+ if (!this.cursorLayer || this.cursorLayer.width < w || this.cursorLayer.height < h) {
569
+ this.cursorLayer = floatCanvas(Math.max(w, this.cursorLayer?.width ?? 0), Math.max(h, this.cursorLayer?.height ?? 0));
570
+ }
571
+ const layer = this.cursorLayer.getContext('2d');
572
+ layer.imageSmoothingEnabled = true;
573
+ layer.imageSmoothingQuality = 'low';
574
+ layer.setTransform(1, 0, 0, 1, 0, 0);
575
+ layer.clearRect(0, 0, w, h);
576
+ layer.globalCompositeOperation = 'lighter';
577
+ layer.globalAlpha = 1 / points.length;
578
+ for (const p of points)
579
+ layer.drawImage(this.sprite, p.x - m - x0, p.y - m - y0);
580
+ layer.globalAlpha = 1;
581
+ layer.globalCompositeOperation = 'source-over';
582
+ ctx.save();
583
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
584
+ ctx.drawImage(this.cursorLayer, 0, 0, w, h, x0, y0, w, h);
585
+ ctx.restore();
586
+ }
587
+ /**
588
+ * First free x on the bar after the window buttons.
589
+ */
590
+ barLeft() {
591
+ return this.layout.window.x + 67.6 * this.layout.unit;
592
+ }
593
+ drawChrome(ctx, address) {
594
+ const L = this.layout;
595
+ const u = L.unit;
596
+ const w = L.window;
597
+ const dark = this.options.frame.chromeTheme === 'dark';
598
+ const theme = CHROME[dark ? 'dark' : 'light'];
599
+ const g = ctx.createLinearGradient(0, w.y, 0, w.y + L.bar);
600
+ g.addColorStop(0, theme.bar);
601
+ g.addColorStop(1, theme.barShade);
602
+ ctx.fillStyle = g;
603
+ ctx.beginPath();
604
+ ctx.roundRect(w.x, w.y, w.width, L.bar + L.radius, [L.radius, L.radius, 0, 0]);
605
+ ctx.fill();
606
+ ctx.fillStyle = theme.separator;
607
+ ctx.fillRect(w.x, w.y + L.bar - u, w.width, u);
608
+ const cy = w.y + L.bar / 2;
609
+ const dot = 6.6 * u;
610
+ DOTS.forEach((color, i) => {
611
+ ctx.beginPath();
612
+ ctx.arc(w.x + 21 * u + i * 20 * u, cy, dot, 0, Math.PI * 2);
613
+ ctx.fillStyle = color;
614
+ ctx.fill();
615
+ ctx.lineWidth = 0.8 * u;
616
+ ctx.strokeStyle = theme.dotBorder;
617
+ ctx.stroke();
618
+ });
619
+ if (this.options.frame.chrome === 'browser') {
620
+ const none = { host: '', path: '', focus: 0 };
621
+ drawBrowserBar(ctx, w, L.bar, u, dark, this.barLeft(), address ?? none);
622
+ return;
623
+ }
624
+ const title = this.options.frame.title;
625
+ if (title) {
626
+ ctx.font = `500 ${14 * u}px Inter, sans-serif`;
627
+ ctx.fillStyle = theme.title;
628
+ ctx.textAlign = 'center';
629
+ ctx.textBaseline = 'middle';
630
+ const max = w.width - 2 * 90 * u;
631
+ let text = title;
632
+ while (text.length > 1 && ctx.measureText(text).width > max)
633
+ text = text.slice(0, -2) + '…';
634
+ ctx.fillText(text, w.x + w.width / 2, cy + 0.5 * u);
635
+ }
636
+ }
637
+ /**
638
+ * Hairlines that define the window edge: dark outside for light backgrounds, a faint
639
+ * light rim inside for dark content.
640
+ */
641
+ drawEdges(ctx) {
642
+ const L = this.layout;
643
+ const u = L.unit;
644
+ const w = L.window;
645
+ ctx.lineWidth = u;
646
+ ctx.beginPath();
647
+ ctx.roundRect(w.x - u / 2, w.y - u / 2, w.width + u, w.height + u, L.radius + u / 2);
648
+ ctx.strokeStyle = 'rgba(0, 0, 0, 0.12)';
649
+ ctx.stroke();
650
+ ctx.beginPath();
651
+ ctx.roundRect(w.x + u / 2, w.y + u / 2, w.width - u, w.height - u, Math.max(0, L.radius - u / 2));
652
+ ctx.strokeStyle = 'rgba(255, 255, 255, 0.09)';
653
+ ctx.stroke();
654
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
655
+ }
656
+ }