@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,690 @@
|
|
|
1
|
+
// Boots the engine in a page: loads the project state, fonts, icons, images
|
|
2
|
+
// and scenes, then exposes frame rendering, checks and the soundtrack.
|
|
3
|
+
import rough from '../vendor/rough.js';
|
|
4
|
+
import { Stage, SceneError, boundsRecorder } from './stage.js';
|
|
5
|
+
import { getTheme, makeBackground, fontString, colorOf } from './themes.js';
|
|
6
|
+
import { Pen } from './pen.js';
|
|
7
|
+
import { drawTransition } from './transitions.js';
|
|
8
|
+
import { buildPhrases, drawCaptions } from './captions.js';
|
|
9
|
+
import { hashStr, mulberry32, suggest, suggestColor, contrast, opaque, withAlpha, parseColor, rgba } from './util.js';
|
|
10
|
+
import { layoutAreas } from './layout.js';
|
|
11
|
+
import { loadClips, ClipFrames } from './clips.js';
|
|
12
|
+
import { UI } from './ui.js';
|
|
13
|
+
import { playbackTextIssue } from './readability.js';
|
|
14
|
+
import { planCamera, cameraAt, cameraMoving, continues, frameRect, backgroundShot } from './director.js';
|
|
15
|
+
|
|
16
|
+
Stage.UI = UI;
|
|
17
|
+
|
|
18
|
+
export async function fetchState() {
|
|
19
|
+
const res = await fetch('/__state.json', { cache: 'no-store' });
|
|
20
|
+
if (!res.ok) throw new Error(`could not load project state: ${res.status} ${await res.text()}`);
|
|
21
|
+
return res.json();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function loadFonts(theme, config, root = '/') {
|
|
25
|
+
const manifest = await (await fetch(root + 'fonts/fonts.json')).json();
|
|
26
|
+
const wanted = new Set(Object.values(theme.fonts).map((f) => f.family));
|
|
27
|
+
// The UI kit (s.ui) draws app screens in Inter and headlines in Instrument Serif.
|
|
28
|
+
wanted.add('Inter');
|
|
29
|
+
wanted.add('Instrument Serif');
|
|
30
|
+
wanted.add('Noto Sans Devanagari');
|
|
31
|
+
const faces = [];
|
|
32
|
+
const add = (family, url, o) => {
|
|
33
|
+
const face = new FontFace(family, `url(${url})`, { weight: String(o.weight ?? 400), style: o.style ?? 'normal', unicodeRange: o.unicodeRange, display: 'block' });
|
|
34
|
+
document.fonts.add(face);
|
|
35
|
+
faces.push(face.load().catch(() => {
|
|
36
|
+
throw new Error(`font "${family}" from ${url} could not be loaded`);
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
39
|
+
for (const f of manifest) if (root === './' || wanted.has(f.family)) add(f.family, `${root}fonts/${f.file}`, f);
|
|
40
|
+
if (root !== './' && ['ja', 'zh'].includes(config.language)) {
|
|
41
|
+
const family = config.language === 'ja' ? 'Noto Sans JP' : 'Noto Sans SC';
|
|
42
|
+
for (const weight of [400, 700]) add(family, `/language-fonts/${config.language}-${weight}.otf`, { weight });
|
|
43
|
+
}
|
|
44
|
+
// A video's own fonts, from "fonts" in video.json.
|
|
45
|
+
for (const f of config.fonts || []) add(f.family, root + 'project/' + f.src.split('/').map(encodeURIComponent).join('/'), f);
|
|
46
|
+
await Promise.all(faces);
|
|
47
|
+
// Make sure every weight used by the theme is ready before the first frame.
|
|
48
|
+
await Promise.all(Object.values(theme.fonts).map((f) => document.fonts.load(`${f.weight} 40px "${f.family}"`)));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function loadImages(assets, root = '/') {
|
|
52
|
+
const images = new Map();
|
|
53
|
+
await Promise.all(
|
|
54
|
+
assets
|
|
55
|
+
.filter((a) => /\.(png|jpe?g|webp|gif|svg)$/i.test(a))
|
|
56
|
+
.map(async (a) => {
|
|
57
|
+
const img = new Image();
|
|
58
|
+
img.src = root + 'project/' + a.split('/').map(encodeURIComponent).join('/');
|
|
59
|
+
await img.decode().catch(() => {
|
|
60
|
+
throw new Error(`image ${a} could not be decoded`);
|
|
61
|
+
});
|
|
62
|
+
images.set(a, img);
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
return images;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class Engine {
|
|
69
|
+
constructor({ canvas, state, scenes, icons, images, clips = new Map(), scale = 1, transparent = false, sfxNames = [] }) {
|
|
70
|
+
const { config, timeline } = state;
|
|
71
|
+
this.state = state;
|
|
72
|
+
this.config = config;
|
|
73
|
+
this.timeline = timeline;
|
|
74
|
+
this.scenesModule = scenes;
|
|
75
|
+
this.W = config.width;
|
|
76
|
+
this.H = config.height;
|
|
77
|
+
const areas = layoutAreas(config, this.W, this.H);
|
|
78
|
+
this.safeArea = areas.safe;
|
|
79
|
+
this.captionArea = areas.captions;
|
|
80
|
+
this.captionBand = areas.band;
|
|
81
|
+
this.appArea = areas.app;
|
|
82
|
+
this.fps = config.fps;
|
|
83
|
+
this.scale = scale;
|
|
84
|
+
this.transparent = transparent;
|
|
85
|
+
this.theme = getTheme(config.theme, config.look);
|
|
86
|
+
this.pen = new Pen({ rough, theme: this.theme });
|
|
87
|
+
this.icons = icons;
|
|
88
|
+
this.iconCache = new Map();
|
|
89
|
+
this.mathCache = new Map();
|
|
90
|
+
this.lateMath = new Map();
|
|
91
|
+
this.collectingMath = false;
|
|
92
|
+
this.images = images;
|
|
93
|
+
this.clips = clips;
|
|
94
|
+
this.clipFrames = new ClipFrames();
|
|
95
|
+
this.clipProbe = null;
|
|
96
|
+
this.sfxNames = sfxNames;
|
|
97
|
+
this.sfxMode = config.sfx;
|
|
98
|
+
this.recorder = null;
|
|
99
|
+
this.textLog = null;
|
|
100
|
+
this.errors = [];
|
|
101
|
+
this.canvas = canvas;
|
|
102
|
+
// H.264 needs even dimensions.
|
|
103
|
+
canvas.width = 2 * Math.round((this.W * scale) / 2);
|
|
104
|
+
canvas.height = 2 * Math.round((this.H * scale) / 2);
|
|
105
|
+
this.ctx = canvas.getContext('2d', { alpha: transparent });
|
|
106
|
+
this.background = transparent ? null : makeBackground(this.theme, this.W, this.H, config.seed);
|
|
107
|
+
this.phrases = config.captions ? buildPhrases(timeline, this.H > this.W ? 28 : 46) : [];
|
|
108
|
+
this.buffers = [0, 1].map(() => {
|
|
109
|
+
const c = document.createElement('canvas');
|
|
110
|
+
c.width = canvas.width;
|
|
111
|
+
c.height = canvas.height;
|
|
112
|
+
return c;
|
|
113
|
+
});
|
|
114
|
+
const ids = new Set(timeline.scenes.map((s) => s.id));
|
|
115
|
+
this.extraScenes = Object.keys(scenes).filter((k) => !ids.has(k));
|
|
116
|
+
for (const sc of timeline.scenes) {
|
|
117
|
+
if (typeof scenes[sc.id] !== 'function') {
|
|
118
|
+
const have = Object.keys(scenes);
|
|
119
|
+
throw new SceneError(`scenes.js has no function for scene "${sc.id}".${have.length ? ' Close names: ' + suggest(sc.id, have, 3).join(', ') : ''}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
image(src) {
|
|
125
|
+
const key = src.replace(/^\.?\//, '');
|
|
126
|
+
const img = this.images.get(key) || this.images.get('assets/' + key);
|
|
127
|
+
if (!img) throw new SceneError(`image "${src}" not found. Put it in the project's assets/ folder. Available: ${[...this.images.keys()].join(', ') || 'none'}`);
|
|
128
|
+
return img;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
clip(src) {
|
|
132
|
+
const key = src.replace(/^\.?\//, '');
|
|
133
|
+
const clip = this.clips.get(key) || this.clips.get('assets/' + key);
|
|
134
|
+
if (!clip) throw new SceneError(`clip "${src}" not found. Put an MP4, MOV, WebM or M4V in the project's assets/ folder. Available: ${[...this.clips.keys()].join(', ') || 'none'}`);
|
|
135
|
+
return clip;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
clipImage(clip, frame) {
|
|
139
|
+
if (this.recorder) return null;
|
|
140
|
+
if (clip.video) {
|
|
141
|
+
const video = clip.video;
|
|
142
|
+
const time = Math.min(frame / clip.fps, Math.max(0, clip.duration - 0.04));
|
|
143
|
+
if (!video.seeking && Math.abs(video.currentTime - time) > 0.04) video.currentTime = time;
|
|
144
|
+
return video.readyState >= 2 ? video : null;
|
|
145
|
+
}
|
|
146
|
+
const key = this.clipFrames.key(clip, frame);
|
|
147
|
+
if (this.clipProbe) {
|
|
148
|
+
this.clipProbe.set(key, { clip, frame });
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const img = this.clipFrames.get(clip, frame);
|
|
152
|
+
if (img) return img;
|
|
153
|
+
if (this.clipFrames.errors.has(key)) throw this.clipFrames.errors.get(key);
|
|
154
|
+
this.clipFrames.load(clip, frame).then(() => {
|
|
155
|
+
this.clipFrames.trim();
|
|
156
|
+
this.onClipFrame?.();
|
|
157
|
+
}).catch((e) => {
|
|
158
|
+
this.clipFrames.errors.set(key, e);
|
|
159
|
+
this.onClipFrame?.();
|
|
160
|
+
});
|
|
161
|
+
return this.clipFrames.nearest(clip, frame);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Loads the clip frames that time T shows. It finds them by drawing the
|
|
165
|
+
// frame on `ctx`. Motion blur passes a spare canvas here, so the picture it
|
|
166
|
+
// is adding up on the main canvas stays as it is.
|
|
167
|
+
async prepareFrame(T, ctx = this.ctx) {
|
|
168
|
+
if (!this.clips.size) return;
|
|
169
|
+
const needed = new Map();
|
|
170
|
+
this.clipProbe = needed;
|
|
171
|
+
try { this.renderFrame(T, ctx); }
|
|
172
|
+
finally { this.clipProbe = null; }
|
|
173
|
+
await Promise.all([...needed.values()].map(({ clip, frame }) => this.clipFrames.load(clip, frame)));
|
|
174
|
+
this.clipFrames.trim(new Set(needed.keys()));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
mathData(tex, display, scene, t) {
|
|
178
|
+
const key = JSON.stringify([tex, display]);
|
|
179
|
+
if (this.staticMath) return this.staticMath[key] || { error: 'formula missing from the player. Export the project again.' };
|
|
180
|
+
if (this.collectingMath) {
|
|
181
|
+
if (!this.mathCache.has(key)) this.mathCache.set(key, null);
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
if (!this.mathCache.has(key)) {
|
|
185
|
+
this.lateMath.set(key, { scene, t, tex });
|
|
186
|
+
// Late formulas still draw synchronously, including the frame that finds them.
|
|
187
|
+
const req = new XMLHttpRequest();
|
|
188
|
+
req.open('GET', this.mathURL(tex, display), false);
|
|
189
|
+
req.send();
|
|
190
|
+
this.mathCache.set(key, this.mathResponse(req.status, req.responseText));
|
|
191
|
+
}
|
|
192
|
+
return this.mathCache.get(key);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
mathURL(tex, display) {
|
|
196
|
+
return `/__math?tex=${encodeURIComponent(tex)}&display=${display ? '1' : '0'}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
mathResponse(status, body) {
|
|
200
|
+
if (status !== 200) {
|
|
201
|
+
try { return { error: JSON.parse(body).error || body }; } catch { return { error: body }; }
|
|
202
|
+
}
|
|
203
|
+
return JSON.parse(body);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async loadMath() {
|
|
207
|
+
this.collectingMath = true;
|
|
208
|
+
try {
|
|
209
|
+
for (const sc of this.timeline.scenes) {
|
|
210
|
+
// Scene errors belong to check or render, as they do without formulas.
|
|
211
|
+
try { this.drawScene(this.ctx, sc.index, 0); } catch {}
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
this.collectingMath = false;
|
|
215
|
+
}
|
|
216
|
+
await Promise.all([...this.mathCache.keys()].map(async key => {
|
|
217
|
+
const [tex, display] = JSON.parse(key);
|
|
218
|
+
const res = await fetch(this.mathURL(tex, display));
|
|
219
|
+
this.mathCache.set(key, this.mathResponse(res.status, await res.text()));
|
|
220
|
+
}));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
sceneAt(T) {
|
|
224
|
+
const list = this.timeline.scenes;
|
|
225
|
+
let i = list.length - 1;
|
|
226
|
+
while (i > 0 && T < list[i].start) i--;
|
|
227
|
+
return i;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
transitionFor(sc) {
|
|
231
|
+
// A scene that goes on with the picture before it fades into it. A wipe
|
|
232
|
+
// or a slide would break that picture apart.
|
|
233
|
+
if (sc.transition === 'auto' && this.continued?.has(sc.index)) return 'fade';
|
|
234
|
+
const name = sc.transition === 'auto' ? this.theme.transition : sc.transition;
|
|
235
|
+
if (this.transparent && ['slide', 'wipe', 'brush'].includes(name)) return 'fade';
|
|
236
|
+
return name || 'fade';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
drawScene(ctx, index, t) {
|
|
240
|
+
const sc = this.timeline.scenes[index];
|
|
241
|
+
if (this.transparent) {
|
|
242
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
243
|
+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
244
|
+
}
|
|
245
|
+
ctx.setTransform(this.scale, 0, 0, this.scale, 0, 0);
|
|
246
|
+
ctx.globalAlpha = 1;
|
|
247
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
248
|
+
const plan = !this.planning && !this.cameraOff ? this.cameraPlans?.get(index) : null;
|
|
249
|
+
// A scene drawn past its end, while the next one comes in, keeps the
|
|
250
|
+
// camera where it stopped, so a continued picture stays on top of itself.
|
|
251
|
+
const cam = plan ? cameraAt(plan, Math.min(t, plan.dur)) : null;
|
|
252
|
+
if (this.background) {
|
|
253
|
+
if (plan) {
|
|
254
|
+
// The background moves a third as much as the camera, so it looks
|
|
255
|
+
// further away.
|
|
256
|
+
const bg = backgroundShot(plan, cam);
|
|
257
|
+
const v = plan.view;
|
|
258
|
+
ctx.save();
|
|
259
|
+
ctx.translate(v.x + v.w / 2, v.y + v.h / 2);
|
|
260
|
+
ctx.scale(bg.z, bg.z);
|
|
261
|
+
ctx.translate(-bg.x, -bg.y);
|
|
262
|
+
ctx.drawImage(this.background, 0, 0, this.W, this.H);
|
|
263
|
+
ctx.restore();
|
|
264
|
+
} else ctx.drawImage(this.background, 0, 0, this.W, this.H);
|
|
265
|
+
}
|
|
266
|
+
if (this.config.boil > 0) this.pen.seedOffset = Math.floor((sc.start + t) * this.config.boil) % 997;
|
|
267
|
+
const saved = Math.random;
|
|
268
|
+
Math.random = mulberry32(hashStr(`random:${sc.id}`));
|
|
269
|
+
ctx.save();
|
|
270
|
+
// The automatic camera films the scene unless the scene moves its own.
|
|
271
|
+
if (plan) {
|
|
272
|
+
const v = plan.view;
|
|
273
|
+
ctx.translate(v.x + v.w / 2, v.y + v.h / 2);
|
|
274
|
+
ctx.scale(cam.z, cam.z);
|
|
275
|
+
ctx.translate(-cam.x, -cam.y);
|
|
276
|
+
if (cameraMoving(plan, t)) this.moving = true;
|
|
277
|
+
}
|
|
278
|
+
const s = new Stage(this, ctx, sc, t);
|
|
279
|
+
// While the camera is planned, anything a scene draws straight on s.ctx
|
|
280
|
+
// is measured as one block that the camera must not cut.
|
|
281
|
+
let raw = null;
|
|
282
|
+
let api = s;
|
|
283
|
+
if (this.planning && this.layoutLog) {
|
|
284
|
+
raw = boundsRecorder(ctx, this.scale, { W: this.W, H: this.H });
|
|
285
|
+
// The UI kit draws on the same recorder; its panels and headlines
|
|
286
|
+
// still report their own times.
|
|
287
|
+
let ui = null;
|
|
288
|
+
api = new Proxy(s, {
|
|
289
|
+
get: (target, prop) => {
|
|
290
|
+
if (prop === 'ctx') return raw.proxy;
|
|
291
|
+
if (prop === 'ui') return (ui ??= new Stage.UI(api));
|
|
292
|
+
const v = target[prop];
|
|
293
|
+
return typeof v === 'function' ? v.bind(target) : v;
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
this.scenesModule[sc.id](api);
|
|
299
|
+
const ink = raw?.box();
|
|
300
|
+
if (ink) this.layoutLog.push({ kind: 'ink', at: 0, out: Infinity, box: ink });
|
|
301
|
+
} catch (e) {
|
|
302
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
303
|
+
err.scene = sc.id;
|
|
304
|
+
err.sceneTime = t;
|
|
305
|
+
throw err;
|
|
306
|
+
} finally {
|
|
307
|
+
Math.random = saved;
|
|
308
|
+
ctx.restore();
|
|
309
|
+
}
|
|
310
|
+
return s;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Measures every scene once and plans its camera. A scene that continues
|
|
314
|
+
// the picture of the scene before it starts where that camera stopped,
|
|
315
|
+
// and the two dissolve into each other. Scene errors are left to check
|
|
316
|
+
// and render, which report them with their time.
|
|
317
|
+
planCameras() {
|
|
318
|
+
this.cameraPlans = new Map();
|
|
319
|
+
this.continued = new Set();
|
|
320
|
+
if (this.config.camera === 'still') return;
|
|
321
|
+
const scratch = document.createElement('canvas');
|
|
322
|
+
scratch.width = 16;
|
|
323
|
+
scratch.height = 16;
|
|
324
|
+
const ctx = scratch.getContext('2d');
|
|
325
|
+
const view = this.safeArea;
|
|
326
|
+
const frame = { W: this.W, H: this.H };
|
|
327
|
+
// The part of the frame that captions and app overlays leave free.
|
|
328
|
+
const usable = { x0: 0, y0: 0, x1: this.W, y1: this.H };
|
|
329
|
+
if (this.appArea) Object.assign(usable, { x0: this.appArea.left, y0: this.appArea.top, x1: this.appArea.right, y1: this.appArea.bottom });
|
|
330
|
+
if (this.captionBand) usable.y1 = Math.min(usable.y1, this.captionBand.top);
|
|
331
|
+
const layouts = this.timeline.scenes.map((sc) => {
|
|
332
|
+
this.layoutLog = [];
|
|
333
|
+
this.planning = true;
|
|
334
|
+
this.manualCamera = false;
|
|
335
|
+
try {
|
|
336
|
+
this.drawScene(ctx, sc.index, Math.max(0, sc.dur - 0.01));
|
|
337
|
+
return this.manualCamera ? null : this.layoutLog.map((e) => ({ ...e, out: e.out ?? Infinity }));
|
|
338
|
+
} catch {
|
|
339
|
+
return null;
|
|
340
|
+
} finally {
|
|
341
|
+
this.layoutLog = null;
|
|
342
|
+
this.planning = false;
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
this.timeline.scenes.forEach((sc, i) => {
|
|
346
|
+
if (!layouts[i]) return;
|
|
347
|
+
const prev = i > 0 ? this.cameraPlans.get(i - 1) : null;
|
|
348
|
+
const linked = prev && layouts[i - 1] && sc.transition !== 'none' && continues(layouts[i - 1], layouts[i], frameRect(cameraAt(prev, prev.dur), view, frame));
|
|
349
|
+
const voiceEnd = sc.voice ? sc.lead + sc.voice.dur : null;
|
|
350
|
+
const still = linked ? Math.min(this.timeline.transitionSeconds, sc.dur * 0.5) : 0;
|
|
351
|
+
const plan = planCamera(layouts[i], { dur: sc.dur, view, frame, pace: this.timeline.pace || 1, voiceEnd, from: linked ? cameraAt(prev, prev.dur) : null, still, usable });
|
|
352
|
+
if (!plan) return;
|
|
353
|
+
this.cameraPlans.set(i, plan);
|
|
354
|
+
if (linked) this.continued.add(i);
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
renderFrame(T, ctx = this.ctx, { captions = true } = {}) {
|
|
359
|
+
const i = this.sceneAt(T);
|
|
360
|
+
const sc = this.timeline.scenes[i];
|
|
361
|
+
const t = T - sc.start;
|
|
362
|
+
const trName = this.transitionFor(sc);
|
|
363
|
+
const trDur = Math.min(this.timeline.transitionSeconds, sc.dur * 0.5);
|
|
364
|
+
if (i > 0 && sc.transition !== 'none' && trName !== 'cut' && t < trDur) {
|
|
365
|
+
const prev = this.timeline.scenes[i - 1];
|
|
366
|
+
const [a, b] = this.buffers;
|
|
367
|
+
if (this.transparent) {
|
|
368
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
369
|
+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
370
|
+
}
|
|
371
|
+
this.moving = true;
|
|
372
|
+
this.drawScene(a.getContext('2d', { alpha: this.transparent }), i - 1, T - prev.start);
|
|
373
|
+
this.drawScene(b.getContext('2d', { alpha: this.transparent }), i, t);
|
|
374
|
+
drawTransition(trName, ctx, a, b, t / trDur, this.W, this.H, hashStr(sc.id), this.transparent);
|
|
375
|
+
} else {
|
|
376
|
+
this.drawScene(ctx, i, t);
|
|
377
|
+
}
|
|
378
|
+
if (captions && this.phrases.length) {
|
|
379
|
+
ctx.setTransform(this.scale, 0, 0, this.scale, 0, 0);
|
|
380
|
+
drawCaptions(ctx, this.phrases, T, this.theme, this.W, this.H, this.captionArea);
|
|
381
|
+
}
|
|
382
|
+
if (this.config.watermark) {
|
|
383
|
+
ctx.setTransform(this.scale, 0, 0, this.scale, 0, 0);
|
|
384
|
+
this.drawWatermark(ctx, this.config.watermark);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Small text in the bottom right corner of every frame, on a soft backing
|
|
389
|
+
// so it stays readable over any scene. Platform formats cover that corner
|
|
390
|
+
// with the app's own buttons, so there it sits at the top right of the part
|
|
391
|
+
// of the frame the app leaves free.
|
|
392
|
+
drawWatermark(ctx, text) {
|
|
393
|
+
const th = this.theme;
|
|
394
|
+
const m = Math.min(this.W, this.H);
|
|
395
|
+
const size = Math.round(m * 0.028);
|
|
396
|
+
const margin = Math.round(m * 0.028);
|
|
397
|
+
ctx.save();
|
|
398
|
+
ctx.globalAlpha = 1;
|
|
399
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
400
|
+
ctx.font = fontString(th, 'body', size, th.fonts.body.family === 'Inter' ? 600 : th.fonts.body.family === 'Kalam' ? 700 : undefined);
|
|
401
|
+
ctx.textAlign = 'right';
|
|
402
|
+
ctx.textBaseline = 'middle';
|
|
403
|
+
const w = ctx.measureText(text).width;
|
|
404
|
+
const padX = size * 0.6;
|
|
405
|
+
const h = size * 1.6;
|
|
406
|
+
const x = this.appArea ? this.appArea.right : this.W - margin;
|
|
407
|
+
const y = this.appArea ? this.appArea.top + h / 2 : this.H - margin - h / 2;
|
|
408
|
+
ctx.fillStyle = withAlpha(th.bg, 0.72);
|
|
409
|
+
ctx.beginPath();
|
|
410
|
+
ctx.roundRect(x - w - padX * 2, y - h / 2, w + padX * 2, h, h / 2);
|
|
411
|
+
ctx.fill();
|
|
412
|
+
ctx.fillStyle = withAlpha(th.ink, 0.82);
|
|
413
|
+
ctx.fillText(text, x - padX, y + size * 0.04);
|
|
414
|
+
ctx.restore();
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Runs every scene over its duration without keeping pixels, collecting the
|
|
418
|
+
// elements that enter (for sound effects) and any scene errors.
|
|
419
|
+
collectEvents(step = 0.1) {
|
|
420
|
+
const scratch = document.createElement('canvas');
|
|
421
|
+
const save = { scale: this.scale };
|
|
422
|
+
this.scale = 0.25;
|
|
423
|
+
scratch.width = Math.round(this.W * 0.25);
|
|
424
|
+
scratch.height = Math.round(this.H * 0.25);
|
|
425
|
+
const ctx = scratch.getContext('2d', { alpha: false });
|
|
426
|
+
const events = [];
|
|
427
|
+
const errors = [];
|
|
428
|
+
try {
|
|
429
|
+
for (const sc of this.timeline.scenes) {
|
|
430
|
+
const seen = new Map();
|
|
431
|
+
this.recorder = { push: (e) => { if (!seen.has(e.key)) seen.set(e.key, e); } };
|
|
432
|
+
const n = Math.ceil(sc.dur / step);
|
|
433
|
+
for (let k = 0; k <= n; k++) {
|
|
434
|
+
const t = Math.min(sc.dur, k * step);
|
|
435
|
+
try {
|
|
436
|
+
this.drawScene(ctx, sc.index, t);
|
|
437
|
+
} catch (e) {
|
|
438
|
+
errors.push({ scene: sc.id, t, message: e.message });
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
for (const e of seen.values()) {
|
|
443
|
+
if (e.at > sc.dur + 0.001) continue;
|
|
444
|
+
events.push({ ...e, scene: sc.id, T: sc.start + e.at });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
} finally {
|
|
448
|
+
this.recorder = null;
|
|
449
|
+
this.scale = save.scale;
|
|
450
|
+
}
|
|
451
|
+
return { events, errors };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Sound cues for the soundtrack: element entrances, explicit s.sfx() calls
|
|
455
|
+
// and scene transitions, thinned so effects never pile up.
|
|
456
|
+
sfxCues() {
|
|
457
|
+
if (this.config.sfx === false && !this.clips.size) return { cues: [], clips: [], errors: [] };
|
|
458
|
+
const { events, errors } = this.collectEvents();
|
|
459
|
+
const clips = events.filter((e) => e.kind === 'clip' && e.volume > 0 && e.speed === 1 && e.hasAudio).map((e) => ({ ...e, start: e.T, end: e.T + e.end - e.at }));
|
|
460
|
+
if (this.config.sfx === false) return { cues: [], clips, errors };
|
|
461
|
+
const cues = [];
|
|
462
|
+
for (const e of events) {
|
|
463
|
+
if (!e.sfx) continue;
|
|
464
|
+
if (!this.sfxNames.includes(e.sfx)) continue;
|
|
465
|
+
const cue = { name: e.sfx, t: e.T, gain: e.gain ?? 1, explicit: !!e.explicit };
|
|
466
|
+
if (e.pitch !== undefined) cue.pitch = e.pitch;
|
|
467
|
+
if (['scribble', 'chalk', 'type', 'rise'].includes(e.sfx)) cue.dur = Math.max(0.15, Math.min(e.dur || 0.4, 3));
|
|
468
|
+
cues.push(cue);
|
|
469
|
+
}
|
|
470
|
+
for (const sc of this.timeline.scenes) {
|
|
471
|
+
if (sc.index === 0 || sc.transition === 'none') continue;
|
|
472
|
+
const name = this.transitionFor(sc);
|
|
473
|
+
if (name === 'cut') continue;
|
|
474
|
+
const s = this.theme.sfx.transition;
|
|
475
|
+
if (s) cues.push({ name: s, t: Math.max(0, sc.start - 0.05), gain: 0.8, explicit: true });
|
|
476
|
+
}
|
|
477
|
+
cues.sort((a, b) => a.t - b.t);
|
|
478
|
+
const out = [];
|
|
479
|
+
const last = new Map();
|
|
480
|
+
let windowStart = 0;
|
|
481
|
+
let inWindow = 0;
|
|
482
|
+
for (const c of cues) {
|
|
483
|
+
const prev = last.get(c.name);
|
|
484
|
+
if (!c.explicit && prev !== undefined && c.t - prev < (c.name === 'tick' ? 0.045 : 0.12)) continue;
|
|
485
|
+
if (c.t - windowStart > 0.5) {
|
|
486
|
+
windowStart = c.t;
|
|
487
|
+
inWindow = 0;
|
|
488
|
+
}
|
|
489
|
+
if (!c.explicit && inWindow >= 3 && c.name !== 'tick') continue;
|
|
490
|
+
inWindow++;
|
|
491
|
+
last.set(c.name, c.t);
|
|
492
|
+
out.push(c);
|
|
493
|
+
}
|
|
494
|
+
return { cues: out, clips, errors };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Samples every scene and reports layout problems.
|
|
498
|
+
check(step = 0.25, viewWidth = null) {
|
|
499
|
+
// check looks at the layout as written. The automatic camera only ever
|
|
500
|
+
// moves in, which makes text larger, and it may leave the edges out of
|
|
501
|
+
// a close-up on purpose.
|
|
502
|
+
this.cameraOff = true;
|
|
503
|
+
try {
|
|
504
|
+
return this._check(step, viewWidth);
|
|
505
|
+
} finally {
|
|
506
|
+
this.cameraOff = false;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
_check(step, viewWidth) {
|
|
511
|
+
const issues = [];
|
|
512
|
+
const add = (level, scene, t, message) => issues.push({ level, scene, t: t === null ? null : Math.round(t * 100) / 100, message });
|
|
513
|
+
if (this.config.look) {
|
|
514
|
+
for (const [key, min] of [['ink', 4.5], ['muted', 3]]) {
|
|
515
|
+
if (contrast(this.theme[key], this.theme.bg) < min) add('warn', null, null, `${this.config.theme}: ${key} has low contrast against bg; use colors with a contrast of at least ${min}`);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
for (const id of this.extraScenes) add('warn', id, null, `scenes.js has a function "${id}" but script.md has no scene with that id, so it is never shown`);
|
|
519
|
+
const scratch = document.createElement('canvas');
|
|
520
|
+
scratch.width = this.canvas.width;
|
|
521
|
+
scratch.height = this.canvas.height;
|
|
522
|
+
const ctx = scratch.getContext('2d', { alpha: false });
|
|
523
|
+
const { W, H } = this;
|
|
524
|
+
const margin = Math.round(Math.min(W, H) * 0.035);
|
|
525
|
+
const minSize = Math.round(Math.min(W, H) * 0.022);
|
|
526
|
+
const uiMinSize = Math.round(Math.min(W, H) * 0.015);
|
|
527
|
+
const colors = Object.fromEntries(['ink', 'accent', ...Object.keys(this.theme.colors)].map(name => [name, colorOf(this.theme, name)]));
|
|
528
|
+
let colorCtx;
|
|
529
|
+
const cssColor = color => {
|
|
530
|
+
if (parseColor(color)) return color;
|
|
531
|
+
if (!colorCtx) {
|
|
532
|
+
const canvas = document.createElement('canvas');
|
|
533
|
+
canvas.width = canvas.height = 1;
|
|
534
|
+
colorCtx = canvas.getContext('2d');
|
|
535
|
+
}
|
|
536
|
+
colorCtx.clearRect(0, 0, 1, 1);
|
|
537
|
+
colorCtx.fillStyle = '#000000';
|
|
538
|
+
colorCtx.fillStyle = color;
|
|
539
|
+
colorCtx.fillRect(0, 0, 1, 1);
|
|
540
|
+
const [r, g, b, a] = colorCtx.getImageData(0, 0, 1, 1).data;
|
|
541
|
+
return rgba({ r, g, b, a: a / 255 });
|
|
542
|
+
};
|
|
543
|
+
const reported = new Set();
|
|
544
|
+
const once = (key, fn) => {
|
|
545
|
+
if (reported.has(key)) return;
|
|
546
|
+
reported.add(key);
|
|
547
|
+
fn();
|
|
548
|
+
};
|
|
549
|
+
this.cueNotes = [];
|
|
550
|
+
for (const sc of this.timeline.scenes) {
|
|
551
|
+
const entrances = [];
|
|
552
|
+
this.recorder = { push: (e) => entrances.push(e) };
|
|
553
|
+
const n = Math.ceil(sc.dur / step);
|
|
554
|
+
for (let k = 0; k <= n; k++) {
|
|
555
|
+
const t = Math.min(sc.dur - 0.001, k * step);
|
|
556
|
+
this.textLog = [];
|
|
557
|
+
try {
|
|
558
|
+
this.drawScene(ctx, sc.index, t);
|
|
559
|
+
} catch (e) {
|
|
560
|
+
once(`err:${sc.id}:${e.message}`, () => add('error', sc.id, t, e.message));
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
for (const pr of this.textLog.filter((x) => x.problem)) once(`problem:${sc.id}:${pr.problem}`, () => add('error', sc.id, t, pr.problem));
|
|
564
|
+
const texts = this.textLog.filter((x) => !x.problem && x.alpha > 0.5);
|
|
565
|
+
for (const tx of texts) {
|
|
566
|
+
if (viewWidth !== null) {
|
|
567
|
+
const message = playbackTextIssue(tx, W, H, viewWidth);
|
|
568
|
+
if (message) once(`playback:${sc.id}:${tx.text}`, () => add('warn', sc.id, t, message));
|
|
569
|
+
}
|
|
570
|
+
const label = tx.text.length > 40 ? tx.text.slice(0, 37) + '...' : tx.text;
|
|
571
|
+
// UI kit text (s.ui) may leave the frame on purpose while the
|
|
572
|
+
// camera zooms or pans; app screens use smaller text than slides.
|
|
573
|
+
const framed = !(tx.ui && tx.zoomed);
|
|
574
|
+
if (tx.x0 < -1 || tx.y0 < -1 || tx.x1 > W + 1 || tx.y1 > H + 1) {
|
|
575
|
+
if (framed) once(`off:${sc.id}:${tx.text}`, () => add('error', sc.id, t, `"${label}" runs off the frame (${Math.round(tx.x0)},${Math.round(tx.y0)} to ${Math.round(tx.x1)},${Math.round(tx.y1)} in a ${W}x${H} frame)`));
|
|
576
|
+
} else if (this.appArea) {
|
|
577
|
+
const a = this.appArea;
|
|
578
|
+
if (framed && (tx.x0 < a.left - 2 || tx.y0 < a.top - 2 || tx.x1 > a.right + 2 || tx.y1 > a.bottom + 2)) {
|
|
579
|
+
once(`app:${sc.id}:${tx.text}`, () => add('warn', sc.id, t, `"${label}" is outside the ${this.config.format} safe area (${a.left},${a.top} to ${a.right},${a.bottom}), where the app's buttons, name or caption can cover it`));
|
|
580
|
+
}
|
|
581
|
+
} else if (framed && (tx.x0 < margin || tx.y0 < margin || tx.x1 > W - margin || tx.y1 > H - margin)) {
|
|
582
|
+
once(`edge:${sc.id}:${tx.text}`, () => add('warn', sc.id, t, `"${label}" sits closer than ${margin}px to the edge`));
|
|
583
|
+
}
|
|
584
|
+
const b = this.captionBand;
|
|
585
|
+
if (b && tx.y1 > b.top + 2 && tx.y0 < b.bottom - 2 && tx.x1 > b.left && tx.x0 < b.right) {
|
|
586
|
+
once(`cap:${sc.id}:${tx.text}`, () => add('warn', sc.id, t, `"${label}" is in the caption band (y ${Math.round(b.top)} to ${Math.round(b.bottom)}), where captions can cover it; keep content above s.safe.bottom (${Math.round(this.safeArea.bottom)})`));
|
|
587
|
+
}
|
|
588
|
+
const least = tx.ui ? uiMinSize : minSize;
|
|
589
|
+
if (!tx.block && Math.round(tx.size) < least) once(`small:${sc.id}:${tx.text}`, () => add('warn', sc.id, t, `"${label}" is ${Math.round(tx.size)}px, hard to read (use at least ${least}px)`));
|
|
590
|
+
const bg = opaque(tx.bg || this.theme.bg, this.theme.bg);
|
|
591
|
+
if (typeof tx.fg === 'string' && contrast(tx.fg, bg) < 3) once(`contrast:${sc.id}:${tx.text}`, () => {
|
|
592
|
+
const suggestion = suggestColor(cssColor(tx.fg), cssColor(bg), tx.size, Object.fromEntries(Object.entries(colors).map(([name, c]) => [name, cssColor(c)])));
|
|
593
|
+
add('warn', sc.id, t, `"${label}" has low contrast against its background (${contrast(tx.fg, bg).toFixed(1)}:1); use color: '${suggestion.color}' (${suggestion.ratio.toFixed(1)}:1)`);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
// Overlaps only count when both are fully shown, not mid-fade.
|
|
597
|
+
const solid = texts.filter((x) => x.alpha > 0.95);
|
|
598
|
+
for (let a = 0; a < solid.length; a++) {
|
|
599
|
+
for (let b = a + 1; b < solid.length; b++) {
|
|
600
|
+
const A = solid[a];
|
|
601
|
+
const B = solid[b];
|
|
602
|
+
if (A.block && B.block) continue;
|
|
603
|
+
// A dropdown or dialog (s.ui) covers what is under it.
|
|
604
|
+
if ((A.ui || 1) !== (B.ui || 1)) continue;
|
|
605
|
+
const ix = Math.min(A.x1, B.x1) - Math.max(A.x0, B.x0);
|
|
606
|
+
const iy = Math.min(A.y1, B.y1) - Math.max(A.y0, B.y0);
|
|
607
|
+
if (ix <= 4 || iy <= 4) continue;
|
|
608
|
+
const small = Math.min((A.x1 - A.x0) * (A.y1 - A.y0), (B.x1 - B.x0) * (B.y1 - B.y0)) || 1;
|
|
609
|
+
if ((ix * iy) / small > 0.15) {
|
|
610
|
+
once(`overlap:${sc.id}:${A.text}:${B.text}`, () => add('error', sc.id, t, `"${A.text.slice(0, 30)}" overlaps "${B.text.slice(0, 30)}"`));
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
this.recorder = null;
|
|
616
|
+
this.textLog = null;
|
|
617
|
+
const times = [...new Set(entrances.map((e) => Math.round(e.at * 100) / 100))].sort((a, b) => a - b);
|
|
618
|
+
for (const e of entrances) {
|
|
619
|
+
if (e.kind === 'clip') {
|
|
620
|
+
if (e.volume > 0 && e.speed !== 1) once(`clip-sound:${sc.id}:${e.key}`, () => add('warn', sc.id, e.at, `clip "${e.src}" stays silent at speed ${e.speed}; use speed: 1 for its sound`));
|
|
621
|
+
if (!e.loop && e.out - e.end > 1) once(`clip-freeze:${sc.id}:${e.key}`, () => add('hint', sc.id, e.end, `clip "${e.src}" freezes on its last frame from ${e.end.toFixed(2)}s until ${e.out.toFixed(2)}s; use loop: true or an earlier out`));
|
|
622
|
+
}
|
|
623
|
+
if (e.at > sc.dur) once(`late:${sc.id}:${e.key}`, () => add('error', sc.id, e.at, `a ${e.kind} is set to appear at ${e.at.toFixed(2)}s but the scene ends at ${sc.dur.toFixed(2)}s`));
|
|
624
|
+
}
|
|
625
|
+
if (!times.length) {
|
|
626
|
+
add('warn', sc.id, null, 'nothing enters in this scene; add at least one element with an "at" time');
|
|
627
|
+
} else {
|
|
628
|
+
const marks = [0, ...times.filter((x) => x >= 0 && x <= sc.dur), sc.dur];
|
|
629
|
+
for (let i = 1; i < marks.length; i++) {
|
|
630
|
+
const gap = marks[i] - marks[i - 1];
|
|
631
|
+
if (gap > 6) add('hint', sc.id, marks[i - 1], `no new element for ${gap.toFixed(1)}s (from ${marks[i - 1].toFixed(1)}s); consider revealing something on a cue in that stretch`);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
const repeated = new Map();
|
|
636
|
+
for (const c of this.cueNotes) repeated.set(`${c.scene}|${c.word.toLowerCase()}`, c);
|
|
637
|
+
for (const c of repeated.values()) {
|
|
638
|
+
add('hint', c.scene, null, `cue "${c.word}" means the first of the ${c.count} times it is spoken in this scene; if you meant a later one, use s.cue("${c.word}", 2) or a [#marker]`);
|
|
639
|
+
}
|
|
640
|
+
this.cueNotes = null;
|
|
641
|
+
for (const { scene, t, tex } of this.lateMath.values()) add('error', scene, t, `math "${tex}" was not collected at the start; keep TeX strings fixed and always call s.math with at and out for timing`);
|
|
642
|
+
return issues;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// Boots the engine for render, check or studio pages.
|
|
647
|
+
export async function boot({ canvas, scale = 1, transparent = false, version = '', player = false }) {
|
|
648
|
+
const root = player ? './' : '/';
|
|
649
|
+
const state = player ? await (await fetch('./state.json')).json() : await fetchState();
|
|
650
|
+
const theme = getTheme(state.config.theme, state.config.look);
|
|
651
|
+
const [icons, images, scenesMod, audio, iconTags, clips] = await Promise.all([
|
|
652
|
+
fetch(root + 'engine/icons/lucide.json').then((r) => r.json()).then((j) => {
|
|
653
|
+
// Old Lucide names keep working.
|
|
654
|
+
for (const [alias, target] of Object.entries(j.aliases || {})) if (!j.icons[alias]) j.icons[alias] = j.icons[target];
|
|
655
|
+
return j.icons;
|
|
656
|
+
}),
|
|
657
|
+
loadImages(state.assets, root),
|
|
658
|
+
import(player ? '../project/scenes.js' : `/project/scenes.js?v=${encodeURIComponent(version || state.version)}`),
|
|
659
|
+
player ? { SFX: {} } : import('./audio/soundtrack.js').catch((e) => ({ SFX: {}, loadError: e })),
|
|
660
|
+
player ? {} : fetch('/engine/icons/lucide-tags.json').then((r) => r.json()),
|
|
661
|
+
loadClips(state.assets, player ? state : null),
|
|
662
|
+
loadFonts(theme, state.config, root),
|
|
663
|
+
]);
|
|
664
|
+
const scenes = scenesMod.default;
|
|
665
|
+
if (!scenes || typeof scenes !== 'object') throw new Error('scenes.js must "export default { sceneId(s) { ... } }"');
|
|
666
|
+
const engine = new Engine({ canvas, state, scenes, icons, images, clips, scale, transparent, sfxNames: Object.keys(audio.SFX) });
|
|
667
|
+
engine.iconTags = iconTags;
|
|
668
|
+
if (player) engine.staticMath = await (await fetch('./math.json')).json();
|
|
669
|
+
else await engine.loadMath();
|
|
670
|
+
engine.planCameras();
|
|
671
|
+
return { engine, audio, state };
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// Mixes narration, music and sound effects in an OfflineAudioContext.
|
|
675
|
+
export async function renderAudio(engine, audio, { sampleRate = 48000 } = {}) {
|
|
676
|
+
if (audio.loadError) throw new Error(`audio engine failed to load: ${audio.loadError.message}`);
|
|
677
|
+
const { timeline, config } = engine.state;
|
|
678
|
+
const { cues, clips, errors } = engine.sfxCues();
|
|
679
|
+
const buffer = await audio.renderSoundtrack({
|
|
680
|
+
duration: timeline.duration,
|
|
681
|
+
sampleRate,
|
|
682
|
+
voice: timeline.scenes.filter((s) => s.voice).map((s) => ({ url: '/project/' + s.voice.url + '?v=' + engine.state.version, start: s.voice.start })),
|
|
683
|
+
voiceSpans: timeline.voiceSpans,
|
|
684
|
+
music: config.music,
|
|
685
|
+
sfx: cues,
|
|
686
|
+
clips,
|
|
687
|
+
scenes: timeline.scenes.map((s) => ({ start: s.start, dur: s.dur })),
|
|
688
|
+
});
|
|
689
|
+
return { buffer, cues, errors };
|
|
690
|
+
}
|