@biunow/agent-biutalk 0.0.0-stage → 1.1.0
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/bin/server.js +355 -0
- package/engine/AGENTS.md +1414 -0
- package/engine/CLAUDE.md +1 -0
- package/engine/LICENSE +21 -0
- package/engine/README.md +216 -0
- package/engine/bin/explainroo.js +19 -0
- package/engine/dev/audio-lab.mjs +268 -0
- package/engine/docs/media/contact-sheet.jpg +0 -0
- package/engine/docs/media/dns-poster.jpg +0 -0
- package/engine/docs/media/intro-poster.jpg +0 -0
- package/engine/docs/media/logo-dark.png +0 -0
- package/engine/docs/media/logo-light.png +0 -0
- package/engine/docs/media/looks-dark.webp +0 -0
- package/engine/docs/media/looks-light.webp +0 -0
- package/engine/docs/media/product-demo.webp +0 -0
- package/engine/docs/media/rebase-poster.jpg +0 -0
- package/engine/docs/media/sizes-dark.webp +0 -0
- package/engine/docs/media/sizes-light.webp +0 -0
- package/engine/engine/audio/dsp.js +69 -0
- package/engine/engine/audio/instruments.js +147 -0
- package/engine/engine/audio/music.js +576 -0
- package/engine/engine/audio/pcm.js +93 -0
- package/engine/engine/audio/rng.js +40 -0
- package/engine/engine/audio/sfx.js +287 -0
- package/engine/engine/audio/soundtrack.js +201 -0
- package/engine/engine/audio/synth.js +721 -0
- package/engine/engine/audio/theory.js +105 -0
- package/engine/engine/audio/wav.js +47 -0
- package/engine/engine/captions.js +131 -0
- package/engine/engine/clips.js +107 -0
- package/engine/engine/code.js +179 -0
- package/engine/engine/director.js +475 -0
- package/engine/engine/frame.html +135 -0
- package/engine/engine/icons/LICENSE-lucide.txt +43 -0
- package/engine/engine/icons/lucide-tags.json +1 -0
- package/engine/engine/icons/lucide.json +1 -0
- package/engine/engine/layout.js +69 -0
- package/engine/engine/pathdata.js +234 -0
- package/engine/engine/pen.js +240 -0
- package/engine/engine/player.html +67 -0
- package/engine/engine/player.js +219 -0
- package/engine/engine/readability.js +9 -0
- package/engine/engine/runtime.js +690 -0
- package/engine/engine/stage.js +2247 -0
- package/engine/engine/studio.html +216 -0
- package/engine/engine/text.js +81 -0
- package/engine/engine/themes.js +360 -0
- package/engine/engine/transitions.js +161 -0
- package/engine/engine/ui.js +1042 -0
- package/engine/engine/upload.js +32 -0
- package/engine/engine/util.js +204 -0
- package/engine/examples/callbacks-to-async-await/code-versions.js +34 -0
- package/engine/examples/callbacks-to-async-await/scenes.js +106 -0
- package/engine/examples/callbacks-to-async-await/script.md +22 -0
- package/engine/examples/callbacks-to-async-await/video.json +16 -0
- package/engine/examples/circle-area/scenes.js +228 -0
- package/engine/examples/circle-area/script.md +37 -0
- package/engine/examples/circle-area/video.json +14 -0
- package/engine/examples/explainroo-intro/scenes.js +133 -0
- package/engine/examples/explainroo-intro/script.md +27 -0
- package/engine/examples/explainroo-intro/video.json +8 -0
- package/engine/examples/how-dns-works/scenes.js +130 -0
- package/engine/examples/how-dns-works/script.md +28 -0
- package/engine/examples/how-dns-works/video.json +8 -0
- package/engine/examples/leap-years/scenes.js +156 -0
- package/engine/examples/leap-years/script.md +25 -0
- package/engine/examples/leap-years/video.json +16 -0
- package/engine/examples/leap-years-ja/scenes.js +194 -0
- package/engine/examples/leap-years-ja/script.md +32 -0
- package/engine/examples/leap-years-ja/video.json +17 -0
- package/engine/examples/merge-vs-rebase/scenes.js +129 -0
- package/engine/examples/merge-vs-rebase/script.md +22 -0
- package/engine/examples/merge-vs-rebase/video.json +8 -0
- package/engine/examples/password-storage-zh/scenes.js +185 -0
- package/engine/examples/password-storage-zh/script.md +25 -0
- package/engine/examples/password-storage-zh/video.json +19 -0
- package/engine/examples/sqlite-demo/README.md +19 -0
- package/engine/examples/sqlite-demo/scenes.js +423 -0
- package/engine/examples/sqlite-demo/script.md +35 -0
- package/engine/examples/sqlite-demo/video.json +17 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-400.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-500.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-600.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-700.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-OFL.txt +93 -0
- package/engine/examples/unspar-demo/assets/unspar-logo.svg +23 -0
- package/engine/examples/unspar-demo/scenes.js +367 -0
- package/engine/examples/unspar-demo/script.md +25 -0
- package/engine/examples/unspar-demo/video.json +21 -0
- package/engine/examples/why-is-the-sky-blue-es/scenes.js +160 -0
- package/engine/examples/why-is-the-sky-blue-es/script.md +22 -0
- package/engine/examples/why-is-the-sky-blue-es/video.json +15 -0
- package/engine/fonts/ArchitectsDaughter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/ArchitectsDaughter-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-700-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin.woff2 +0 -0
- package/engine/fonts/Inter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Inter-400-latin.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin-ext.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin.woff2 +0 -0
- package/engine/fonts/NotoSansDevanagari-400.ttf +0 -0
- package/engine/fonts/NotoSansDevanagari-700.ttf +0 -0
- package/engine/fonts/PatrickHand-400-latin-ext.woff2 +0 -0
- package/engine/fonts/PatrickHand-400-latin.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin-ext.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin-ext.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin.woff2 +0 -0
- package/engine/fonts/fonts.json +216 -0
- package/engine/fonts/licenses/ArchitectsDaughter-OFL.txt +93 -0
- package/engine/fonts/licenses/CabinSketch-OFL.txt +93 -0
- package/engine/fonts/licenses/InstrumentSerif-OFL.txt +93 -0
- package/engine/fonts/licenses/Inter-OFL.txt +93 -0
- package/engine/fonts/licenses/JetBrainsMono-OFL.txt +93 -0
- package/engine/fonts/licenses/Kalam-OFL.txt +93 -0
- package/engine/fonts/licenses/NotoSansDevanagari-OFL.txt +118 -0
- package/engine/fonts/licenses/PatrickHand-OFL.txt +93 -0
- package/engine/fonts/licenses/PlusJakartaSans-OFL.txt +93 -0
- package/engine/fonts/licenses/SpaceGrotesk-OFL.txt +93 -0
- package/engine/package-lock.json +1201 -0
- package/engine/package.json +67 -0
- package/engine/scripts/build-icons.mjs +74 -0
- package/engine/scripts/fetch-fonts.mjs +68 -0
- package/engine/skills/explainroo/SKILL.md +51 -0
- package/engine/src/align.js +357 -0
- package/engine/src/browser.js +121 -0
- package/engine/src/capture.js +315 -0
- package/engine/src/chapters.js +32 -0
- package/engine/src/cli.js +421 -0
- package/engine/src/clips.js +19 -0
- package/engine/src/elevenlabs.js +90 -0
- package/engine/src/env.js +16 -0
- package/engine/src/export.js +187 -0
- package/engine/src/ffmpeg.js +103 -0
- package/engine/src/fonts.js +86 -0
- package/engine/src/g2p.py +76 -0
- package/engine/src/images.js +151 -0
- package/engine/src/licenses/misaki-Apache-2.0.txt +201 -0
- package/engine/src/math.js +83 -0
- package/engine/src/models.js +161 -0
- package/engine/src/phonemes.js +162 -0
- package/engine/src/pipeline.js +63 -0
- package/engine/src/project.js +331 -0
- package/engine/src/qa.js +186 -0
- package/engine/src/recordings.js +89 -0
- package/engine/src/render.js +156 -0
- package/engine/src/script.js +266 -0
- package/engine/src/server.js +288 -0
- package/engine/src/subtitles.js +78 -0
- package/engine/src/timeline.js +55 -0
- package/engine/src/voice.js +240 -0
- package/engine/src/wav.js +98 -0
- package/engine/templates/starter/scenes.js +31 -0
- package/engine/templates/starter/script.md +14 -0
- package/engine/test/align.test.js +108 -0
- package/engine/test/brand.test.js +41 -0
- package/engine/test/browser.test.js +89 -0
- package/engine/test/camera-browser.test.js +225 -0
- package/engine/test/caption-phrases.test.js +35 -0
- package/engine/test/capture-browser.test.js +90 -0
- package/engine/test/capture-fixture.js +46 -0
- package/engine/test/capture.test.js +62 -0
- package/engine/test/chapters.test.js +41 -0
- package/engine/test/cjk-browser.test.js +101 -0
- package/engine/test/cjk.test.js +232 -0
- package/engine/test/cli-exit.test.js +25 -0
- package/engine/test/cli.test.js +116 -0
- package/engine/test/clips-browser.test.js +125 -0
- package/engine/test/clips.test.js +67 -0
- package/engine/test/code-morph-browser.test.js +99 -0
- package/engine/test/code-morph.test.js +110 -0
- package/engine/test/contrast-browser.test.js +63 -0
- package/engine/test/contrast.test.js +80 -0
- package/engine/test/director.test.js +316 -0
- package/engine/test/engine-pure.test.js +29 -0
- package/engine/test/export-browser.test.js +224 -0
- package/engine/test/export.test.js +96 -0
- package/engine/test/fixtures/capture.html +55 -0
- package/engine/test/formats-pace.test.js +104 -0
- package/engine/test/languages-browser.test.js +78 -0
- package/engine/test/languages.test.js +140 -0
- package/engine/test/looks-browser.test.js +109 -0
- package/engine/test/looks-preview.test.js +57 -0
- package/engine/test/looks.test.js +130 -0
- package/engine/test/math-browser.test.js +114 -0
- package/engine/test/math.test.js +54 -0
- package/engine/test/multi-size-browser.test.js +133 -0
- package/engine/test/multi-size.test.js +116 -0
- package/engine/test/phonemes.test.js +92 -0
- package/engine/test/readability-browser.test.js +36 -0
- package/engine/test/readability.test.js +20 -0
- package/engine/test/render-options.test.js +52 -0
- package/engine/test/script.test.js +67 -0
- package/engine/test/server.test.js +256 -0
- package/engine/test/stills-browser.test.js +70 -0
- package/engine/test/subtitles.test.js +83 -0
- package/engine/test/timeline.test.js +31 -0
- package/engine/test/transparent-browser.test.js +123 -0
- package/engine/test/ui-kit.test.js +96 -0
- package/engine/test/upload-browser.test.js +55 -0
- package/engine/test/voice-providers.test.js +338 -0
- package/lib/host.mjs +480 -0
- package/manifest.json +54 -0
- package/package.json +8 -4
- package/skills/biutalk/SKILL.md +21 -0
- package/studio/app.js +209 -0
- package/studio/assert-panel-permit.mjs +67 -0
- package/studio/composer.css +354 -0
- package/studio/composer.js +169 -0
- package/studio/index.html +82 -0
- package/studio/looks/blueprint.svg +21 -0
- package/studio/looks/chalk.svg +15 -0
- package/studio/looks/clean.svg +20 -0
- package/studio/looks/editorial.svg +18 -0
- package/studio/looks/midnight.svg +17 -0
- package/studio/looks/paper.svg +11 -0
- package/studio/select-menu.js +268 -0
- package/studio/styles.css +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
// The automatic camera. Before anything is drawn, explainroo measures each
|
|
2
|
+
// element of a scene once, and this file plans the camera from that. The
|
|
3
|
+
// camera opens on the picture and widens when something new would not fit.
|
|
4
|
+
// Something new that already fits needs no move. Now and then the camera
|
|
5
|
+
// moves in on a new thing and then follows new things at that distance. It
|
|
6
|
+
// moves in again only five seconds after the last change of zoom. Near the
|
|
7
|
+
// end of the scene it shows the whole picture. During each scene it also
|
|
8
|
+
// pushes in very slowly. Scenes that call s.camera() keep their own camera.
|
|
9
|
+
import { clamp, lerp } from './util.js';
|
|
10
|
+
|
|
11
|
+
// Things that appear within BEAT_GAP seconds of each other are one beat
|
|
12
|
+
// and get one shot.
|
|
13
|
+
const BEAT_GAP = 0.8;
|
|
14
|
+
const FILL = 0.72; // share of the view a close-up's subject fills
|
|
15
|
+
const PICTURE_FILL = 0.82; // share of the view the whole picture fills
|
|
16
|
+
const MAX_ZOOM = 2.2;
|
|
17
|
+
const LEAD = 0.3; // the camera starts moving a moment before the element appears
|
|
18
|
+
const MIN_HOLD = 2.2; // shortest time between the starts of two moves
|
|
19
|
+
const SETTLE = 0.08; // smaller changes than this are not worth a move
|
|
20
|
+
const DRIFT = 0.035; // how much closer the slow push gets over a whole scene
|
|
21
|
+
// Moving in on something: the new shot is at least PUSH times closer, the
|
|
22
|
+
// viewer gets PUSH_HOLD seconds to look once the camera has arrived, and the
|
|
23
|
+
// zoom last changed, in or out, PUSH_GAP or more seconds ago.
|
|
24
|
+
const PUSH = 1.3;
|
|
25
|
+
const PUSH_HOLD = 1.5;
|
|
26
|
+
const PUSH_GAP = 5;
|
|
27
|
+
// A highlighted code line or terminal command gets a close-up when it is at
|
|
28
|
+
// least this much closer.
|
|
29
|
+
const FOCUS_GAIN = 1.25;
|
|
30
|
+
// Kinds that are a background when they cover most of the frame.
|
|
31
|
+
const BACKDROPS = ['shape', 'custom', 'image', 'clip', 'panel'];
|
|
32
|
+
|
|
33
|
+
const smoother = (p) => p * p * p * (p * (p * 6 - 15) + 10);
|
|
34
|
+
|
|
35
|
+
function union(boxes) {
|
|
36
|
+
let x0 = Infinity;
|
|
37
|
+
let y0 = Infinity;
|
|
38
|
+
let x1 = -Infinity;
|
|
39
|
+
let y1 = -Infinity;
|
|
40
|
+
for (const b of boxes) {
|
|
41
|
+
x0 = Math.min(x0, b.x - b.w / 2);
|
|
42
|
+
y0 = Math.min(y0, b.y - b.h / 2);
|
|
43
|
+
x1 = Math.max(x1, b.x + b.w / 2);
|
|
44
|
+
y1 = Math.max(y1, b.y + b.h / 2);
|
|
45
|
+
}
|
|
46
|
+
return { x: (x0 + x1) / 2, y: (y0 + y1) / 2, w: x1 - x0, h: y1 - y0 };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// view: the part of the frame the subject should fill (the safe area),
|
|
50
|
+
// frame: { W, H }. Returns { x, y, z }: the scene point shown at the view's
|
|
51
|
+
// center and the zoom.
|
|
52
|
+
export function frameBox(box, view, frame, maxZoom = MAX_ZOOM, fill = FILL) {
|
|
53
|
+
const z = clamp(Math.min((view.w * fill) / Math.max(1, box.w), (view.h * fill) / Math.max(1, box.h)), 1, maxZoom);
|
|
54
|
+
return clampShot({ x: box.x, y: box.y, z }, view, frame);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Keeps the visible part of the scene inside the frame, so backgrounds a
|
|
58
|
+
// scene paints itself never show their edges.
|
|
59
|
+
function clampShot(shot, view, frame) {
|
|
60
|
+
const cx = view.x + view.w / 2;
|
|
61
|
+
const cy = view.y + view.h / 2;
|
|
62
|
+
const z = shot.z;
|
|
63
|
+
return {
|
|
64
|
+
x: clamp(shot.x, cx / z, frame.W - (frame.W - cx) / z),
|
|
65
|
+
y: clamp(shot.y, cy / z, frame.H - (frame.H - cy) / z),
|
|
66
|
+
z,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The part of the scene the whole frame shows.
|
|
71
|
+
export function frameRect(shot, view, frame) {
|
|
72
|
+
const cx = view.x + view.w / 2;
|
|
73
|
+
const cy = view.y + view.h / 2;
|
|
74
|
+
return { x0: shot.x - cx / shot.z, y0: shot.y - cy / shot.z, x1: shot.x + (frame.W - cx) / shot.z, y1: shot.y + (frame.H - cy) / shot.z };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function viewRect(shot, view) {
|
|
78
|
+
const cx = view.x + view.w / 2;
|
|
79
|
+
const cy = view.y + view.h / 2;
|
|
80
|
+
return { x0: shot.x - (cx - view.x) / shot.z, y0: shot.y - (cy - view.y) / shot.z, x1: shot.x + (view.x + view.w - cx) / shot.z, y1: shot.y + (view.y + view.h - cy) / shot.z };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Whether a box is partly in view. Edges within `margin` of the view's edge
|
|
84
|
+
// count as cut too, so nothing touches the edge of the frame. A box that
|
|
85
|
+
// pokes in by less than `slack` counts as out.
|
|
86
|
+
// `r` is the whole frame; `use` (default r) the part captions and app
|
|
87
|
+
// overlays leave free, which a visible box has to sit in.
|
|
88
|
+
function cut(box, r, margin, slack = 0, use = r) {
|
|
89
|
+
const x0 = box.x - box.w / 2;
|
|
90
|
+
const x1 = box.x + box.w / 2;
|
|
91
|
+
const y0 = box.y - box.h / 2;
|
|
92
|
+
const y1 = box.y + box.h / 2;
|
|
93
|
+
const outside = x1 <= r.x0 + slack || x0 >= r.x1 - slack || y1 <= r.y0 + slack || y0 >= r.y1 - slack;
|
|
94
|
+
const inside = x0 >= use.x0 + margin && x1 <= use.x1 - margin && y0 >= use.y0 + margin && y1 <= use.y1 - margin;
|
|
95
|
+
return !inside && !outside;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The free part of the frame (frame pixels) as scene coordinates for a shot.
|
|
99
|
+
function usableRect(shot, view, usable) {
|
|
100
|
+
const cx = view.x + view.w / 2;
|
|
101
|
+
const cy = view.y + view.h / 2;
|
|
102
|
+
return { x0: shot.x + (usable.x0 - cx) / shot.z, y0: shot.y + (usable.y0 - cy) / shot.z, x1: shot.x + (usable.x1 - cx) / shot.z, y1: shot.y + (usable.y1 - cy) / shot.z };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Whether a shot shows `box` (when given) whole and cuts none of `others`,
|
|
106
|
+
// at its own zoom and at the tightest framing the slow drift reaches.
|
|
107
|
+
function clean(shot, box, others, view, frame, usable) {
|
|
108
|
+
const z = shot.z;
|
|
109
|
+
const margin = (Math.min(frame.W, frame.H) * 0.07) / z;
|
|
110
|
+
const tight = clampShot({ ...shot, z: z * (1 + DRIFT) }, view, frame);
|
|
111
|
+
if (box) {
|
|
112
|
+
const v = viewRect(tight, view);
|
|
113
|
+
if (box.x - box.w / 2 < v.x0 - 0.5 || box.x + box.w / 2 > v.x1 + 0.5 || box.y - box.h / 2 < v.y0 - 0.5 || box.y + box.h / 2 > v.y1 + 0.5) return false;
|
|
114
|
+
}
|
|
115
|
+
const r = frameRect(tight, view, frame);
|
|
116
|
+
const wide = frameRect(shot, view, frame);
|
|
117
|
+
return !others.some((e) => cut(e.box, r, margin, 1 / z, usableRect(tight, view, usable)) || cut(e.box, wide, margin, 1 / z, usableRect(shot, view, usable)));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Frames `focus` as close as it can while every other visible element is
|
|
121
|
+
// either fully in the shot or fully out of it. Half a label at the edge
|
|
122
|
+
// looks like a mistake. It tries a few zoom levels. For each it tries the
|
|
123
|
+
// center on the subject and the centers that put a neighbour right inside
|
|
124
|
+
// or right outside the frame's edge, and keeps the closest, best centered
|
|
125
|
+
// shot. At zoom 1 the shot is the frame as it was written, which always
|
|
126
|
+
// works.
|
|
127
|
+
export function compose(focus, visible, view, frame, usable = { x0: 0, y0: 0, x1: frame.W, y1: frame.H }, fill = FILL, maxZoom = MAX_ZOOM) {
|
|
128
|
+
const box = union(focus.map((e) => e.box));
|
|
129
|
+
// Arrows and lines may run off the edge of a close-up, and so may an
|
|
130
|
+
// element the shot looks into, like a code window around its highlighted
|
|
131
|
+
// lines. A cut label or card may not.
|
|
132
|
+
const looking = focus.every((e) => e.kind === 'focus');
|
|
133
|
+
const others = visible.filter((e) => !focus.includes(e) && !['arrow', 'line', 'focus'].includes(e.kind) && !(looking && contains(e.box, box)));
|
|
134
|
+
const cx = view.x + view.w / 2;
|
|
135
|
+
const cy = view.y + view.h / 2;
|
|
136
|
+
const best0 = frameBox(box, view, frame, Math.max(1, maxZoom), fill);
|
|
137
|
+
let best = null;
|
|
138
|
+
for (let z = best0.z; z > 1.001; z /= 1.06) {
|
|
139
|
+
const margin = (Math.min(frame.W, frame.H) * 0.07) / z;
|
|
140
|
+
// Centers that keep the subject inside the view.
|
|
141
|
+
const xr = [box.x + box.w / 2 + margin - (view.x + view.w - cx) / z, box.x - box.w / 2 - margin + (cx - view.x) / z];
|
|
142
|
+
const yr = [box.y + box.h / 2 + margin - (view.y + view.h - cy) / z, box.y - box.h / 2 - margin + (cy - view.y) / z];
|
|
143
|
+
if (xr[0] > xr[1] || yr[0] > yr[1]) continue;
|
|
144
|
+
const pad = 3 / z;
|
|
145
|
+
const xs = [box.x];
|
|
146
|
+
const ys = [box.y];
|
|
147
|
+
for (const e of others) {
|
|
148
|
+
const l = e.box.x - e.box.w / 2;
|
|
149
|
+
const r = e.box.x + e.box.w / 2;
|
|
150
|
+
const t = e.box.y - e.box.h / 2;
|
|
151
|
+
const b = e.box.y + e.box.h / 2;
|
|
152
|
+
xs.push(r + pad + cx / z, l - pad - (frame.W - cx) / z, l - margin + cx / z, r + margin - (frame.W - cx) / z);
|
|
153
|
+
ys.push(b + pad + cy / z, t - pad - (frame.H - cy) / z, t - margin + cy / z, b + margin - (frame.H - cy) / z);
|
|
154
|
+
}
|
|
155
|
+
for (const x0 of xs) {
|
|
156
|
+
if (x0 < xr[0] - 1e-6 || x0 > xr[1] + 1e-6) continue;
|
|
157
|
+
for (const y0 of ys) {
|
|
158
|
+
if (y0 < yr[0] - 1e-6 || y0 > yr[1] + 1e-6) continue;
|
|
159
|
+
const shot = clampShot({ x: x0, y: y0, z }, view, frame);
|
|
160
|
+
if (!clean(shot, box, others, view, frame, usable)) continue;
|
|
161
|
+
const off = Math.hypot((shot.x - box.x) * z / view.w, (shot.y - box.y) * z / view.h);
|
|
162
|
+
const score = Math.log(z) - 0.8 * off;
|
|
163
|
+
if (!best || score > best.score) best = { shot, score };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (best) return best.shot;
|
|
168
|
+
// Otherwise the frame as it was written. Its slow push stays off when it
|
|
169
|
+
// would move something into the captions or the app's buttons.
|
|
170
|
+
const full = frameBox(box, view, frame, 1);
|
|
171
|
+
const tight = clampShot({ ...full, z: 1 + DRIFT }, view, frame);
|
|
172
|
+
const within = (e, shot) => {
|
|
173
|
+
const u = usableRect(shot, view, usable);
|
|
174
|
+
return e.box.x - e.box.w / 2 >= u.x0 && e.box.x + e.box.w / 2 <= u.x1 && e.box.y - e.box.h / 2 >= u.y0 && e.box.y + e.box.h / 2 <= u.y1;
|
|
175
|
+
};
|
|
176
|
+
const held = [...focus, ...others].filter((e) => !['arrow', 'line', 'focus'].includes(e.kind));
|
|
177
|
+
return held.some((e) => within(e, full) && !within(e, tight)) ? { ...full, drift: 0 } : full;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function contains(outer, inner) {
|
|
181
|
+
return outer.x - outer.w / 2 <= inner.x - inner.w / 2 + 0.5 && outer.x + outer.w / 2 >= inner.x + inner.w / 2 - 0.5 && outer.y - outer.h / 2 <= inner.y - inner.h / 2 + 0.5 && outer.y + outer.h / 2 >= inner.y + inner.h / 2 - 0.5;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function same(a, b) {
|
|
185
|
+
return Math.abs(a.x - b.x) < 0.01 && Math.abs(a.y - b.y) < 0.01 && Math.abs(a.z - b.z) < 1e-4 && (a.drift ?? 1) === (b.drift ?? 1);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// How long a move takes at pace 1: longer for a bigger change of zoom or
|
|
189
|
+
// place, and a little longer for the pull-back at the end of a scene.
|
|
190
|
+
function moveLength(from, to, kind, frame) {
|
|
191
|
+
const distance = Math.hypot(to.x - from.x, to.y - from.y) / (frame.W / Math.min(from.z, to.z));
|
|
192
|
+
return clamp(0.9 + 0.5 * Math.abs(Math.log(to.z / from.z)) + 0.6 * distance, 0.95, 1.7) * (kind === 'reveal' ? 1.2 : 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function differs(a, b, frame) {
|
|
196
|
+
return (a.drift ?? 1) !== (b.drift ?? 1) || Math.abs(Math.log(a.z / b.z)) > SETTLE || Math.hypot(a.x - b.x, a.y - b.y) > (SETTLE * frame.W) / Math.max(a.z, b.z);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// elements: [{ at, out, box }] in scene coordinates. Returns null when there
|
|
200
|
+
// is nothing to film.
|
|
201
|
+
// `from`: where the camera is when the scene starts, for a scene that
|
|
202
|
+
// continues the one before it.
|
|
203
|
+
export function planCamera(elements, { dur, view, frame, pace = 1, voiceEnd = null, from = null, still = 0, usable = null }) {
|
|
204
|
+
usable ??= { x0: 0, y0: 0, x1: frame.W, y1: frame.H };
|
|
205
|
+
const area = frame.W * frame.H;
|
|
206
|
+
const items = elements
|
|
207
|
+
// A plain shape, drawing or picture that covers most of the frame is a
|
|
208
|
+
// background. Charts, code, text and what a scene draws by hand ("ink")
|
|
209
|
+
// count however large they are.
|
|
210
|
+
.filter((e) => e.box && (!BACKDROPS.includes(e.kind) || e.box.w * e.box.h < area * 0.6) && (e.box.w > 2 || e.box.h > 2))
|
|
211
|
+
.map((e) => ({ ...e, at: Math.max(0, e.at), out: e.out ?? Infinity }))
|
|
212
|
+
.filter((e) => e.at < dur - 0.3)
|
|
213
|
+
.sort((a, b) => a.at - b.at);
|
|
214
|
+
if (!items.length) return null;
|
|
215
|
+
|
|
216
|
+
const beats = [];
|
|
217
|
+
for (const e of items) {
|
|
218
|
+
const last = beats[beats.length - 1];
|
|
219
|
+
if (last && e.at - last.start <= BEAT_GAP) last.els.push(e);
|
|
220
|
+
else beats.push({ start: e.at, els: [e] });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const hold = MIN_HOLD / pace;
|
|
224
|
+
const visibleDuring = (t0, t1) => items.filter((e) => e.at <= t1 && e.out > t0 + 0.25);
|
|
225
|
+
const picture = (list) => list.filter((e) => e.kind !== 'focus');
|
|
226
|
+
// What the camera may move in on. An arrow's label is kept whole, but it
|
|
227
|
+
// is part of the arrow, which gets no close-up.
|
|
228
|
+
const solid = (list) => list.filter((e) => !['arrow', 'line', 'focus', 'label'].includes(e.kind));
|
|
229
|
+
// Everything on screen from t0 to t1, framed as one picture.
|
|
230
|
+
const overview = (t0, t1) => {
|
|
231
|
+
const v = picture(visibleDuring(t0, t1));
|
|
232
|
+
return v.length ? compose(v, v, view, frame, usable, PICTURE_FILL) : null;
|
|
233
|
+
};
|
|
234
|
+
// Whether a shot already shows these elements whole, in the free part of
|
|
235
|
+
// the frame, also at the end of its slow push. A beat of only arrows and
|
|
236
|
+
// lines fits when they are at least partly in view. An arrow's label has
|
|
237
|
+
// to be whole.
|
|
238
|
+
const fits = (shot, list) => {
|
|
239
|
+
const body = list.filter((e) => e.kind !== 'arrow' && e.kind !== 'line');
|
|
240
|
+
const tight = shot.drift === 0 ? shot : clampShot({ ...shot, z: shot.z * (1 + DRIFT) }, view, frame);
|
|
241
|
+
const pad = (Math.min(frame.W, frame.H) * 0.02) / shot.z;
|
|
242
|
+
return [shot, tight].every((s) => {
|
|
243
|
+
const u = usableRect(s, view, usable);
|
|
244
|
+
if (!body.length) return list.every((e) => e.box.x + e.box.w / 2 > u.x0 && e.box.x - e.box.w / 2 < u.x1 && e.box.y + e.box.h / 2 > u.y0 && e.box.y - e.box.h / 2 < u.y1);
|
|
245
|
+
return body.every((e) => e.box.x - e.box.w / 2 >= u.x0 + pad && e.box.x + e.box.w / 2 <= u.x1 - pad && e.box.y - e.box.h / 2 >= u.y0 + pad && e.box.y + e.box.h / 2 <= u.y1 - pad);
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
// The one or two elements in `before` closest to `els`.
|
|
249
|
+
const nearest = (els, before) => {
|
|
250
|
+
if (!before.length) return [];
|
|
251
|
+
const c = union(els.map((e) => e.box));
|
|
252
|
+
const d = (e) => Math.hypot(e.box.x - c.x, e.box.y - c.y);
|
|
253
|
+
const sorted = [...before].sort((a, b) => d(a) - d(b));
|
|
254
|
+
return sorted.slice(0, sorted.length > 1 && d(sorted[1]) < d(sorted[0]) * 1.4 ? 2 : 1);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// Near the end the camera shows the whole picture.
|
|
258
|
+
const lastBeat = beats[beats.length - 1].start;
|
|
259
|
+
const revealAt = Math.max(lastBeat + 1.8, (voiceEnd ?? lastBeat) - 0.9);
|
|
260
|
+
// When the next new thing or focus point after beat i would not fit the
|
|
261
|
+
// shot.
|
|
262
|
+
const heldUntil = (shot, i) => {
|
|
263
|
+
for (let j = i + 1; j < beats.length; j++) {
|
|
264
|
+
const next = beats[j].els.filter((e) => e.out > beats[j].start + 0.4);
|
|
265
|
+
if (next.length && !fits(shot, next)) return Math.max(first, beats[j].start - LEAD);
|
|
266
|
+
}
|
|
267
|
+
return dur;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// `still`: the camera holds while the scene dissolves in from the one it
|
|
271
|
+
// continues, so both pictures stay on top of each other.
|
|
272
|
+
const first = Math.max(0, still);
|
|
273
|
+
const shots = [];
|
|
274
|
+
let cur = from;
|
|
275
|
+
let last = from ? first - hold : -Infinity;
|
|
276
|
+
let zoomed = -Infinity;
|
|
277
|
+
// Takes a shot from `begin` on. The first one opens the scene.
|
|
278
|
+
// A required move keeps something new whole, however small it is.
|
|
279
|
+
const take = (shot, begin, kind, required = false) => {
|
|
280
|
+
if (!shot) return false;
|
|
281
|
+
if (cur && (required ? same(cur, shot) : !differs(cur, shot, frame))) return false;
|
|
282
|
+
if (cur && Math.abs(Math.log(shot.z / cur.z)) > 0.1) zoomed = begin;
|
|
283
|
+
shots.push({ begin: cur ? begin : 0, shot, kind, required });
|
|
284
|
+
// The opening is not a move: the first move may follow it sooner.
|
|
285
|
+
last = cur ? begin : begin - hold * 0.4;
|
|
286
|
+
cur = shot;
|
|
287
|
+
return true;
|
|
288
|
+
};
|
|
289
|
+
for (let i = 0; i < beats.length; i++) {
|
|
290
|
+
const beat = beats[i];
|
|
291
|
+
const begin = Math.max(first, beat.start - LEAD);
|
|
292
|
+
const next = i + 1 < beats.length ? Math.max(first, beats[i + 1].start - LEAD) : dur;
|
|
293
|
+
const free = !cur || begin >= last + hold - 1e-9;
|
|
294
|
+
// Whether the camera may move closer now.
|
|
295
|
+
const closer = !cur || (next - begin >= (PUSH_HOLD + 1) / pace && begin - zoomed >= PUSH_GAP / pace);
|
|
296
|
+
const points = beat.els.filter((e) => e.kind === 'focus');
|
|
297
|
+
const fresh = picture(beat.els).filter((e) => e.out > beat.start + 0.4);
|
|
298
|
+
|
|
299
|
+
// Highlighted code lines, terminal commands and s.focus get a close-up
|
|
300
|
+
// when the shot does not already show them big and there is time.
|
|
301
|
+
if (points.length && free) {
|
|
302
|
+
const explicit = points.some((p) => p.explicit);
|
|
303
|
+
const ends = Math.min(...points.map((p) => p.out));
|
|
304
|
+
const close = compose(points, visibleDuring(begin, Math.min(ends, begin + hold * 2 + LEAD)), view, frame, usable, FILL, explicit || closer ? MAX_ZOOM : cur.z);
|
|
305
|
+
// The close-up holds until it ends or something new would not fit it.
|
|
306
|
+
const until = Math.min(ends, heldUntil(close, i));
|
|
307
|
+
const gain = cur ? close.z / cur.z : Infinity;
|
|
308
|
+
if (until - begin >= hold && ((cur && !fits(cur, points)) || gain >= (explicit ? 1.05 : FOCUS_GAIN))) {
|
|
309
|
+
if (take(close, begin, 'focus', Boolean(cur && !fits(cur, points)))) continue;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (!fresh.length) continue;
|
|
313
|
+
const window = begin + hold + LEAD;
|
|
314
|
+
if (!cur) {
|
|
315
|
+
take(overview(begin, window), begin, 'picture');
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (!free && !fits(cur, fresh)) {
|
|
319
|
+
// Too soon after the last move for something that does not fit: the
|
|
320
|
+
// last shot takes in the whole picture instead.
|
|
321
|
+
if (shots.length) {
|
|
322
|
+
const prev = shots[shots.length - 1];
|
|
323
|
+
const wide = overview(prev.begin, window);
|
|
324
|
+
if (wide) Object.assign(prev, { shot: wide, kind: 'picture' });
|
|
325
|
+
cur = prev.shot;
|
|
326
|
+
}
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const before = items.filter((e) => e.at < beat.start - 0.05 && e.out > beat.start + 0.3 && !['focus', 'ink', 'label'].includes(e.kind));
|
|
330
|
+
if (fits(cur, fresh)) {
|
|
331
|
+
// It is already in the shot. The camera moves in only on a new card,
|
|
332
|
+
// label or picture. A line, an arrow, or something drawn inside an
|
|
333
|
+
// element that is already there, like a line in a chart, gets no
|
|
334
|
+
// close-up. Nor does an icon or shape without words: up close it shows
|
|
335
|
+
// nothing more. It tries the new thing with its neighbours first, then
|
|
336
|
+
// on its own.
|
|
337
|
+
const own = solid(fresh).filter((e) => !e.bare && !before.some((f) => f.box.w * f.box.h >= e.box.w * e.box.h * 2 && contains(f.box, e.box)));
|
|
338
|
+
// It is on screen already, so the push may wait a moment for its turn.
|
|
339
|
+
const at = Math.max(begin, last + hold);
|
|
340
|
+
if (!own.length || at > beat.start + 1 / pace || at - zoomed < PUSH_GAP / pace) continue;
|
|
341
|
+
// What comes soon after belongs to the same shot when it can.
|
|
342
|
+
const soon = beats.slice(i + 1).filter((b) => b.start - beat.start <= PUSH_HOLD / pace).flatMap((b) => solid(picture(b.els)));
|
|
343
|
+
const near = nearest(own, before);
|
|
344
|
+
const seen = visibleDuring(at, at + PUSH_HOLD / pace + hold);
|
|
345
|
+
for (const list of [[...near, ...own, ...soon], [...near, ...own], [...own, ...soon], own]) {
|
|
346
|
+
const shot = compose(list, seen, view, frame, usable);
|
|
347
|
+
const until = Math.min(heldUntil(shot, i), fits(shot, picture(visibleDuring(revealAt, revealAt + 0.1))) ? dur : revealAt);
|
|
348
|
+
if (shot.z >= cur.z * PUSH && until - at - moveLength(cur, shot, 'push', frame) / pace >= PUSH_HOLD / pace) {
|
|
349
|
+
take(shot, at, 'push');
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
// It does not fit: the new thing with what it relates to, at most as
|
|
356
|
+
// close as the camera may go now.
|
|
357
|
+
const visible = visibleDuring(begin, Math.max(next, window));
|
|
358
|
+
const limit = closer ? MAX_ZOOM : cur.z;
|
|
359
|
+
let subject = compose([...nearest(fresh, before), ...fresh], visible, view, frame, usable, FILL, limit);
|
|
360
|
+
if (subject.z < Math.min(1.15, limit)) subject = compose(fresh, visible, view, frame, usable, FILL, limit);
|
|
361
|
+
take(fits(subject, fresh) ? subject : overview(begin, window), begin, 'follow', true);
|
|
362
|
+
}
|
|
363
|
+
if (!shots.length && !from) return null;
|
|
364
|
+
// Near the end the camera shows the whole picture when the shot leaves
|
|
365
|
+
// part of it out.
|
|
366
|
+
const reveal = Math.max(revealAt, last + hold);
|
|
367
|
+
// A scene too short for that ends on its last move. When that move is a
|
|
368
|
+
// close-up that leaves part of the picture out, the last frame would not
|
|
369
|
+
// show the whole idea, so the move frames the whole picture instead. A
|
|
370
|
+
// move in that becomes a wide shot is then usually no move at all.
|
|
371
|
+
const atEnd = items.filter((e) => e.kind !== 'focus' && e.at <= dur - 0.1 && e.out >= dur - 0.05);
|
|
372
|
+
const tail = shots[shots.length - 1];
|
|
373
|
+
if (reveal >= dur - 1.1 && atEnd.length && tail && tail.begin > 0 && ['push', 'follow'].includes(tail.kind) && !fits(cur, atEnd)) {
|
|
374
|
+
const wide = overview(tail.begin, dur);
|
|
375
|
+
if (wide) cur = tail.shot = wide;
|
|
376
|
+
}
|
|
377
|
+
if (cur && reveal < dur - 1.1) {
|
|
378
|
+
const all = picture(visibleDuring(reveal, reveal + 0.1));
|
|
379
|
+
if (all.length && !fits(cur, all)) take(compose(all, visibleDuring(reveal, dur), view, frame, usable, PICTURE_FILL), reveal, 'reveal');
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const start = from ?? shots[0].shot;
|
|
383
|
+
const moves = [];
|
|
384
|
+
let current = start;
|
|
385
|
+
for (const { begin, shot, kind, required } of from ? shots : shots.slice(1)) {
|
|
386
|
+
if (required ? same(current, shot) : !differs(current, shot, frame)) continue;
|
|
387
|
+
// Every move ends before the scene does.
|
|
388
|
+
const room = dur - 0.05 - begin;
|
|
389
|
+
if (room < 0.45 / pace) continue;
|
|
390
|
+
const length = Math.min(room, moveLength(current, shot, kind, frame) / pace);
|
|
391
|
+
moves.push({ start: begin, dur: length, to: shot, kind });
|
|
392
|
+
current = shot;
|
|
393
|
+
}
|
|
394
|
+
// Each move starts from wherever the camera is at that moment, so moves
|
|
395
|
+
// that overlap stay smooth.
|
|
396
|
+
const plan = { start, moves, dur, view, frame, still, elements: items };
|
|
397
|
+
for (const m of moves) {
|
|
398
|
+
m.from = cameraAt(plan, m.start, false);
|
|
399
|
+
// Between shots far apart the camera pulls back on the way, so less is
|
|
400
|
+
// cut off while it travels.
|
|
401
|
+
const a = frameRect(m.from, view, frame);
|
|
402
|
+
const b = frameRect(m.to, view, frame);
|
|
403
|
+
const shared = Math.max(0, Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0)) * Math.max(0, Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0));
|
|
404
|
+
if (shared < 0.25 * Math.min((a.x1 - a.x0) * (a.y1 - a.y0), (b.x1 - b.x0) * (b.y1 - b.y0))) {
|
|
405
|
+
const both = { x: (Math.min(a.x0, b.x0) + Math.max(a.x1, b.x1)) / 2, y: (Math.min(a.y0, b.y0) + Math.max(a.y1, b.y1)) / 2, w: Math.max(a.x1, b.x1) - Math.min(a.x0, b.x0), h: Math.max(a.y1, b.y1) - Math.min(a.y0, b.y0) };
|
|
406
|
+
const via = frameBox(both, view, frame, Math.min(m.from.z, m.to.z));
|
|
407
|
+
if (via.z < Math.min(m.from.z, m.to.z) * 0.97) m.via = via;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return plan;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// `drift` on a shot (0 or 1, default 1) says whether the slow push may run
|
|
414
|
+
// there; moves blend it.
|
|
415
|
+
export function cameraAt(plan, t, drift = true) {
|
|
416
|
+
let cam = plan.start;
|
|
417
|
+
for (const m of plan.moves) {
|
|
418
|
+
if (t < m.start || !m.from) break;
|
|
419
|
+
const p = smoother(clamp((t - m.start) / m.dur));
|
|
420
|
+
const d = lerp(m.from.drift ?? 1, m.to.drift ?? 1, p);
|
|
421
|
+
if (m.via) {
|
|
422
|
+
// A curve through the wider shot in the middle.
|
|
423
|
+
const q = (a, b, c) => (1 - p) * (1 - p) * a + 2 * (1 - p) * p * b + p * p * c;
|
|
424
|
+
const mid = (k) => 2 * m.via[k] - (m.from[k] + m.to[k]) / 2;
|
|
425
|
+
cam = {
|
|
426
|
+
x: q(m.from.x, mid('x'), m.to.x),
|
|
427
|
+
y: q(m.from.y, mid('y'), m.to.y),
|
|
428
|
+
z: Math.max(1, Math.exp(q(Math.log(m.from.z), 2 * Math.log(m.via.z) - (Math.log(m.from.z) + Math.log(m.to.z)) / 2, Math.log(m.to.z)))),
|
|
429
|
+
};
|
|
430
|
+
cam = { ...clampShot(cam, plan.view, plan.frame), drift: d };
|
|
431
|
+
} else {
|
|
432
|
+
cam = {
|
|
433
|
+
x: lerp(m.from.x, m.to.x, p),
|
|
434
|
+
y: lerp(m.from.y, m.to.y, p),
|
|
435
|
+
z: Math.exp(lerp(Math.log(m.from.z), Math.log(m.to.z), p)),
|
|
436
|
+
drift: d,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (!drift) return cam;
|
|
441
|
+
// The slow push starts once a continued scene has dissolved in.
|
|
442
|
+
const still = plan.still || 0;
|
|
443
|
+
const push = 1 + DRIFT * (cam.drift ?? 1) * clamp((t - still) / Math.max(1, plan.dur - still));
|
|
444
|
+
return { ...clampShot({ x: cam.x, y: cam.y, z: cam.z * push }, plan.view, plan.frame), drift: cam.drift ?? 1 };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// True while the camera is in a move (not counting the slow drift).
|
|
448
|
+
export function cameraMoving(plan, t) {
|
|
449
|
+
return plan.moves.some((m) => t > m.start && t < m.start + m.dur);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Whether scene B picks up where scene A left off: what A's last frame
|
|
453
|
+
// shows is mostly still there when B starts, in the same places. `last` is
|
|
454
|
+
// the part of the scene A's camera shows at the end. An arrow's label goes
|
|
455
|
+
// with its arrow, so it does not count on its own.
|
|
456
|
+
export function continues(a, b, last) {
|
|
457
|
+
const inView = (e) => e.box.x + e.box.w / 2 > last.x0 && e.box.x - e.box.w / 2 < last.x1 && e.box.y + e.box.h / 2 > last.y0 && e.box.y - e.box.h / 2 < last.y1;
|
|
458
|
+
a = a.filter((e) => e.kind !== 'label');
|
|
459
|
+
b = b.filter((e) => e.kind !== 'label');
|
|
460
|
+
const end = a.filter((e) => e.out === Infinity && inView(e));
|
|
461
|
+
const carried = b.filter((e) => e.at <= 0.05);
|
|
462
|
+
if (end.length < 2 || carried.length < 2) return false;
|
|
463
|
+
const same = (x, y) => x.kind === y.kind && Math.abs(x.box.x - y.box.x) < 2 && Math.abs(x.box.y - y.box.y) < 2 && Math.abs(x.box.w - y.box.w) < 2 && Math.abs(x.box.h - y.box.h) < 2;
|
|
464
|
+
const kept = end.filter((e) => carried.some((f) => same(e, f)));
|
|
465
|
+
const known = carried.filter((e) => a.some((f) => same(e, f)));
|
|
466
|
+
return kept.length >= end.length * 0.7 && known.length >= carried.length * 0.6;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Where the background is, for the camera at `cam`: it follows at a third of
|
|
470
|
+
// the camera's strength, as if it sat further back.
|
|
471
|
+
export function backgroundShot(plan, cam, depth = 0.33) {
|
|
472
|
+
const cx = plan.view.x + plan.view.w / 2;
|
|
473
|
+
const cy = plan.view.y + plan.view.h / 2;
|
|
474
|
+
return clampShot({ x: cx + (cam.x - cx) * depth, y: cy + (cam.y - cy) * depth, z: Math.pow(cam.z, depth) }, plan.view, plan.frame);
|
|
475
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>explainroo frame</title>
|
|
6
|
+
<style>html,body{margin:0;background:#000}canvas{display:block}</style>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<canvas id="c"></canvas>
|
|
10
|
+
<script type="module">
|
|
11
|
+
import { boot, renderAudio } from '/engine/runtime.js';
|
|
12
|
+
import { upload } from '/engine/upload.js';
|
|
13
|
+
|
|
14
|
+
const canvas = document.getElementById('c');
|
|
15
|
+
const params = new URLSearchParams(location.search);
|
|
16
|
+
const scale = Number(params.get('scale') || 1);
|
|
17
|
+
const transparent = params.get('transparent') === '1';
|
|
18
|
+
|
|
19
|
+
function describe(e) {
|
|
20
|
+
const where = e.scene ? ` (scene "${e.scene}" at ${Number(e.sceneTime).toFixed(2)}s)` : '';
|
|
21
|
+
return (e.message || String(e)) + where;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
window.explainrooReady = (async () => {
|
|
25
|
+
const { engine, audio, state } = await boot({ canvas, scale, transparent });
|
|
26
|
+
const fps = state.config.fps;
|
|
27
|
+
const sub = document.createElement('canvas');
|
|
28
|
+
sub.width = canvas.width;
|
|
29
|
+
sub.height = canvas.height;
|
|
30
|
+
const subCtx = sub.getContext('2d', { alpha: false });
|
|
31
|
+
// Draws the frame `samples` times over half a frame's time around T and
|
|
32
|
+
// averages them, like a film camera's shutter. The samples stay inside
|
|
33
|
+
// the frame's scene, so a cut never blends with the scene before it. Each
|
|
34
|
+
// sample loads its own clip frames, so the result does not depend on what
|
|
35
|
+
// was drawn before.
|
|
36
|
+
const blurFrame = async (T, samples) => {
|
|
37
|
+
const ctx = canvas.getContext('2d');
|
|
38
|
+
const sc = state.timeline.scenes[engine.sceneAt(T)];
|
|
39
|
+
const lo = sc.start;
|
|
40
|
+
const hi = Math.min(sc.start + sc.dur, state.timeline.duration) - 1e-4;
|
|
41
|
+
for (let k = 0; k < samples; k++) {
|
|
42
|
+
const t = Math.min(hi, Math.max(lo, T + ((k + 0.5) / samples - 0.5) * (0.5 / fps)));
|
|
43
|
+
if (engine.clips.size) await engine.prepareFrame(t, subCtx);
|
|
44
|
+
engine.renderFrame(t, subCtx);
|
|
45
|
+
ctx.save();
|
|
46
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
47
|
+
ctx.globalAlpha = k === 0 ? 1 : 1 / (k + 1);
|
|
48
|
+
ctx.drawImage(sub, 0, 0);
|
|
49
|
+
ctx.restore();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
window.explainroo = {
|
|
53
|
+
info() {
|
|
54
|
+
return { duration: state.timeline.duration, frames: state.timeline.frames, fps, width: canvas.width, height: canvas.height, scenes: state.timeline.scenes.map((s) => ({ id: s.id, start: s.start, dur: s.dur })) };
|
|
55
|
+
},
|
|
56
|
+
// The automatic camera's plan for each scene it films.
|
|
57
|
+
cameras() {
|
|
58
|
+
return Object.fromEntries([...engine.cameraPlans].map(([i, p]) => [state.timeline.scenes[i].id, { start: p.start, moves: p.moves.map(({ start, dur, from, to }) => ({ start, dur, from, to })), elements: p.elements, view: p.view }]));
|
|
59
|
+
},
|
|
60
|
+
// samples > 1 adds motion blur: a frame in which something moves is
|
|
61
|
+
// drawn that many times across half a frame's time and averaged, like a
|
|
62
|
+
// film camera's shutter. Still frames are drawn once.
|
|
63
|
+
async frames(list, quality, samples = 1) {
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const f of list) {
|
|
66
|
+
try {
|
|
67
|
+
if (engine.clips.size) await engine.prepareFrame(f / fps);
|
|
68
|
+
engine.moving = false;
|
|
69
|
+
engine.renderFrame(f / fps);
|
|
70
|
+
if (samples > 1 && engine.moving && !transparent) await blurFrame(f / fps, samples);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
throw new Error(describe(e));
|
|
73
|
+
}
|
|
74
|
+
out.push(transparent ? canvas.toDataURL('image/png').slice(22) : canvas.toDataURL('image/jpeg', quality).slice(23));
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
},
|
|
78
|
+
async stills(list, { type = 'image/png', quality, captions = true } = {}) {
|
|
79
|
+
for (const { T, path } of list) {
|
|
80
|
+
try {
|
|
81
|
+
if (engine.clips.size) await engine.prepareFrame(T);
|
|
82
|
+
engine.renderFrame(T, undefined, { captions });
|
|
83
|
+
} catch (e) {
|
|
84
|
+
throw new Error(describe(e));
|
|
85
|
+
}
|
|
86
|
+
const blob = await new Promise((r) => canvas.toBlob(r, type, quality));
|
|
87
|
+
await upload(path, blob);
|
|
88
|
+
}
|
|
89
|
+
return list.length;
|
|
90
|
+
},
|
|
91
|
+
async sheet({ times, labels, cols, thumb, path }) {
|
|
92
|
+
const tw = thumb;
|
|
93
|
+
const th = Math.round((tw * canvas.height) / canvas.width);
|
|
94
|
+
const labelH = 30;
|
|
95
|
+
const rows = Math.ceil(times.length / cols);
|
|
96
|
+
const sheet = document.createElement('canvas');
|
|
97
|
+
sheet.width = cols * (tw + 8) + 8;
|
|
98
|
+
sheet.height = rows * (th + labelH + 8) + 8;
|
|
99
|
+
const g = sheet.getContext('2d');
|
|
100
|
+
g.fillStyle = '#16181d';
|
|
101
|
+
g.fillRect(0, 0, sheet.width, sheet.height);
|
|
102
|
+
g.font = '600 17px Inter, sans-serif';
|
|
103
|
+
g.textBaseline = 'middle';
|
|
104
|
+
for (let i = 0; i < times.length; i++) {
|
|
105
|
+
try {
|
|
106
|
+
if (engine.clips.size) await engine.prepareFrame(times[i]);
|
|
107
|
+
engine.renderFrame(times[i]);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
throw new Error(describe(e));
|
|
110
|
+
}
|
|
111
|
+
const x = 8 + (i % cols) * (tw + 8);
|
|
112
|
+
const y = 8 + Math.floor(i / cols) * (th + labelH + 8);
|
|
113
|
+
g.drawImage(canvas, x, y, tw, th);
|
|
114
|
+
g.fillStyle = '#e6e9ef';
|
|
115
|
+
g.fillText(labels[i], x + 4, y + th + labelH / 2);
|
|
116
|
+
}
|
|
117
|
+
const blob = await new Promise((r) => sheet.toBlob(r, 'image/jpeg', 0.88));
|
|
118
|
+
await upload(path, blob);
|
|
119
|
+
return { width: sheet.width, height: sheet.height };
|
|
120
|
+
},
|
|
121
|
+
check(step, viewWidth) {
|
|
122
|
+
return engine.check(step, viewWidth);
|
|
123
|
+
},
|
|
124
|
+
async soundtrack(path) {
|
|
125
|
+
const { buffer, cues, errors } = await renderAudio(engine, audio);
|
|
126
|
+
if (errors.length) throw new Error(errors.map((e) => `scene "${e.scene}" at ${e.t.toFixed(2)}s: ${e.message}`).join('\n'));
|
|
127
|
+
await upload(path, audio.encodeWav(buffer));
|
|
128
|
+
return { cues: cues.length, duration: buffer.duration };
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
return true;
|
|
132
|
+
})();
|
|
133
|
+
</script>
|
|
134
|
+
</body>
|
|
135
|
+
</html>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucide Icons and Contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
The following Lucide icons are derived from the Feather project:
|
|
20
|
+
|
|
21
|
+
airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out
|
|
22
|
+
|
|
23
|
+
The MIT License (MIT) (for the icons listed above)
|
|
24
|
+
|
|
25
|
+
Copyright (c) 2013-present Cole Bemis
|
|
26
|
+
|
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
28
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
29
|
+
in the Software without restriction, including without limitation the rights
|
|
30
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
31
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
32
|
+
furnished to do so, subject to the following conditions:
|
|
33
|
+
|
|
34
|
+
The above copyright notice and this permission notice shall be included in all
|
|
35
|
+
copies or substantial portions of the Software.
|
|
36
|
+
|
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
38
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
39
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
40
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
41
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
43
|
+
SOFTWARE.
|