@open-artifact/motion-stage 0.0.0-stage → 0.12.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +69 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +155 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +208 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +97 -0
- package/src/protocol/layers.d.ts +123 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +261 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/captions.d.ts +26 -0
- package/src/runtime/captions.js +103 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +40 -0
- package/src/runtime/film.js +29 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +123 -0
- package/src/runtime/kit/camera.d.ts +40 -0
- package/src/runtime/kit/camera.js +56 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +22 -0
- package/src/runtime/kit/geometry.js +46 -0
- package/src/runtime/kit/index.d.ts +95 -0
- package/src/runtime/kit/index.js +149 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +121 -0
- package/src/runtime/layers/item.js +149 -0
- package/src/runtime/layers/parts.d.ts +50 -0
- package/src/runtime/layers/parts.js +155 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +202 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +333 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +228 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +69 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +35 -0
- package/src/runtime/recut.js +158 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +342 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +156 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +47 -0
- package/src/runtime/scenes/install.js +387 -0
- package/src/runtime/scenes/read.d.ts +15 -0
- package/src/runtime/scenes/read.js +57 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +40 -0
- package/src/runtime/score.d.ts +26 -0
- package/src/runtime/score.js +221 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +49 -0
- package/src/runtime/snapshot/serialize.js +271 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +210 -0
- package/src/runtime/video-sync.d.ts +26 -0
- package/src/runtime/video-sync.js +126 -0
- package/src/stills.d.ts +41 -0
- package/src/stills.js +253 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +216 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +29 -0
- package/src/tools/types.d.ts +69 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +5 -0
- package/src/tools.js +3 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const LONGEST_CUE = 76;
|
|
2
|
+
const fields = (value) => value !== null && typeof value === 'object' ? value : null;
|
|
3
|
+
/**
|
|
4
|
+
* The cut's narration, held to the host's limits: the page applies the same, so a line dropped here is named and
|
|
5
|
+
* not captioned.
|
|
6
|
+
*/
|
|
7
|
+
export function readNarration(narration, duration, limits) {
|
|
8
|
+
const given = fields(narration);
|
|
9
|
+
if (!given || !Array.isArray(given['lines']))
|
|
10
|
+
return null;
|
|
11
|
+
const all = given['lines'];
|
|
12
|
+
if (all.length > limits.maxLines)
|
|
13
|
+
console.error('narration has ' + all.length + ' lines; only the first ' + limits.maxLines + ' are used');
|
|
14
|
+
const lines = [];
|
|
15
|
+
all.slice(0, limits.maxLines).forEach((entry, i) => {
|
|
16
|
+
const line = fields(entry);
|
|
17
|
+
const at = line?.['at'];
|
|
18
|
+
const dur = line?.['dur'];
|
|
19
|
+
const text = line?.['text'];
|
|
20
|
+
const audio = line?.['audio'];
|
|
21
|
+
if (typeof at !== 'number' ||
|
|
22
|
+
typeof dur !== 'number' ||
|
|
23
|
+
!(at >= 0 && at < duration) ||
|
|
24
|
+
!(dur > 0 && dur <= limits.maxLineSeconds) ||
|
|
25
|
+
typeof text !== 'string' ||
|
|
26
|
+
typeof audio !== 'string' ||
|
|
27
|
+
!audio) {
|
|
28
|
+
console.error('narration line ' +
|
|
29
|
+
(i + 1) +
|
|
30
|
+
' is left out. It needs { at, text, audio, dur }: a start in seconds inside the film, the words, the name of an audio asset, and that audio’s length in seconds (' +
|
|
31
|
+
limits.maxLineSeconds +
|
|
32
|
+
' at most); at and dur as numbers');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
lines.push({ at: at, text, audio, dur });
|
|
36
|
+
});
|
|
37
|
+
const captions = given['captions'];
|
|
38
|
+
const style = fields(captions)?.['style'];
|
|
39
|
+
return {
|
|
40
|
+
lines,
|
|
41
|
+
duck: typeof given['duck'] === 'number' ? given['duck'] : null,
|
|
42
|
+
captions: captions === false ? false : typeof style === 'string' ? { style } : {},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** A line over LONGEST_CUE characters shows in parts of about equal length, split at spaces, each for its share of the line's time. */
|
|
46
|
+
export function captionCues(lines) {
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const line of lines) {
|
|
49
|
+
const words = line.text.trim().split(/\s+/);
|
|
50
|
+
const total = words.join(' ').length;
|
|
51
|
+
if (!total)
|
|
52
|
+
continue;
|
|
53
|
+
const aim = total / Math.ceil(total / LONGEST_CUE);
|
|
54
|
+
let part = '';
|
|
55
|
+
let done = 0;
|
|
56
|
+
words.forEach((word, i) => {
|
|
57
|
+
part += (part ? ' ' : '') + word;
|
|
58
|
+
const next = words[i + 1];
|
|
59
|
+
if (next !== undefined && part.length + (next.length + 1) / 2 < aim)
|
|
60
|
+
return;
|
|
61
|
+
out.push({
|
|
62
|
+
from: (line.at + (line.dur * done) / total),
|
|
63
|
+
to: (line.at + line.dur * Math.min(1, (done + part.length + 1) / total)),
|
|
64
|
+
text: part,
|
|
65
|
+
});
|
|
66
|
+
done += part.length + 1;
|
|
67
|
+
part = '';
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
/** `shortSide`, the stage's shorter side, sizes the type. What draw shows depends only on t. */
|
|
73
|
+
export function captionLayer(lines, style, shortSide, sans) {
|
|
74
|
+
const cues = captionCues(lines);
|
|
75
|
+
const box = document.createElement('div');
|
|
76
|
+
const words = document.createElement('span');
|
|
77
|
+
box.className = 'film-caption';
|
|
78
|
+
box.dir = 'auto';
|
|
79
|
+
box.style.cssText =
|
|
80
|
+
'position:absolute;left:10%;right:10%;bottom:7%;text-align:center;text-wrap:balance;pointer-events:none;z-index:2147483647;font:600 ' +
|
|
81
|
+
Math.round(shortSide * 0.042) +
|
|
82
|
+
'px/1.62 ' +
|
|
83
|
+
sans;
|
|
84
|
+
// Cloned per row, so each row's background hugs its own words.
|
|
85
|
+
words.style.cssText =
|
|
86
|
+
'padding:.2em .6em;border-radius:.3em;background:rgba(0,0,0,.7);color:#fff;-webkit-box-decoration-break:clone;box-decoration-break:clone' +
|
|
87
|
+
(style ? ';' + style : '');
|
|
88
|
+
box.appendChild(words);
|
|
89
|
+
return {
|
|
90
|
+
draw(t, stage) {
|
|
91
|
+
let cue = null;
|
|
92
|
+
for (const each of cues)
|
|
93
|
+
if (t >= each.from && t < each.to)
|
|
94
|
+
cue = each;
|
|
95
|
+
// Last in the stage, so it stays above what the cut adds.
|
|
96
|
+
if (stage.lastChild !== box)
|
|
97
|
+
stage.appendChild(box);
|
|
98
|
+
box.style.visibility = cue ? 'visible' : 'hidden';
|
|
99
|
+
if (cue && words.textContent !== cue.text)
|
|
100
|
+
words.textContent = cue.text;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ErrorPhase } from '../protocol.ts';
|
|
2
|
+
export declare const errorPhase: () => ErrorPhase;
|
|
3
|
+
export declare function setErrorPhase(next: ErrorPhase): void;
|
|
4
|
+
export declare const failedToLoad: () => boolean;
|
|
5
|
+
export declare function setLateHandler(handler: ((error: unknown) => void) | null): void;
|
|
6
|
+
/** A failure episode that ended: the next one is reported afresh. */
|
|
7
|
+
export declare function forgetErrors(): void;
|
|
8
|
+
export declare function reportError(at: ErrorPhase, error: unknown): void;
|
|
9
|
+
/** Runs `run` and gives back what it threw through the window's error event, or null. Guards nest. */
|
|
10
|
+
export declare function guarded(run: () => void): unknown;
|
|
11
|
+
/** Console lines go to the host as well, which shows them; the film's errors fail a check. */
|
|
12
|
+
export declare function captureConsole(): void;
|
|
13
|
+
export declare function catchErrors(): void;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { say } from "./port.js";
|
|
2
|
+
const LONGEST_LINE = 2000;
|
|
3
|
+
const LONGEST_MESSAGE = 600;
|
|
4
|
+
const LONGEST_STACK = 2000;
|
|
5
|
+
let phase = 'load';
|
|
6
|
+
let loadFailed = false;
|
|
7
|
+
let seen = new Set();
|
|
8
|
+
/** Set while a script runs under tryScript: what it throws is kept for the caller instead of reported. */
|
|
9
|
+
let guard = null;
|
|
10
|
+
/** In a film made of scenes, who hears what deferred code throws while the film loads, instead of the film failing. */
|
|
11
|
+
let lateTo = null;
|
|
12
|
+
export const errorPhase = () => phase;
|
|
13
|
+
export function setErrorPhase(next) {
|
|
14
|
+
phase = next;
|
|
15
|
+
}
|
|
16
|
+
export const failedToLoad = () => loadFailed;
|
|
17
|
+
export function setLateHandler(handler) {
|
|
18
|
+
lateTo = handler;
|
|
19
|
+
}
|
|
20
|
+
/** A failure episode that ended: the next one is reported afresh. */
|
|
21
|
+
export function forgetErrors() {
|
|
22
|
+
seen = new Set();
|
|
23
|
+
}
|
|
24
|
+
const messageOf = (error) => {
|
|
25
|
+
if (error instanceof Error)
|
|
26
|
+
return error.message || 'error';
|
|
27
|
+
return String(error ?? 'error');
|
|
28
|
+
};
|
|
29
|
+
export function reportError(at, error) {
|
|
30
|
+
const message = messageOf(error).slice(0, LONGEST_MESSAGE);
|
|
31
|
+
const stack = (error instanceof Error ? (error.stack ?? '') : '').slice(0, LONGEST_STACK);
|
|
32
|
+
const key = at + '|' + message;
|
|
33
|
+
if (seen.has(key))
|
|
34
|
+
return;
|
|
35
|
+
seen.add(key);
|
|
36
|
+
say({ type: 'error', phase: at, message, stack });
|
|
37
|
+
}
|
|
38
|
+
/** Runs `run` and gives back what it threw through the window's error event, or null. Guards nest. */
|
|
39
|
+
export function guarded(run) {
|
|
40
|
+
const outer = guard;
|
|
41
|
+
const mine = { error: null };
|
|
42
|
+
guard = mine;
|
|
43
|
+
try {
|
|
44
|
+
run();
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
guard = outer;
|
|
48
|
+
}
|
|
49
|
+
return mine.error;
|
|
50
|
+
}
|
|
51
|
+
function shown(part) {
|
|
52
|
+
try {
|
|
53
|
+
if (part instanceof Error)
|
|
54
|
+
return String(part.stack || part.message || part);
|
|
55
|
+
if (typeof part === 'object')
|
|
56
|
+
return JSON.stringify(part);
|
|
57
|
+
return String(part);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return String(part);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Console lines go to the host as well, which shows them; the film's errors fail a check. */
|
|
64
|
+
export function captureConsole() {
|
|
65
|
+
const levels = ['log', 'info', 'warn', 'error'];
|
|
66
|
+
for (const level of levels) {
|
|
67
|
+
const original = console[level].bind(console);
|
|
68
|
+
console[level] = (...parts) => {
|
|
69
|
+
original(...parts);
|
|
70
|
+
say({ type: 'console', level, text: parts.map(shown).join(' ').slice(0, LONGEST_LINE) });
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function catchErrors() {
|
|
75
|
+
window.addEventListener('error', (event) => {
|
|
76
|
+
const error = event.error ?? new Error(String(event.message));
|
|
77
|
+
if (guard) {
|
|
78
|
+
guard.error = error;
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (lateTo && phase === 'load') {
|
|
83
|
+
lateTo(error);
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (phase === 'load')
|
|
88
|
+
loadFailed = true;
|
|
89
|
+
reportError(phase, event.error ?? event.message);
|
|
90
|
+
});
|
|
91
|
+
window.addEventListener('unhandledrejection', (event) => {
|
|
92
|
+
if (lateTo && phase === 'load') {
|
|
93
|
+
lateTo(event.reason);
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
reportError(phase, event.reason);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FilmSeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import type { Chapter } from '../protocol.ts';
|
|
3
|
+
import type { Cut } from './film.ts';
|
|
4
|
+
/** window.CUT as the stage calls it, or why it cannot be. A width, height or fps left out takes its default. */
|
|
5
|
+
export declare function readCut(given: unknown): Cut | string;
|
|
6
|
+
/** The cut's chapters that can be read, in order, within the film. */
|
|
7
|
+
export declare function chaptersOf(cut: Pick<Cut, 'chapters' | 'duration'>): Chapter<FilmSeconds>[];
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const MOST_FILM_S = 300;
|
|
2
|
+
const MOST_CHAPTERS = 32;
|
|
3
|
+
const LONGEST_LABEL = 48;
|
|
4
|
+
const isFps = (value) => value === 24 || value === 30 || value === 60;
|
|
5
|
+
const isSide = (value) => Number.isInteger(value) && Number(value) >= 16 && Number(value) <= 4096;
|
|
6
|
+
/** window.CUT as the stage calls it, or why it cannot be. A width, height or fps left out takes its default. */
|
|
7
|
+
export function readCut(given) {
|
|
8
|
+
if (given === null || typeof given !== 'object')
|
|
9
|
+
return 'the script did not set window.CUT';
|
|
10
|
+
const field = (key) => Reflect.get(given, key);
|
|
11
|
+
const render = field('render');
|
|
12
|
+
if (typeof render !== 'function')
|
|
13
|
+
return 'window.CUT.render(t, kit) is missing';
|
|
14
|
+
const width = field('width') ?? 1920;
|
|
15
|
+
const height = field('height') ?? 1080;
|
|
16
|
+
const fps = field('fps') ?? 60;
|
|
17
|
+
const duration = field('duration');
|
|
18
|
+
if (!isSide(width) || !isSide(height))
|
|
19
|
+
return 'CUT.width/height must be integers between 16 and 4096';
|
|
20
|
+
if (!isFps(fps))
|
|
21
|
+
return 'CUT.fps must be 24, 30 or 60';
|
|
22
|
+
if (!(typeof duration === 'number' && isFinite(duration) && duration > 0 && duration <= MOST_FILM_S))
|
|
23
|
+
return 'CUT.duration must be a number of seconds in (0, 300]';
|
|
24
|
+
const init = field('init');
|
|
25
|
+
const background = field('background');
|
|
26
|
+
// The film's own object, so that a render reading `this` finds what it set.
|
|
27
|
+
return {
|
|
28
|
+
width,
|
|
29
|
+
height,
|
|
30
|
+
fps,
|
|
31
|
+
duration,
|
|
32
|
+
background: typeof background === 'string' ? background : '#ffffff',
|
|
33
|
+
chapters: field('chapters'),
|
|
34
|
+
render: (t, kit) => Reflect.apply(render, given, [t, kit]),
|
|
35
|
+
init: typeof init === 'function' ? (kit) => Reflect.apply(init, given, [kit]) : null,
|
|
36
|
+
// Read when asked, after init: a film of scenes sets its score and narration there.
|
|
37
|
+
get score() {
|
|
38
|
+
const score = field('score');
|
|
39
|
+
return typeof score === 'function' ? (builder, kit) => Reflect.apply(score, given, [builder, kit]) : null;
|
|
40
|
+
},
|
|
41
|
+
get narration() {
|
|
42
|
+
return field('narration');
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** The cut's chapters that can be read, in order, within the film. */
|
|
47
|
+
export function chaptersOf(cut) {
|
|
48
|
+
const out = [];
|
|
49
|
+
if (!Array.isArray(cut.chapters))
|
|
50
|
+
return out;
|
|
51
|
+
const given = cut.chapters;
|
|
52
|
+
for (const chapter of given) {
|
|
53
|
+
if (out.length >= MOST_CHAPTERS)
|
|
54
|
+
break;
|
|
55
|
+
if (chapter === null || typeof chapter !== 'object')
|
|
56
|
+
continue;
|
|
57
|
+
const t = Reflect.get(chapter, 't');
|
|
58
|
+
const label = Reflect.get(chapter, 'label');
|
|
59
|
+
if (typeof t === 'number' && isFinite(t) && typeof label === 'string')
|
|
60
|
+
out.push({ t: Math.max(0, Math.min(cut.duration, t)), label: label.slice(0, LONGEST_LABEL) });
|
|
61
|
+
}
|
|
62
|
+
return out.sort((a, b) => a.t - b.t);
|
|
63
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FilmSeconds } from '@open-artifact/motion-core';
|
|
2
|
+
export declare const realNow: () => number;
|
|
3
|
+
export declare const wallNow: () => number;
|
|
4
|
+
export declare const RealMutationObserver: typeof MutationObserver;
|
|
5
|
+
export declare const RealMessageChannel: typeof MessageChannel | null;
|
|
6
|
+
/** What `new Date()` says at film time 0. */
|
|
7
|
+
export declare const FILM_EPOCH_MS: number;
|
|
8
|
+
export declare const filmTime: () => FilmSeconds;
|
|
9
|
+
export declare function setFilmTime(t: FilmSeconds): void;
|
|
10
|
+
/** Math.random starts afresh from `seed` (a scene's own seed, at its own moment). */
|
|
11
|
+
export declare function reseed(seed: number): void;
|
|
12
|
+
export declare function installFilmClock(): void;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { frameSeed, rng } from "./kit/random.js";
|
|
2
|
+
/*
|
|
3
|
+
* The film clock is the only clock the film can read, and its randomness starts afresh at every moment drawn: a
|
|
4
|
+
* frame follows from t alone. What the stage itself needs of the real clock and of the browser is taken here, at
|
|
5
|
+
* startup, before any film code can replace it.
|
|
6
|
+
*/
|
|
7
|
+
export const realNow = performance.now.bind(performance);
|
|
8
|
+
export const wallNow = Date.now.bind(Date);
|
|
9
|
+
export const RealMutationObserver = MutationObserver;
|
|
10
|
+
export const RealMessageChannel = typeof MessageChannel === 'function' ? MessageChannel : null;
|
|
11
|
+
/** What `new Date()` says at film time 0. */
|
|
12
|
+
export const FILM_EPOCH_MS = Date.UTC(2026, 0, 1, 12, 0, 0);
|
|
13
|
+
let filmT = 0;
|
|
14
|
+
let random = rng(frameSeed(0));
|
|
15
|
+
export const filmTime = () => filmT;
|
|
16
|
+
export function setFilmTime(t) {
|
|
17
|
+
filmT = t;
|
|
18
|
+
random = rng(frameSeed(t * 1000));
|
|
19
|
+
}
|
|
20
|
+
/** Math.random starts afresh from `seed` (a scene's own seed, at its own moment). */
|
|
21
|
+
export function reseed(seed) {
|
|
22
|
+
random = rng(seed);
|
|
23
|
+
}
|
|
24
|
+
/** Zero-argument `Date()`, `new Date()` and `Date.now()` read the film clock; an explicit date passes through. */
|
|
25
|
+
function installFilmDate() {
|
|
26
|
+
const RealDate = Date;
|
|
27
|
+
const at = () => FILM_EPOCH_MS + Math.round(filmT * 1000);
|
|
28
|
+
function FilmDate(...args) {
|
|
29
|
+
const given = args.length === 0 ? [at()] : args;
|
|
30
|
+
if (new.target === undefined)
|
|
31
|
+
return Reflect.construct(RealDate, given).toString();
|
|
32
|
+
return Reflect.construct(RealDate, given, new.target);
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(FilmDate, 'prototype', { value: RealDate.prototype });
|
|
35
|
+
Object.assign(FilmDate, { UTC: RealDate.UTC, parse: RealDate.parse, now: at });
|
|
36
|
+
Reflect.set(window, 'Date', FilmDate);
|
|
37
|
+
}
|
|
38
|
+
export function installFilmClock() {
|
|
39
|
+
installFilmDate();
|
|
40
|
+
try {
|
|
41
|
+
Reflect.set(performance, 'now', () => filmT * 1000);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Not writable here: left as it is.
|
|
45
|
+
}
|
|
46
|
+
Math.random = () => random();
|
|
47
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FilmSeconds, PlaySeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import type { RenderMode, StageMeta } from '../protocol.ts';
|
|
3
|
+
import type { CaptionLayer } from './captions.ts';
|
|
4
|
+
import type { Kit } from './kit/index.ts';
|
|
5
|
+
import type { SceneClock } from './recut.ts';
|
|
6
|
+
/** window.CUT, as checked: everything the stage calls on it. */
|
|
7
|
+
export interface Cut {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
fps: 24 | 30 | 60;
|
|
11
|
+
duration: number;
|
|
12
|
+
background: string;
|
|
13
|
+
chapters: unknown;
|
|
14
|
+
render(t: FilmSeconds, kit: Kit): unknown;
|
|
15
|
+
init: ((kit: Kit) => unknown) | null;
|
|
16
|
+
score: ((builder: unknown, kit: Kit) => unknown) | null;
|
|
17
|
+
narration: unknown;
|
|
18
|
+
}
|
|
19
|
+
/** The one film this frame loads. */
|
|
20
|
+
export declare const film: {
|
|
21
|
+
cut: Cut | null;
|
|
22
|
+
kit: Kit | null;
|
|
23
|
+
meta: StageMeta | null;
|
|
24
|
+
/** null: nobody retimed the scenes, so played time is the film's own. */
|
|
25
|
+
clock: SceneClock | null;
|
|
26
|
+
captions: CaptionLayer | null;
|
|
27
|
+
loaded: boolean;
|
|
28
|
+
mode: RenderMode;
|
|
29
|
+
/** Film seconds per played second at the moment drawn last. */
|
|
30
|
+
rate: number;
|
|
31
|
+
played: PlaySeconds;
|
|
32
|
+
};
|
|
33
|
+
/** The film's own time at played time `t`, and the played length. */
|
|
34
|
+
export declare function filmAt(t: PlaySeconds): {
|
|
35
|
+
T: FilmSeconds;
|
|
36
|
+
rate: number;
|
|
37
|
+
};
|
|
38
|
+
export declare const playedDuration: () => number;
|
|
39
|
+
/** A thing a film gave back that may be a promise. */
|
|
40
|
+
export declare const isThenable: (value: unknown) => value is PromiseLike<unknown>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The one film this frame loads. */
|
|
2
|
+
export const film = {
|
|
3
|
+
cut: null,
|
|
4
|
+
kit: null,
|
|
5
|
+
meta: null,
|
|
6
|
+
/** null: nobody retimed the scenes, so played time is the film's own. */
|
|
7
|
+
clock: null,
|
|
8
|
+
captions: null,
|
|
9
|
+
loaded: false,
|
|
10
|
+
mode: 'rough',
|
|
11
|
+
/** Film seconds per played second at the moment drawn last. */
|
|
12
|
+
rate: 1,
|
|
13
|
+
played: 0,
|
|
14
|
+
};
|
|
15
|
+
/** The film's own time at played time `t`, and the played length. */
|
|
16
|
+
export function filmAt(t) {
|
|
17
|
+
const cut = film.cut;
|
|
18
|
+
if (!cut)
|
|
19
|
+
return { T: t, rate: 1 };
|
|
20
|
+
const clock = film.clock;
|
|
21
|
+
if (!clock)
|
|
22
|
+
return { T: Math.min(cut.duration, t), rate: 1 };
|
|
23
|
+
const at = clock.at(t);
|
|
24
|
+
// The parts' lengths are rounded to 1e-6, so their sum can miss the cut's duration: the played end draws the cut's own end.
|
|
25
|
+
return { T: (t >= clock.duration ? cut.duration : Math.min(cut.duration, at.T)), rate: at.rate };
|
|
26
|
+
}
|
|
27
|
+
export const playedDuration = () => film.clock?.duration ?? film.cut?.duration ?? 0;
|
|
28
|
+
/** A thing a film gave back that may be a promise. */
|
|
29
|
+
export const isThenable = (value) => value !== null && (typeof value === 'object' || typeof value === 'function') && typeof Reflect.get(value, 'then') === 'function';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function watchNavigation(): void;
|
|
2
|
+
/**
|
|
3
|
+
* The stage is a picture: nothing in it may take keyboard focus from the player (a lifted composer carries
|
|
4
|
+
* autofocus or calls focus()). A focus that lands anyway is handed back to the host's window.
|
|
5
|
+
*/
|
|
6
|
+
export declare function containFocus(): void;
|
|
7
|
+
/** A link on the stage is never followed: the host is told of the click and decides. */
|
|
8
|
+
export declare function holdLinks(): void;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { say } from "./port.js";
|
|
2
|
+
/*
|
|
3
|
+
* Registered before any film code, and capturing: a film's own capturing listener on window would otherwise run
|
|
4
|
+
* first and could stop these.
|
|
5
|
+
*/
|
|
6
|
+
export function watchNavigation() {
|
|
7
|
+
const navigating = () => say({ type: 'navigating' });
|
|
8
|
+
window.addEventListener('beforeunload', navigating, true);
|
|
9
|
+
// `persisted` is read through its getter as it was before the film ran, which could redefine it to call every navigation a suspend.
|
|
10
|
+
const persisted = typeof PageTransitionEvent === 'function' ? Object.getOwnPropertyDescriptor(PageTransitionEvent.prototype, 'persisted')?.get : undefined;
|
|
11
|
+
window.addEventListener('pagehide', (event) => {
|
|
12
|
+
let suspended = false;
|
|
13
|
+
try {
|
|
14
|
+
suspended = !!persisted && Reflect.apply(persisted, event, []) === true;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
suspended = false;
|
|
18
|
+
}
|
|
19
|
+
if (!suspended)
|
|
20
|
+
navigating();
|
|
21
|
+
}, true);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The stage is a picture: nothing in it may take keyboard focus from the player (a lifted composer carries
|
|
25
|
+
* autofocus or calls focus()). A focus that lands anyway is handed back to the host's window.
|
|
26
|
+
*/
|
|
27
|
+
export function containFocus() {
|
|
28
|
+
const parent = window.parent;
|
|
29
|
+
try {
|
|
30
|
+
HTMLElement.prototype.focus = () => { };
|
|
31
|
+
SVGElement.prototype.focus = () => { };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Left as they are.
|
|
35
|
+
}
|
|
36
|
+
window.addEventListener('focus', () => {
|
|
37
|
+
try {
|
|
38
|
+
const active = document.activeElement;
|
|
39
|
+
if (active instanceof HTMLElement || active instanceof SVGElement)
|
|
40
|
+
active.blur();
|
|
41
|
+
parent.focus();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// A cross-origin focus refused: nothing else to do.
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function hrefOf(link) {
|
|
49
|
+
if (link instanceof HTMLAnchorElement || link instanceof HTMLAreaElement)
|
|
50
|
+
return link.href;
|
|
51
|
+
if (link instanceof SVGAElement)
|
|
52
|
+
return link.href.baseVal;
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
/** A link on the stage is never followed: the host is told of the click and decides. */
|
|
56
|
+
export function holdLinks() {
|
|
57
|
+
const hold = (event) => {
|
|
58
|
+
const target = event.target;
|
|
59
|
+
const link = target instanceof Element ? target.closest('a[href], area[href]') : null;
|
|
60
|
+
if (!link)
|
|
61
|
+
return;
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
if (event.type !== 'click')
|
|
64
|
+
return;
|
|
65
|
+
const href = hrefOf(link);
|
|
66
|
+
if (/^https?:\/\//i.test(href))
|
|
67
|
+
say({ type: 'link', href: href.slice(0, 2048) });
|
|
68
|
+
};
|
|
69
|
+
for (const kind of ['click', 'auxclick'])
|
|
70
|
+
document.addEventListener(kind, hold, true);
|
|
71
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StageFace, StageFaces } from '../protocol.ts';
|
|
2
|
+
export type StackName = 'sans' | 'serif' | 'mono' | 'display';
|
|
3
|
+
/** `kit.fonts`: one object for the whole load, so a film that kept a reference reads the stacks as filled. */
|
|
4
|
+
export declare const fontFamilies: Record<StackName, string>;
|
|
5
|
+
export declare const fontFaceCss: () => string;
|
|
6
|
+
/** The `<style>` on the stage that holds the faces; it follows every face added later. */
|
|
7
|
+
export declare function attachFontStyle(style: HTMLStyleElement): void;
|
|
8
|
+
export declare function usableFaces(list: readonly StageFace[] | undefined): StageFace[];
|
|
9
|
+
/** Faces from the page, shipped or fetched, beside the film's own font assets. Resolves once all are loaded. */
|
|
10
|
+
export declare function addFonts(list: readonly StageFace[] | undefined): Promise<unknown[]>;
|
|
11
|
+
/** A font asset is a face named by its file's stem: `BrandSans-700.woff2` is "BrandSans-700". */
|
|
12
|
+
export declare function addFontAsset(name: string, url: string): void;
|
|
13
|
+
export declare function resetFonts(): void;
|
|
14
|
+
/** The film's faces, and a person's, go to the heads of their stacks before the cut reads them. */
|
|
15
|
+
export declare function fillStacks(faces: StageFaces | null): void;
|
|
16
|
+
/**
|
|
17
|
+
* The stacks for a css module, which cannot read the kit: var(--sans) always, and the others where a face fills
|
|
18
|
+
* them. In :where(:root), so that a film's own property of the name wins.
|
|
19
|
+
*/
|
|
20
|
+
export declare function stackCss(): string;
|
|
21
|
+
/** Waits for every face to be in, before init() measures anything. */
|
|
22
|
+
export declare function fontsIn(): Promise<void>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const SYSTEM_STACKS = {
|
|
2
|
+
sans: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif',
|
|
3
|
+
serif: 'ui-serif, Georgia, "Times New Roman", serif',
|
|
4
|
+
mono: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
5
|
+
};
|
|
6
|
+
/** `kit.fonts`: one object for the whole load, so a film that kept a reference reads the stacks as filled. */
|
|
7
|
+
export const fontFamilies = { ...SYSTEM_STACKS, display: '' };
|
|
8
|
+
/** The stacks a face was named for (by the film's scenes.json fonts or a person's choice). */
|
|
9
|
+
let filled = [];
|
|
10
|
+
const probes = [];
|
|
11
|
+
let fontCss = '';
|
|
12
|
+
let fontStyle = null;
|
|
13
|
+
export const fontFaceCss = () => fontCss;
|
|
14
|
+
/** The `<style>` on the stage that holds the faces; it follows every face added later. */
|
|
15
|
+
export function attachFontStyle(style) {
|
|
16
|
+
fontStyle = style;
|
|
17
|
+
style.textContent = fontCss;
|
|
18
|
+
}
|
|
19
|
+
/** A face with a unicode-range loads only for text in it, so a probe may carry its sample text. */
|
|
20
|
+
function loadFace(probe) {
|
|
21
|
+
const loading = typeof probe === 'string' ? document.fonts.load(probe) : document.fonts.load(probe[0], probe[1]);
|
|
22
|
+
return loading.catch(() => null);
|
|
23
|
+
}
|
|
24
|
+
// Each field is matched before it is joined into CSS: fetched faces come from outside.
|
|
25
|
+
const FACE_FIELDS = {
|
|
26
|
+
family: /^[A-Za-z0-9 _-]{1,64}$/,
|
|
27
|
+
url: /^data:font\/woff2;base64,[A-Za-z0-9+/]+={0,2}$/,
|
|
28
|
+
weight: /^[0-9]{1,4}(?: [0-9]{1,4})?$/,
|
|
29
|
+
style: /^(?:normal|italic)$/,
|
|
30
|
+
range: /^(?:U\+[0-9A-F]{1,6}(?:-[0-9A-F]{1,6})?(?:,(?=U)|$))*$/,
|
|
31
|
+
};
|
|
32
|
+
export function usableFaces(list) {
|
|
33
|
+
return (list ?? []).filter((face) => FACE_FIELDS.family.test(face.family) &&
|
|
34
|
+
FACE_FIELDS.url.test(face.url) &&
|
|
35
|
+
FACE_FIELDS.weight.test(face.weight) &&
|
|
36
|
+
FACE_FIELDS.style.test(face.style) &&
|
|
37
|
+
(face.range === undefined || FACE_FIELDS.range.test(face.range)));
|
|
38
|
+
}
|
|
39
|
+
/** Faces from the page, shipped or fetched, beside the film's own font assets. Resolves once all are loaded. */
|
|
40
|
+
export function addFonts(list) {
|
|
41
|
+
const had = fontCss;
|
|
42
|
+
const loads = usableFaces(list).map((face) => {
|
|
43
|
+
const rule = `\n@font-face{font-family:"${face.family}";src:url(${face.url}) format("woff2");font-weight:${face.weight};font-style:${face.style}` +
|
|
44
|
+
(face.range ? ';unicode-range:' + face.range : '') +
|
|
45
|
+
(face.box ? `;ascent-override:${Number(face.box[0])}%;descent-override:${Number(face.box[1])}%;line-gap-override:0%` : '') +
|
|
46
|
+
';font-display:block}';
|
|
47
|
+
const probe = [
|
|
48
|
+
`${face.style} ${face.weight.split(' ')[0]} 16px "${face.family}"`,
|
|
49
|
+
typeof face.sample === 'string' && face.sample.length <= 2 ? face.sample : ' ',
|
|
50
|
+
];
|
|
51
|
+
if (fontCss.indexOf(rule) < 0) {
|
|
52
|
+
fontCss += rule;
|
|
53
|
+
probes.push(probe);
|
|
54
|
+
}
|
|
55
|
+
return probe;
|
|
56
|
+
});
|
|
57
|
+
if (fontStyle && fontCss !== had)
|
|
58
|
+
fontStyle.textContent = fontCss;
|
|
59
|
+
return Promise.all(loads.map(loadFace));
|
|
60
|
+
}
|
|
61
|
+
/** A font asset is a face named by its file's stem: `BrandSans-700.woff2` is "BrandSans-700". */
|
|
62
|
+
export function addFontAsset(name, url) {
|
|
63
|
+
const family = name.replace(/\.[a-z0-9]+$/i, '').replace(/["\\]/g, '');
|
|
64
|
+
fontCss += `\n@font-face{font-family:"${family}";src:url(${url});font-display:block}`;
|
|
65
|
+
probes.push(`16px "${family}"`);
|
|
66
|
+
}
|
|
67
|
+
export function resetFonts() {
|
|
68
|
+
fontCss = '';
|
|
69
|
+
probes.length = 0;
|
|
70
|
+
}
|
|
71
|
+
/** The film's faces, and a person's, go to the heads of their stacks before the cut reads them. */
|
|
72
|
+
export function fillStacks(faces) {
|
|
73
|
+
filled = [];
|
|
74
|
+
fontFamilies.sans = SYSTEM_STACKS.sans;
|
|
75
|
+
fontFamilies.serif = SYSTEM_STACKS.serif;
|
|
76
|
+
fontFamilies.mono = SYSTEM_STACKS.mono;
|
|
77
|
+
if (faces) {
|
|
78
|
+
void addFonts(faces.fonts);
|
|
79
|
+
const slots = faces.slots;
|
|
80
|
+
filled = ['sans', 'serif', 'mono', 'display'].filter((stack) => typeof slots[stack] === 'string');
|
|
81
|
+
// Sans first: a headline's face falls back to the film's own sans.
|
|
82
|
+
for (const stack of filled) {
|
|
83
|
+
const family = (slots[stack] ?? '').replace(/["\\]/g, '');
|
|
84
|
+
fontFamilies[stack] = `"${family}", ` + (stack === 'display' ? fontFamilies.sans : SYSTEM_STACKS[stack]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Headlines with no face of their own: the sans stack with its first family twice. It draws alike, and words that
|
|
88
|
+
// wear it written out can still be told from words in the sans stack.
|
|
89
|
+
if (!filled.includes('display'))
|
|
90
|
+
fontFamilies.display = (/^("[^"]*"|[^,]*)/.exec(fontFamilies.sans)?.[0] ?? '') + ', ' + fontFamilies.sans;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The stacks for a css module, which cannot read the kit: var(--sans) always, and the others where a face fills
|
|
94
|
+
* them. In :where(:root), so that a film's own property of the name wins.
|
|
95
|
+
*/
|
|
96
|
+
export function stackCss() {
|
|
97
|
+
const stacks = ['sans', ...filled.filter((stack) => stack !== 'sans')];
|
|
98
|
+
return ':where(:root){' + stacks.map((stack) => `--${stack}:${fontFamilies[stack]}`).join(';') + '}';
|
|
99
|
+
}
|
|
100
|
+
/** Waits for every face to be in, before init() measures anything. */
|
|
101
|
+
export async function fontsIn() {
|
|
102
|
+
try {
|
|
103
|
+
await Promise.all(probes.map(loadFace));
|
|
104
|
+
await document.fonts.ready;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
// No font loading API: carry on.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HostToStage } from '../protocol.ts';
|
|
2
|
+
type FrameAsk = Extract<HostToStage, {
|
|
3
|
+
type: 'frame';
|
|
4
|
+
}>;
|
|
5
|
+
/** A pull that only inspects the picture: past this, the page could not use it anyway. */
|
|
6
|
+
export declare const SMALL_SNAPSHOT_MAX_CHARS = 1000000;
|
|
7
|
+
/** Draws the asked moment and answers with its snapshot, after any live render still painting has settled. */
|
|
8
|
+
export declare function pullFrame(ask: FrameAsk): void;
|
|
9
|
+
export {};
|