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,34 @@
1
+ import type { CursorSample, Point, TimelineEvent } from '../types.ts';
2
+ /**
3
+ * Epoch time in milliseconds with sub-millisecond precision. Same time base as the frame
4
+ * timestamps of the screencast.
5
+ */
6
+ export declare function epochNow(): number;
7
+ export declare function sleep(ms: number): Promise<void>;
8
+ /**
9
+ * Record time: milliseconds since the capture started.
10
+ */
11
+ export declare class Clock {
12
+ origin: number;
13
+ started: boolean;
14
+ start(): void;
15
+ now(): number;
16
+ }
17
+ /**
18
+ * A source of record time in milliseconds.
19
+ */
20
+ export interface RecordClock {
21
+ now(): number;
22
+ }
23
+ /**
24
+ * The cursor samples and timeline events of a recording.
25
+ */
26
+ export declare class Timeline {
27
+ readonly clock: RecordClock;
28
+ readonly cursor: CursorSample[];
29
+ readonly events: TimelineEvent[];
30
+ constructor(clock: RecordClock);
31
+ now(): number;
32
+ sample(point: Point, t?: number): void;
33
+ push(event: TimelineEvent): void;
34
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Epoch time in milliseconds with sub-millisecond precision. Same time base as the frame
3
+ * timestamps of the screencast.
4
+ */
5
+ export function epochNow() {
6
+ return performance.timeOrigin + performance.now();
7
+ }
8
+ export function sleep(ms) {
9
+ return ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
10
+ }
11
+ /**
12
+ * Record time: milliseconds since the capture started.
13
+ */
14
+ export class Clock {
15
+ origin = 0;
16
+ started = false;
17
+ start() {
18
+ this.origin = epochNow();
19
+ this.started = true;
20
+ }
21
+ now() {
22
+ return this.started ? epochNow() - this.origin : 0;
23
+ }
24
+ }
25
+ /**
26
+ * The cursor samples and timeline events of a recording.
27
+ */
28
+ export class Timeline {
29
+ clock;
30
+ cursor = [];
31
+ events = [];
32
+ constructor(clock) {
33
+ this.clock = clock;
34
+ }
35
+ now() {
36
+ return this.clock.now();
37
+ }
38
+ sample(point, t = this.clock.now()) {
39
+ this.cursor.push({ t, x: point.x, y: point.y });
40
+ }
41
+ push(event) {
42
+ this.events.push(event);
43
+ }
44
+ }
@@ -0,0 +1,12 @@
1
+ import { type Canvas } from '@napi-rs/canvas';
2
+ import type { BackgroundOptions, Size } from '../types.ts';
3
+ export declare const BACKGROUND_PRESETS: string[];
4
+ /**
5
+ * Adds monochrome film grain with a triangular distribution of +-`amount` levels.
6
+ */
7
+ export declare function addGrain(canvas: Canvas, amount: number, seed?: number): void;
8
+ /**
9
+ * Renders the background once into an opaque canvas of the output size.
10
+ * `unit` scales pixel values (image blur) from a 1080-pixel output.
11
+ */
12
+ export declare function createBackground(options: BackgroundOptions, size: Size, unit: number): Promise<Canvas>;
@@ -0,0 +1,209 @@
1
+ import path from 'node:path';
2
+ import { createCanvas, loadImage } from '@napi-rs/canvas';
3
+ import { mixOklab, parseColor, rgba } from "./color.js";
4
+ const PRESETS = {
5
+ paper: {
6
+ stops: ['#eee8dd', '#e2d9cb'],
7
+ angle: 160,
8
+ lights: [{ x: 0.18, y: -0.12, r: 0.95, color: '#fbf8f2', alpha: 0.9 }],
9
+ vignette: 0.09,
10
+ grain: 3,
11
+ },
12
+ fog: {
13
+ stops: ['#e6e9ed', '#d3d8df'],
14
+ angle: 170,
15
+ lights: [{ x: 0.3, y: -0.15, r: 0.95, color: '#f7f9fb', alpha: 0.85 }],
16
+ vignette: 0.1,
17
+ grain: 3,
18
+ },
19
+ sage: {
20
+ stops: ['#c7cdbd', '#a3ad9b'],
21
+ angle: 150,
22
+ lights: [{ x: 0.2, y: -0.05, r: 0.85, color: '#e3e7da', alpha: 0.75 }],
23
+ vignette: 0.14,
24
+ grain: 3,
25
+ },
26
+ dune: {
27
+ stops: ['#ddcbb1', '#c1a680'],
28
+ angle: 145,
29
+ lights: [{ x: 0.22, y: -0.08, r: 0.9, color: '#f2e8d6', alpha: 0.75 }],
30
+ vignette: 0.14,
31
+ grain: 3,
32
+ },
33
+ clay: {
34
+ stops: ['#c99e88', '#a06d58'],
35
+ angle: 150,
36
+ lights: [{ x: 0.2, y: -0.05, r: 0.85, color: '#e8cbb8', alpha: 0.65 }],
37
+ vignette: 0.18,
38
+ grain: 3,
39
+ },
40
+ dusk: {
41
+ stops: ['#3c475e', '#5d5a6c', '#9a7a6c'],
42
+ angle: 125,
43
+ lights: [
44
+ { x: 0.12, y: -0.05, r: 0.75, color: '#5a6782', alpha: 0.55 },
45
+ { x: 0.98, y: 1.05, r: 0.6, color: '#c3937b', alpha: 0.35 },
46
+ ],
47
+ vignette: 0.22,
48
+ grain: 3.5,
49
+ },
50
+ harbor: {
51
+ stops: ['#2c4b52', '#1a2f35'],
52
+ angle: 160,
53
+ lights: [{ x: 0.25, y: -0.12, r: 0.95, color: '#4a747d', alpha: 0.6 }],
54
+ vignette: 0.25,
55
+ grain: 3.5,
56
+ },
57
+ graphite: {
58
+ stops: ['#35373c', '#1f2024'],
59
+ angle: 170,
60
+ lights: [{ x: 0.35, y: -0.18, r: 0.95, color: '#50535a', alpha: 0.7 }],
61
+ vignette: 0.3,
62
+ grain: 3.5,
63
+ },
64
+ ink: {
65
+ stops: ['#161c2a', '#0b0f18'],
66
+ angle: 165,
67
+ lights: [
68
+ { x: 0.2, y: -0.12, r: 0.9, color: '#28344d', alpha: 0.75 },
69
+ { x: 0.96, y: 1.08, r: 0.55, color: '#1c3b45', alpha: 0.4 },
70
+ ],
71
+ vignette: 0.3,
72
+ grain: 3.5,
73
+ },
74
+ ember: {
75
+ stops: ['#2b201c', '#16100e'],
76
+ angle: 160,
77
+ lights: [{ x: 0.82, y: -0.12, r: 0.9, color: '#5e3d2e', alpha: 0.65 }],
78
+ vignette: 0.3,
79
+ grain: 3.5,
80
+ },
81
+ };
82
+ export const BACKGROUND_PRESETS = Object.keys(PRESETS);
83
+ /**
84
+ * Linear gradient along a CSS angle (0deg points up, 90deg right), with stops mixed
85
+ * in OKLab.
86
+ */
87
+ function fillLinear(ctx, size, colors, angle) {
88
+ const { width: w, height: h } = size;
89
+ const a = (angle * Math.PI) / 180;
90
+ const dx = Math.sin(a);
91
+ const dy = -Math.cos(a);
92
+ const half = (Math.abs(w * dx) + Math.abs(h * dy)) / 2;
93
+ const g = ctx.createLinearGradient(w / 2 - dx * half, h / 2 - dy * half, w / 2 + dx * half, h / 2 + dy * half);
94
+ const parsed = colors.map((c) => parseColor(c));
95
+ if (parsed.some((c) => !c) || colors.length < 2) {
96
+ colors.forEach((c, i) => g.addColorStop(colors.length > 1 ? i / (colors.length - 1) : 0, c));
97
+ }
98
+ else {
99
+ const steps = 24;
100
+ for (let i = 0; i <= steps; i++) {
101
+ const p = (i / steps) * (colors.length - 1);
102
+ const k = Math.min(colors.length - 2, Math.floor(p));
103
+ g.addColorStop(i / steps, rgba(mixOklab(parsed[k], parsed[k + 1], p - k)));
104
+ }
105
+ }
106
+ ctx.fillStyle = g;
107
+ ctx.fillRect(0, 0, w, h);
108
+ }
109
+ /**
110
+ * A soft light: a radial glow with a smooth, compact falloff (no visible edge).
111
+ */
112
+ function fillLight(ctx, size, light) {
113
+ const color = parseColor(light.color);
114
+ if (!color)
115
+ return;
116
+ const diag = Math.hypot(size.width, size.height);
117
+ const x = light.x * size.width;
118
+ const y = light.y * size.height;
119
+ const g = ctx.createRadialGradient(x, y, 0, x, y, light.r * diag);
120
+ for (let i = 0; i <= 16; i++) {
121
+ const u = i / 16;
122
+ g.addColorStop(u, rgba(color, light.alpha * (1 - u * u) ** 3));
123
+ }
124
+ ctx.fillStyle = g;
125
+ ctx.fillRect(0, 0, size.width, size.height);
126
+ }
127
+ function fillVignette(ctx, size, strength) {
128
+ const diag = Math.hypot(size.width, size.height);
129
+ const x = size.width / 2;
130
+ const y = size.height * 0.42;
131
+ const g = ctx.createRadialGradient(x, y, diag * 0.25, x, y, diag * 0.62);
132
+ for (let i = 0; i <= 12; i++) {
133
+ const u = i / 12;
134
+ g.addColorStop(u, `rgba(0, 0, 0, ${(strength * u * u * (3 - 2 * u)).toFixed(4)})`);
135
+ }
136
+ ctx.fillStyle = g;
137
+ ctx.fillRect(0, 0, size.width, size.height);
138
+ }
139
+ /**
140
+ * Adds monochrome film grain with a triangular distribution of +-`amount` levels.
141
+ */
142
+ export function addGrain(canvas, amount, seed = 1) {
143
+ if (amount <= 0)
144
+ return;
145
+ const ctx = canvas.getContext('2d');
146
+ const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
147
+ const d = image.data;
148
+ let s = seed >>> 0 || 1;
149
+ const next = () => {
150
+ s ^= s << 13;
151
+ s ^= s >>> 17;
152
+ s ^= s << 5;
153
+ return (s >>> 0) / 4294967296;
154
+ };
155
+ for (let i = 0; i < d.length; i += 4) {
156
+ const n = (next() + next() - 1) * amount;
157
+ d[i] += n;
158
+ d[i + 1] += n;
159
+ d[i + 2] += n;
160
+ }
161
+ ctx.putImageData(image, 0, 0);
162
+ }
163
+ async function drawImageBackground(ctx, size, file, blur) {
164
+ const image = await loadImage(path.resolve(file));
165
+ const { width: w, height: h } = size;
166
+ const over = blur * 2;
167
+ const scale = Math.max((w + over * 2) / image.width, (h + over * 2) / image.height);
168
+ const dw = image.width * scale;
169
+ const dh = image.height * scale;
170
+ ctx.imageSmoothingEnabled = true;
171
+ ctx.imageSmoothingQuality = 'high';
172
+ if (blur > 0)
173
+ ctx.filter = `blur(${blur}px)`;
174
+ ctx.drawImage(image, (w - dw) / 2, (h - dh) / 2, dw, dh);
175
+ ctx.filter = 'none';
176
+ }
177
+ /**
178
+ * Renders the background once into an opaque canvas of the output size.
179
+ * `unit` scales pixel values (image blur) from a 1080-pixel output.
180
+ */
181
+ export async function createBackground(options, size, unit) {
182
+ const canvas = createCanvas(size.width, size.height);
183
+ const ctx = canvas.getContext('2d');
184
+ ctx.fillStyle = '#000000';
185
+ ctx.fillRect(0, 0, size.width, size.height);
186
+ if (options.type === 'solid') {
187
+ ctx.fillStyle = options.color;
188
+ ctx.fillRect(0, 0, size.width, size.height);
189
+ }
190
+ else if (options.type === 'gradient') {
191
+ fillLinear(ctx, size, options.colors.length ? options.colors : ['#000000'], options.angle ?? 135);
192
+ addGrain(canvas, 2.5);
193
+ }
194
+ else if (options.type === 'image') {
195
+ await drawImageBackground(ctx, size, options.path, (options.blur ?? 0) * unit);
196
+ }
197
+ else {
198
+ const preset = PRESETS[options.name];
199
+ if (!preset) {
200
+ throw new Error(`Unknown background preset "${options.name}". Presets: ${BACKGROUND_PRESETS.join(', ')}.`);
201
+ }
202
+ fillLinear(ctx, size, preset.stops, preset.angle);
203
+ for (const light of preset.lights)
204
+ fillLight(ctx, size, light);
205
+ fillVignette(ctx, size, preset.vignette);
206
+ addGrain(canvas, preset.grain);
207
+ }
208
+ return canvas;
209
+ }
@@ -0,0 +1,46 @@
1
+ import type { SKRSContext2D } from '@napi-rs/canvas';
2
+ import type { Rect, UrlEvent } from '../types.ts';
3
+ import type { TimedEvent } from './timeline.ts';
4
+ /**
5
+ * Height of the browser toolbar, in units.
6
+ */
7
+ export declare const BROWSER_BAR = 52;
8
+ /**
9
+ * What the address bar shows at one time. The host has the full tone and the rest of the
10
+ * address a lower tone. `focus` is 0 to 1: the field is larger while a URL is typed in it
11
+ * (with a `caret`) or while the script shows the URL.
12
+ */
13
+ export interface Address {
14
+ host: string;
15
+ path: string;
16
+ focus: number;
17
+ caret?: boolean;
18
+ }
19
+ /**
20
+ * Splits a URL into the host and the rest, without the scheme. `origin` replaces the
21
+ * origin of the URL; it can have a path, as in `jupyterlite.github.io/demo`.
22
+ */
23
+ export declare function splitAddress(url: string, origin?: string): {
24
+ host: string;
25
+ path: string;
26
+ };
27
+ /**
28
+ * The address bar at output time `t`: the latest address of the page, as typed text while
29
+ * a `cast.goto` types it. `urls` are in output time order; `start` is the address when the
30
+ * session has no address events.
31
+ */
32
+ export declare function addressAt(urls: TimedEvent<UrlEvent>[], t: number, options: {
33
+ address: boolean | string;
34
+ origin: string;
35
+ }, start?: string): Address;
36
+ /**
37
+ * Draws the controls of the browser toolbar on the bar of `window`: back, forward and
38
+ * reload, and the address field in the middle. The caller draws the bar and the window
39
+ * buttons. `left` is the first free x after the window buttons.
40
+ */
41
+ export declare function drawBrowserBar(ctx: SKRSContext2D, window: Rect, bar: number, unit: number, dark: boolean, left: number, address: Address): void;
42
+ /**
43
+ * Draws the address field larger, above the toolbar, while a URL is typed in it. Call it
44
+ * after the page content: the larger field covers the edge of the bar.
45
+ */
46
+ export declare function drawAddressLens(ctx: SKRSContext2D, window: Rect, bar: number, unit: number, dark: boolean, left: number, address: Address): void;
@@ -0,0 +1,223 @@
1
+ import { smoothstep } from "./easing.js";
2
+ /**
3
+ * Height of the browser toolbar, in units.
4
+ */
5
+ export const BROWSER_BAR = 52;
6
+ /**
7
+ * Time the typed address stays in the focused field before the page opens, and the time
8
+ * the focus ring takes to appear, in milliseconds.
9
+ */
10
+ const ENTER = 380;
11
+ const FOCUS = 260;
12
+ /**
13
+ * Splits a URL into the host and the rest, without the scheme. `origin` replaces the
14
+ * origin of the URL; it can have a path, as in `jupyterlite.github.io/demo`.
15
+ */
16
+ export function splitAddress(url, origin = '') {
17
+ let host = url;
18
+ let path = '';
19
+ // Text such as `example.org/docs` has no scheme: read it as a web address.
20
+ const plain = !/^[a-z][a-z0-9+.-]*:\/\//i.test(url) && !/^about:|\s/.test(url);
21
+ try {
22
+ const parsed = new URL(plain ? `https://${url}` : url);
23
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
24
+ host = parsed.host;
25
+ path = `${parsed.pathname}${parsed.search}${parsed.hash}`;
26
+ }
27
+ }
28
+ catch {
29
+ // Not a URL: show the text as it is.
30
+ }
31
+ const base = origin.replace(/^https?:\/\//, '').replace(/\/+$/, '');
32
+ if (base) {
33
+ const slash = base.indexOf('/');
34
+ host = slash < 0 ? base : base.slice(0, slash);
35
+ path = `${slash < 0 ? '' : base.slice(slash)}${path}`;
36
+ }
37
+ return { host, path: path === '/' ? '' : path };
38
+ }
39
+ /**
40
+ * The address bar at output time `t`: the latest address of the page, as typed text while
41
+ * a `cast.goto` types it. `urls` are in output time order; `start` is the address when the
42
+ * session has no address events.
43
+ */
44
+ export function addressAt(urls, t, options, start = '') {
45
+ if (options.address === false)
46
+ return { host: '', path: '', focus: 0 };
47
+ if (typeof options.address === 'string')
48
+ return { ...splitAddress(options.address), focus: 0 };
49
+ const event = urls.findLast((e) => e.out <= t);
50
+ const full = splitAddress(event?.url ?? start, options.origin);
51
+ const typing = event?.typing ?? 0;
52
+ const show = event?.show ?? 0;
53
+ const age = event ? t - event.out : 0;
54
+ if (show > 0 && age < show) {
55
+ return {
56
+ ...full,
57
+ focus: Math.min(smoothstep(0, FOCUS, age), smoothstep(show, show - FOCUS, age)),
58
+ };
59
+ }
60
+ if (typing <= 0 || age >= typing + ENTER)
61
+ return { ...full, focus: 0 };
62
+ const length = full.host.length + full.path.length;
63
+ const shown = Math.min(length, Math.ceil((age / typing) * length));
64
+ return {
65
+ host: full.host.slice(0, shown),
66
+ path: full.path.slice(0, Math.max(0, shown - full.host.length)),
67
+ focus: smoothstep(0, FOCUS, age),
68
+ caret: true,
69
+ };
70
+ }
71
+ const THEMES = {
72
+ light: {
73
+ icon: '#6a6a70',
74
+ field: 'rgba(0, 0, 0, 0.055)',
75
+ fieldFocus: '#ffffff',
76
+ host: '#1d1d21',
77
+ path: '#77777d',
78
+ ring: '59, 130, 246',
79
+ },
80
+ dark: {
81
+ icon: '#a3a3aa',
82
+ field: 'rgba(255, 255, 255, 0.075)',
83
+ fieldFocus: '#1a1a1d',
84
+ host: '#ededf1',
85
+ path: '#9a9aa2',
86
+ ring: '96, 165, 250',
87
+ },
88
+ };
89
+ /**
90
+ * How much larger the address field is while a URL is typed in it. The text must be easy
91
+ * to read in a video that shows the whole window.
92
+ */
93
+ const LENS = 1.55;
94
+ /**
95
+ * Place of the address field on the bar: in the middle of the window, clear of the
96
+ * buttons on the left. `left` is the first free x after the window buttons.
97
+ */
98
+ function fieldBox(window, bar, unit, left) {
99
+ const height = 34 * unit;
100
+ const controls = left + 110 * unit - window.x;
101
+ const width = Math.max(40 * unit, Math.min(760 * unit, window.width * 0.56, window.width - 2 * controls));
102
+ return {
103
+ x: window.x + (window.width - width) / 2,
104
+ y: window.y + (bar - height) / 2,
105
+ width,
106
+ height,
107
+ };
108
+ }
109
+ /**
110
+ * Draws the address field with its text. With `focus`, the field has the focus colors and
111
+ * a ring, and a caret after the text while a URL is typed.
112
+ */
113
+ function drawField(ctx, box, unit, theme, address, focus) {
114
+ const u = unit;
115
+ const cy = box.y + box.height / 2;
116
+ ctx.beginPath();
117
+ ctx.roundRect(box.x, box.y, box.width, box.height, box.height / 2);
118
+ if (focus > 0) {
119
+ ctx.fillStyle = theme.fieldFocus;
120
+ ctx.fill();
121
+ ctx.lineWidth = 1.4 * u;
122
+ ctx.strokeStyle = `rgba(${theme.ring}, 0.95)`;
123
+ ctx.stroke();
124
+ }
125
+ else {
126
+ ctx.fillStyle = theme.field;
127
+ ctx.fill();
128
+ }
129
+ ctx.font = `500 ${15 * u}px Inter, sans-serif`;
130
+ ctx.textAlign = 'left';
131
+ ctx.textBaseline = 'middle';
132
+ const pad = 17 * u;
133
+ const room = box.width - 2 * pad;
134
+ let { host, path } = address;
135
+ const measure = (text) => ctx.measureText(text).width;
136
+ while (path.length > 1 && measure(`${host}${path}`) > room)
137
+ path = `${path.slice(0, -2)}…`;
138
+ while (host.length > 1 && measure(`${host}${path}`) > room)
139
+ host = `${host.slice(0, -2)}…`;
140
+ const ty = cy + 0.5 * u;
141
+ ctx.fillStyle = theme.host;
142
+ ctx.fillText(host, box.x + pad, ty);
143
+ ctx.fillStyle = theme.path;
144
+ ctx.fillText(path, box.x + pad + measure(host), ty);
145
+ if (focus > 0 && address.caret) {
146
+ ctx.fillStyle = theme.host;
147
+ const caret = box.x + pad + measure(`${host}${path}`) + 1.5 * u;
148
+ ctx.fillRect(caret, cy - 9.5 * u, 1.5 * u, 19 * u);
149
+ }
150
+ }
151
+ /**
152
+ * Draws the controls of the browser toolbar on the bar of `window`: back, forward and
153
+ * reload, and the address field in the middle. The caller draws the bar and the window
154
+ * buttons. `left` is the first free x after the window buttons.
155
+ */
156
+ export function drawBrowserBar(ctx, window, bar, unit, dark, left, address) {
157
+ const u = unit;
158
+ const theme = THEMES[dark ? 'dark' : 'light'];
159
+ const cy = window.y + bar / 2;
160
+ ctx.save();
161
+ ctx.lineWidth = 1.7 * u;
162
+ ctx.lineCap = 'round';
163
+ ctx.lineJoin = 'round';
164
+ ctx.strokeStyle = theme.icon;
165
+ const chevron = (x, direction) => {
166
+ ctx.beginPath();
167
+ ctx.moveTo(x + direction * 3 * u, cy - 5.5 * u);
168
+ ctx.lineTo(x - direction * 3 * u, cy);
169
+ ctx.lineTo(x + direction * 3 * u, cy + 5.5 * u);
170
+ ctx.stroke();
171
+ };
172
+ const x0 = left + 22 * u;
173
+ chevron(x0, 1);
174
+ ctx.globalAlpha = 0.4;
175
+ chevron(x0 + 30 * u, -1);
176
+ ctx.globalAlpha = 1;
177
+ // Reload: an arc that is open at the top right, and ends in the corner of an arrow head.
178
+ const rx = x0 + 62 * u;
179
+ const r = 6.2 * u;
180
+ const corner = { x: rx + 1.22 * r, y: cy - 0.22 * r };
181
+ const head = 0.66 * r;
182
+ ctx.beginPath();
183
+ ctx.arc(rx, cy, r, 0.11 * Math.PI, 1.75 * Math.PI);
184
+ ctx.lineTo(corner.x, corner.y);
185
+ ctx.stroke();
186
+ ctx.beginPath();
187
+ ctx.moveTo(corner.x, corner.y - head);
188
+ ctx.lineTo(corner.x, corner.y);
189
+ ctx.lineTo(corner.x - head, corner.y);
190
+ ctx.stroke();
191
+ drawField(ctx, fieldBox(window, bar, u, left), u, theme, address, 0);
192
+ ctx.restore();
193
+ }
194
+ /**
195
+ * Draws the address field larger, above the toolbar, while a URL is typed in it. Call it
196
+ * after the page content: the larger field covers the edge of the bar.
197
+ */
198
+ export function drawAddressLens(ctx, window, bar, unit, dark, left, address) {
199
+ if (address.focus <= 0)
200
+ return;
201
+ const box = fieldBox(window, bar, unit, left);
202
+ // The larger field must stay clear of the buttons on the left.
203
+ const room = window.width - 2 * (left + 118 * unit - window.x);
204
+ const width = Math.min(box.width, room / LENS);
205
+ box.x += (box.width - width) / 2;
206
+ box.width = width;
207
+ const grow = 1 - (1 - address.focus) ** 3;
208
+ const scale = 1 + (LENS - 1) * grow;
209
+ ctx.save();
210
+ ctx.translate(box.x + box.width / 2, box.y + box.height / 2);
211
+ ctx.scale(scale, scale);
212
+ ctx.translate(-box.x - box.width / 2, -box.y - box.height / 2);
213
+ ctx.shadowColor = `rgba(0, 0, 0, ${(dark ? 0.5 : 0.22) * grow})`;
214
+ ctx.shadowBlur = 22 * unit;
215
+ ctx.shadowOffsetY = 6 * unit;
216
+ ctx.beginPath();
217
+ ctx.roundRect(box.x, box.y, box.width, box.height, box.height / 2);
218
+ ctx.fillStyle = THEMES[dark ? 'dark' : 'light'].fieldFocus;
219
+ ctx.fill();
220
+ ctx.shadowColor = 'transparent';
221
+ drawField(ctx, box, unit, THEMES[dark ? 'dark' : 'light'], address, 1);
222
+ ctx.restore();
223
+ }
@@ -0,0 +1,119 @@
1
+ import type { FollowMode, Size, ZoomEvent } from '../types.ts';
2
+ import type { ResolvedAutoZoom } from './defaults.ts';
3
+ import type { TimedEvent } from './timeline.ts';
4
+ /**
5
+ * Camera state in viewport CSS pixels: `scale` 1 shows the full viewport; the visible
6
+ * rectangle has size viewport / scale and is centered on (cx, cy).
7
+ */
8
+ export interface CameraState {
9
+ scale: number;
10
+ cx: number;
11
+ cy: number;
12
+ }
13
+ export interface CameraTrack {
14
+ scale: Float64Array;
15
+ cx: Float64Array;
16
+ cy: Float64Array;
17
+ }
18
+ export interface CameraOptions {
19
+ maxScale: number;
20
+ duration: number;
21
+ easing: ZoomEvent['easing'];
22
+ /**
23
+ * Follow mode for shots that do not set one.
24
+ */
25
+ follow: FollowMode;
26
+ /**
27
+ * Multiplies the follow spring frequency.
28
+ */
29
+ followSpeed?: number;
30
+ /**
31
+ * Named zoom levels for events with `level`.
32
+ */
33
+ levels?: Record<string, number>;
34
+ /**
35
+ * Size of the region a fit zoom fills, relative to the rest frame, at an output time:
36
+ * larger than 1 in camera mode, where the page may use the whole output.
37
+ */
38
+ fit?: (t: number) => FitRegion;
39
+ /**
40
+ * Framing rules for end states and held states at an output time (edges, caption
41
+ * band). Default: the visible rectangle stays inside the viewport.
42
+ */
43
+ constrain?: Constrain;
44
+ }
45
+ export type Constrain = (state: CameraState, t: number) => CameraState;
46
+ export interface FitRegion {
47
+ x: number;
48
+ y: number;
49
+ }
50
+ /**
51
+ * Default space around a zoom focus without `scale`, in CSS pixels.
52
+ */
53
+ export declare const DEFAULT_FOCUS_PADDING = 40;
54
+ /**
55
+ * Follow keeps the cursor this fraction of the visible size away from the edges.
56
+ */
57
+ export declare const FOLLOW_MARGIN = 0.06;
58
+ /**
59
+ * Angular frequency of the follow spring, in 1/s (settles in about 0.7 s).
60
+ */
61
+ export declare const FOLLOW_OMEGA = 6;
62
+ /**
63
+ * Follow mode 'cursor': the cursor may move freely inside this central fraction of the
64
+ * visible size; the camera travels when it pushes past, on a calmer spring.
65
+ */
66
+ export declare const DEAD_ZONE = 0.3;
67
+ export declare const CURSOR_OMEGA = 5;
68
+ /**
69
+ * The follow mode of a shot: its own `follow` (true is 'edge', false is 'off'), or the
70
+ * render option.
71
+ */
72
+ export declare function followMode(event: Pick<ZoomEvent, 'follow'> | undefined, fallback: FollowMode): FollowMode;
73
+ /**
74
+ * The scale a zoom event asks for: `scale`, else its named `level`, else undefined (fit).
75
+ */
76
+ export declare function zoomScale(event: Pick<ZoomEvent, 'scale' | 'level'>, levels: Record<string, number>): number | undefined;
77
+ /**
78
+ * Moves the center so that the visible rectangle stays inside the viewport.
79
+ */
80
+ export declare function clampCamera(state: CameraState, viewport: Size): CameraState;
81
+ /**
82
+ * The camera state that a zoom event asks for. `scale` is the magnification relative to
83
+ * the rest view. Without it, the focus plus `padding` fits the region the camera can
84
+ * use, which is `fit` times the rest frame.
85
+ */
86
+ export declare function zoomTarget(event: Pick<ZoomEvent, 'focus' | 'scale' | 'padding'>, viewport: Size, maxScale: number, fit?: FitRegion): CameraState;
87
+ /**
88
+ * Interpolates two camera states. The scale changes geometrically and the center moves
89
+ * so that the zoom pivots on the one page point that has the same screen position in
90
+ * both states, like a pinch zoom: every page point moves on a straight line, with one
91
+ * speed peak. Equal scales give a straight pan. Only the end states are constrained.
92
+ */
93
+ export declare function interpolateCamera(a: CameraState, b: CameraState, e: number): CameraState;
94
+ export interface CursorInput {
95
+ x: ArrayLike<number>;
96
+ y: ArrayLike<number>;
97
+ /**
98
+ * Size of the drawn cursor in CSS pixels, kept in view below and right of the hotspot.
99
+ */
100
+ extent: number;
101
+ }
102
+ /**
103
+ * Computes the camera for every output frame. A zoom event starts a transition from the
104
+ * current motion (a running transition keeps running underneath, so position and
105
+ * velocity stay continuous). Between transitions, a critically damped spring pans toward
106
+ * the follow target. The spring reads the target 2/omega ahead, which cancels its lag, so
107
+ * the cursor stays in view even during fast moves. A transition lands on the follow target
108
+ * of its end time, so an engaged follow adds no second move.
109
+ */
110
+ export declare function computeCamera(times: ArrayLike<number>, zooms: TimedEvent<ZoomEvent>[], cursor: CursorInput | null, viewport: Size, options: CameraOptions): CameraTrack;
111
+ /**
112
+ * Generates zoom events from clicks and typing: zoom in so the camera arrives
113
+ * about 300 ms before the first click, pan when a later click is far away, and
114
+ * zoom out `hold` ms after the last activity.
115
+ */
116
+ export declare function autoZoomEvents(events: TimedEvent[], cursorAt: (out: number) => {
117
+ x: number;
118
+ y: number;
119
+ }, viewport: Size, auto: ResolvedAutoZoom, duration: number): TimedEvent<ZoomEvent>[];