@open-artifact/motion-stage 0.0.0-stage → 0.12.0-rc.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 +71 -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,53 @@
|
|
|
1
|
+
import { guarded } from "./console.js";
|
|
2
|
+
const modules = new Map();
|
|
3
|
+
/** The global through which injected code finds the kit it was given: read once, at the top of each script. */
|
|
4
|
+
export const KIT_GLOBAL = '__stageKit';
|
|
5
|
+
export function addModules(list) {
|
|
6
|
+
for (const module of list)
|
|
7
|
+
modules.set(module.name, { kind: module.kind, text: module.text });
|
|
8
|
+
}
|
|
9
|
+
export const moduleNames = () => [...modules.keys()];
|
|
10
|
+
/** By its whole name, or by a bare stem that only one module has: "scenes" finds "scenes.json". */
|
|
11
|
+
export function findModule(name) {
|
|
12
|
+
const exact = modules.get(name);
|
|
13
|
+
if (exact)
|
|
14
|
+
return exact;
|
|
15
|
+
if (name.includes('.'))
|
|
16
|
+
return null;
|
|
17
|
+
const hits = [...modules.keys()].filter((key) => key.startsWith(name + '.') && key.indexOf('.', name.length + 1) < 0);
|
|
18
|
+
const only = hits.length === 1 ? hits[0] : undefined;
|
|
19
|
+
return only === undefined ? null : (modules.get(only) ?? null);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Runs code in the page with `kit` in reach. What it throws surfaces through the window's error event. `own`: strict
|
|
23
|
+
* code in a function's scope, so that no top-level name reaches window, where another module's could meet it.
|
|
24
|
+
*/
|
|
25
|
+
export function injectScript(code, label, own = false) {
|
|
26
|
+
const script = document.createElement('script');
|
|
27
|
+
script.text =
|
|
28
|
+
(own ? `(function () { "use strict"; const kit = window.${KIT_GLOBAL};\n` : `{ const kit = window.${KIT_GLOBAL};\n`) +
|
|
29
|
+
code +
|
|
30
|
+
(own ? '\n})();' : '\n}') +
|
|
31
|
+
'\n//# sourceURL=' +
|
|
32
|
+
label.replace(/[^A-Za-z0-9_./ -]/g, '_') +
|
|
33
|
+
'\n';
|
|
34
|
+
document.body.appendChild(script);
|
|
35
|
+
script.remove();
|
|
36
|
+
}
|
|
37
|
+
export function setScriptKit(kit) {
|
|
38
|
+
Reflect.set(window, KIT_GLOBAL, kit);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Runs code with `kit` as its kit and gives back what it threw, or null: a script put into the page throws to the
|
|
42
|
+
* window's error event during the call, never to the caller, so a try alone would catch nothing.
|
|
43
|
+
*/
|
|
44
|
+
export function tryScript(code, label, kit, own) {
|
|
45
|
+
const outerKit = Reflect.get(window, KIT_GLOBAL);
|
|
46
|
+
setScriptKit(kit);
|
|
47
|
+
try {
|
|
48
|
+
return guarded(() => injectScript(code, label, own));
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
setScriptKit(outerKit);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HostToStage, StageToHost } from '../protocol.ts';
|
|
2
|
+
/** What the stage says before the host's port has come waits for it. */
|
|
3
|
+
export declare function say(message: StageToHost): void;
|
|
4
|
+
/**
|
|
5
|
+
* The host posts the port to this window once, on the frame's load. The frame is sandboxed with allow-scripts only,
|
|
6
|
+
* so the port is the one way in: the first one from the parent is kept, any other ignored.
|
|
7
|
+
*/
|
|
8
|
+
export declare function connect(onMessage: (message: HostToStage) => void): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { STAGE_PROTOCOL_VERSION, isHostToStage, isStageConnect } from "../protocol.js";
|
|
2
|
+
// Taken before the film runs: a film may replace MessagePort's methods to read or drop what the stage says.
|
|
3
|
+
const realPost = MessagePort.prototype.postMessage;
|
|
4
|
+
const realParent = window.parent;
|
|
5
|
+
const MOST_QUEUED = 200;
|
|
6
|
+
let port = null;
|
|
7
|
+
const queued = [];
|
|
8
|
+
/** Never with a transfer list: building one runs Array.prototype[Symbol.iterator], which a film can make throw. */
|
|
9
|
+
function post(to, message) {
|
|
10
|
+
try {
|
|
11
|
+
Reflect.apply(realPost, to, [message]);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// The browser refused to clone it: dropped.
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** What the stage says before the host's port has come waits for it. */
|
|
18
|
+
export function say(message) {
|
|
19
|
+
if (port)
|
|
20
|
+
post(port, message);
|
|
21
|
+
else if (queued.length < MOST_QUEUED)
|
|
22
|
+
queued.push(message);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The host posts the port to this window once, on the frame's load. The frame is sandboxed with allow-scripts only,
|
|
26
|
+
* so the port is the one way in: the first one from the parent is kept, any other ignored.
|
|
27
|
+
*/
|
|
28
|
+
export function connect(onMessage) {
|
|
29
|
+
const take = (event) => {
|
|
30
|
+
if (port || event.source !== realParent || !isStageConnect(event.data))
|
|
31
|
+
return;
|
|
32
|
+
const given = event.ports[0];
|
|
33
|
+
if (!given)
|
|
34
|
+
return;
|
|
35
|
+
port = given;
|
|
36
|
+
window.removeEventListener('message', take, true);
|
|
37
|
+
given.onmessage = (incoming) => {
|
|
38
|
+
if (isHostToStage(incoming.data))
|
|
39
|
+
onMessage(incoming.data);
|
|
40
|
+
};
|
|
41
|
+
post(given, { type: 'hello', version: STAGE_PROTOCOL_VERSION });
|
|
42
|
+
for (const message of queued.splice(0))
|
|
43
|
+
post(given, message);
|
|
44
|
+
};
|
|
45
|
+
window.addEventListener('message', take, true);
|
|
46
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RetouchedRecord } from '@open-artifact/motion-core';
|
|
2
|
+
import type { HostToStage } from '../protocol.ts';
|
|
3
|
+
type LayersAsk = Extract<HostToStage, {
|
|
4
|
+
type: 'layers';
|
|
5
|
+
}>;
|
|
6
|
+
type LookAsk = Extract<HostToStage, {
|
|
7
|
+
type: 'look';
|
|
8
|
+
}>;
|
|
9
|
+
/** Takes the record of what a person changed by hand; a fault costs their changes, not the film. */
|
|
10
|
+
export declare function takeRecord(record: RetouchedRecord | null): void;
|
|
11
|
+
/**
|
|
12
|
+
* A look that is being tried (a drag on a slider), or the record a load brought. It takes the marks off the one
|
|
13
|
+
* stage, so it takes its turn with the frame pulls.
|
|
14
|
+
*/
|
|
15
|
+
export declare function tryLook(record: RetouchedRecord | null): void;
|
|
16
|
+
/** A reading takes its turn with the frame pulls, since it draws on the one stage. A newer question, or a stop, ends one under way. */
|
|
17
|
+
export declare function readLayers(d: LayersAsk): void;
|
|
18
|
+
/**
|
|
19
|
+
* How the film itself draws one thing, for the controls that change it. d.t: the moment the page has on show, drawn
|
|
20
|
+
* first, which sets the marks the answer tells of, and again at the end. A question a newer one has overtaken by its
|
|
21
|
+
* turn is answered with no look, and draws nothing.
|
|
22
|
+
*/
|
|
23
|
+
export declare function lookAt(d: LookAsk): void;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { filmTime } from "./determinism.js";
|
|
2
|
+
import { film } from "./film.js";
|
|
3
|
+
import { scanLayers } from "./layers/scan.js";
|
|
4
|
+
import { say } from "./port.js";
|
|
5
|
+
import { afterSample, drawOwn, holdStage, onShow, releaseStage, renderAt, renderState, rendersSettle, setShown, syncClips, takeTurn, unretouched } from "./render.js";
|
|
6
|
+
import { ask, lookOn } from "./retouch/ask.js";
|
|
7
|
+
import { kinFor, lostIn, partsFor, setRecord, stageOf, underHideIn } from "./retouch/retouch.js";
|
|
8
|
+
import { laidOut, whenLaidOut } from "./stage.js";
|
|
9
|
+
import { stretchOf } from "./stretch.js";
|
|
10
|
+
const messageOf = (error) => String(error instanceof Error ? error.message : error).slice(0, 200);
|
|
11
|
+
const quiet = (q) => {
|
|
12
|
+
q?.then(undefined, () => undefined);
|
|
13
|
+
};
|
|
14
|
+
/** A moment of this stretch and of no other: short of its end, the next one's first moment, rounded down, and not before its start. */
|
|
15
|
+
const momentIn = (s, T) => Math.max(s.lo, Math.floor(Math.min(s.hi - 1 / 240, T - s.base) * 1000) / 1000);
|
|
16
|
+
/** Takes the record of what a person changed by hand; a fault costs their changes, not the film. */
|
|
17
|
+
export function takeRecord(record) {
|
|
18
|
+
try {
|
|
19
|
+
setRecord(record);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
unretouched();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A look that is being tried (a drag on a slider), or the record a load brought. It takes the marks off the one
|
|
27
|
+
* stage, so it takes its turn with the frame pulls.
|
|
28
|
+
*/
|
|
29
|
+
export function tryLook(record) {
|
|
30
|
+
takeTurn(async () => {
|
|
31
|
+
const wait = rendersSettle();
|
|
32
|
+
if (wait)
|
|
33
|
+
await wait;
|
|
34
|
+
takeRecord(record);
|
|
35
|
+
if (!film.loaded)
|
|
36
|
+
return;
|
|
37
|
+
const sampled = renderState.gavePromise;
|
|
38
|
+
quiet(renderAt(onShow()));
|
|
39
|
+
// A sample this draw began: the moment on show is drawn again after it, unless a live frame is on its way.
|
|
40
|
+
const again = afterSample(sampled, onShow);
|
|
41
|
+
if (again)
|
|
42
|
+
quiet((await again).q);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Draws a moment at once for a reading. A render that returns a promise has not drawn yet when it returns, so what
|
|
47
|
+
* stands on the stage is of no one moment: better no list than boxes that lie elsewhere.
|
|
48
|
+
*/
|
|
49
|
+
function drawNow(t) {
|
|
50
|
+
const drawn = renderAt(t);
|
|
51
|
+
if (drawn) {
|
|
52
|
+
quiet(drawn);
|
|
53
|
+
throw new Error('render(t) returns a promise');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let layersWanted = 0;
|
|
57
|
+
/** A new frame is laid out a moment after it is shown: a reading asked for before then waits this long for it. */
|
|
58
|
+
const LAYOUT_WAIT_MS = 2000;
|
|
59
|
+
/** A reading takes its turn with the frame pulls, since it draws on the one stage. A newer question, or a stop, ends one under way. */
|
|
60
|
+
export function readLayers(d) {
|
|
61
|
+
layersWanted = d.seq;
|
|
62
|
+
if (d.stop)
|
|
63
|
+
return;
|
|
64
|
+
takeTurn(async () => {
|
|
65
|
+
let report = null;
|
|
66
|
+
if (film.loaded && d.seq === layersWanted && !laidOut())
|
|
67
|
+
await whenLaidOut(LAYOUT_WAIT_MS);
|
|
68
|
+
const stage = stageOf();
|
|
69
|
+
if (film.loaded && stage && d.seq === layersWanted) {
|
|
70
|
+
holdStage();
|
|
71
|
+
try {
|
|
72
|
+
report = await scanLayers(d, {
|
|
73
|
+
stage,
|
|
74
|
+
render: drawNow,
|
|
75
|
+
look: (el) => {
|
|
76
|
+
const s = stretchOf(filmTime());
|
|
77
|
+
const k = lookOn(el, s.lo, s.hi, s.name);
|
|
78
|
+
if (k)
|
|
79
|
+
k.at = momentIn(s, filmTime());
|
|
80
|
+
return k;
|
|
81
|
+
},
|
|
82
|
+
kin: kinFor,
|
|
83
|
+
parts: partsFor,
|
|
84
|
+
lost: () => {
|
|
85
|
+
const s = stretchOf(filmTime());
|
|
86
|
+
return s.name === null ? [] : lostIn(s.name, s.lo, s.hi);
|
|
87
|
+
},
|
|
88
|
+
underHide: () => {
|
|
89
|
+
const s = stretchOf(filmTime());
|
|
90
|
+
return s.name === null ? [] : underHideIn(s.name, s.lo, s.hi);
|
|
91
|
+
},
|
|
92
|
+
}, () => d.seq !== layersWanted);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
console.warn('layers: ' + messageOf(error));
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
releaseStage();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
say({ type: 'layers', report: report ?? { seq: d.seq, from: d.from, to: d.to, at: d.at, lost: [], underHide: [], rows: null } });
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
let lookWanted = 0;
|
|
105
|
+
/**
|
|
106
|
+
* How the film itself draws one thing, for the controls that change it. d.t: the moment the page has on show, drawn
|
|
107
|
+
* first, which sets the marks the answer tells of, and again at the end. A question a newer one has overtaken by its
|
|
108
|
+
* turn is answered with no look, and draws nothing.
|
|
109
|
+
*/
|
|
110
|
+
export function lookAt(d) {
|
|
111
|
+
lookWanted = d.seq;
|
|
112
|
+
takeTurn(async () => {
|
|
113
|
+
let look = null;
|
|
114
|
+
let lost = [];
|
|
115
|
+
if (film.loaded && d.seq === lookWanted) {
|
|
116
|
+
holdStage();
|
|
117
|
+
try {
|
|
118
|
+
const wait = rendersSettle();
|
|
119
|
+
if (wait)
|
|
120
|
+
await wait;
|
|
121
|
+
const t = d.t !== null && Number.isFinite(d.t) ? d.t : onShow();
|
|
122
|
+
const sampled = renderState.gavePromise;
|
|
123
|
+
let away = false;
|
|
124
|
+
try {
|
|
125
|
+
const drawn = renderAt(t);
|
|
126
|
+
const s = stretchOf(filmTime());
|
|
127
|
+
const at = momentIn(s, filmTime());
|
|
128
|
+
const name = s.name;
|
|
129
|
+
if (drawn)
|
|
130
|
+
await drawn.then(undefined, () => undefined);
|
|
131
|
+
else if (renderState.ok && name !== null) {
|
|
132
|
+
away = true;
|
|
133
|
+
look = ask(name, d.id, s.lo, s.hi, at, (x) => drawOwn(s.base + Math.max(s.lo, Math.min(s.hi - 1 / 240, x))));
|
|
134
|
+
lost = lostIn(name, s.lo, s.hi);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
look = null;
|
|
139
|
+
console.warn('How the film draws a thing could not be read, so the page offers no way to change it: ' + messageOf(error));
|
|
140
|
+
}
|
|
141
|
+
if (away) {
|
|
142
|
+
setShown(t);
|
|
143
|
+
quiet(renderAt(t));
|
|
144
|
+
quiet(syncClips('hold'));
|
|
145
|
+
}
|
|
146
|
+
// A sample its draws began goes first, and the moment is drawn again before the answer.
|
|
147
|
+
const again = afterSample(sampled, t);
|
|
148
|
+
if (again) {
|
|
149
|
+
quiet((await again).q);
|
|
150
|
+
quiet(syncClips('hold'));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// The next frame draws it.
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
releaseStage();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
say({ type: 'looked', seq: d.seq, id: d.id, look, lost });
|
|
161
|
+
});
|
|
162
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { FilmSeconds, PlaySeconds, ScoreData } from '@open-artifact/motion-core';
|
|
2
|
+
import type { Chapter, FilmPart, PlayedScore } from '../protocol.ts';
|
|
3
|
+
/** A part of the film on its own time, before a person's lengths are laid on it. */
|
|
4
|
+
export interface NaturalPart {
|
|
5
|
+
key: string | null;
|
|
6
|
+
label: string;
|
|
7
|
+
a0: FilmSeconds;
|
|
8
|
+
nat: number;
|
|
9
|
+
}
|
|
10
|
+
export interface SceneClock {
|
|
11
|
+
parts: FilmPart[];
|
|
12
|
+
duration: PlaySeconds;
|
|
13
|
+
moved: boolean;
|
|
14
|
+
/** The lengths asked for would take the film past its limit, so every part plays at its own. */
|
|
15
|
+
aside: boolean;
|
|
16
|
+
at(t: PlaySeconds): {
|
|
17
|
+
T: FilmSeconds;
|
|
18
|
+
rate: number;
|
|
19
|
+
};
|
|
20
|
+
back(T: FilmSeconds): PlaySeconds;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The film cut on its own time: at 0, and at each labelled chapter more than CUT_GAP_S from the cut before it and
|
|
24
|
+
* from the end. `ids`: the scenes' ids, in a film made of scenes.
|
|
25
|
+
*/
|
|
26
|
+
export declare function filmParts(duration: number, chapters: readonly Chapter<FilmSeconds>[], ids: readonly string[] | null): NaturalPart[];
|
|
27
|
+
/**
|
|
28
|
+
* The parts as played, from the film's own parts and `modules/recut.json`. An entry stands only while the film still
|
|
29
|
+
* says its "was", and a length only within the limits; one that does not is ignored, never clamped.
|
|
30
|
+
*/
|
|
31
|
+
export declare function sceneClock(own: readonly NaturalPart[], text: string | null): SceneClock;
|
|
32
|
+
/** The clock of a film nobody retimed. */
|
|
33
|
+
export declare function plainClock(duration: number): SceneClock;
|
|
34
|
+
/** The score is recorded on the film's own time and played on played time; a stretched part's sound slows with it, at the same pitch. */
|
|
35
|
+
export declare function playedScore(score: ScoreData, clock: SceneClock): PlayedScore;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const MOST_FILM_S = 300;
|
|
2
|
+
const NEAR_S = 0.001;
|
|
3
|
+
const EDGE_S = 1e-9;
|
|
4
|
+
const MOST_RECORD_CHARS = 262144;
|
|
5
|
+
const MOST_ENTRIES = 33;
|
|
6
|
+
const CUT_GAP_S = 0.05;
|
|
7
|
+
const round6 = (x) => Math.round(x * 1e6) / 1e6;
|
|
8
|
+
const film = (x) => x;
|
|
9
|
+
const play = (x) => x;
|
|
10
|
+
/**
|
|
11
|
+
* The film cut on its own time: at 0, and at each labelled chapter more than CUT_GAP_S from the cut before it and
|
|
12
|
+
* from the end. `ids`: the scenes' ids, in a film made of scenes.
|
|
13
|
+
*/
|
|
14
|
+
export function filmParts(duration, chapters, ids) {
|
|
15
|
+
const cuts = [{ t: 0, label: '' }];
|
|
16
|
+
for (const chapter of chapters) {
|
|
17
|
+
if (chapter.label.trim() === '' || chapter.t >= duration - CUT_GAP_S)
|
|
18
|
+
continue;
|
|
19
|
+
const first = cuts[0];
|
|
20
|
+
if (chapter.t <= CUT_GAP_S) {
|
|
21
|
+
if (cuts.length === 1 && first)
|
|
22
|
+
first.label = chapter.label;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const last = cuts[cuts.length - 1];
|
|
26
|
+
if (last && chapter.t - last.t > CUT_GAP_S)
|
|
27
|
+
cuts.push({ t: chapter.t, label: chapter.label });
|
|
28
|
+
}
|
|
29
|
+
const keyed = !!ids && ids.length === cuts.length;
|
|
30
|
+
return cuts.map((cut, i) => ({
|
|
31
|
+
key: keyed ? (ids?.[i] ?? null) : null,
|
|
32
|
+
label: cut.label,
|
|
33
|
+
a0: film(cut.t),
|
|
34
|
+
nat: round6((cuts[i + 1]?.t ?? duration) - cut.t),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
const record = (value) => (value !== null && typeof value === 'object' ? value : {});
|
|
38
|
+
const HIDDEN_CHARACTERS = /[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Zl}\p{Zp}\p{Noncharacter_Code_Point}\p{Default_Ignorable_Code_Point}⠀\u{16fe4}\s]+/gu;
|
|
39
|
+
const tidyName = (name) => Array.from(name.slice(0, 96)).slice(0, 48).join('').replace(HIDDEN_CHARACTERS, ' ').trim();
|
|
40
|
+
function fits(nat, length) {
|
|
41
|
+
return (typeof length === 'number' &&
|
|
42
|
+
isFinite(length) &&
|
|
43
|
+
length >= nat * 0.5 - 1e-9 &&
|
|
44
|
+
length <= nat * 3 + 1e-9 &&
|
|
45
|
+
length >= Math.min(1, nat) &&
|
|
46
|
+
length <= MOST_FILM_S);
|
|
47
|
+
}
|
|
48
|
+
function readEntries(text) {
|
|
49
|
+
if (typeof text !== 'string' || text.length > MOST_RECORD_CHARS)
|
|
50
|
+
return [];
|
|
51
|
+
let parsed = null;
|
|
52
|
+
try {
|
|
53
|
+
parsed = JSON.parse(text);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
const root = record(parsed);
|
|
59
|
+
return root['v'] === 1 && Array.isArray(root['scenes']) ? root['scenes'].slice(0, MOST_ENTRIES) : [];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The parts as played, from the film's own parts and `modules/recut.json`. An entry stands only while the film still
|
|
63
|
+
* says its "was", and a length only within the limits; one that does not is ignored, never clamped.
|
|
64
|
+
*/
|
|
65
|
+
export function sceneClock(own, text) {
|
|
66
|
+
const entries = readEntries(text);
|
|
67
|
+
const lay = (stretch) => {
|
|
68
|
+
let p0 = 0;
|
|
69
|
+
return own.map((part, i) => {
|
|
70
|
+
let dur = part.nat;
|
|
71
|
+
let label = part.label;
|
|
72
|
+
for (const entry of entries) {
|
|
73
|
+
if (entry === null || typeof entry !== 'object')
|
|
74
|
+
continue;
|
|
75
|
+
const fields = record(entry);
|
|
76
|
+
const name = record(fields['name']);
|
|
77
|
+
const length = record(fields['dur']);
|
|
78
|
+
const matches = part.key !== null ? fields['id'] === part.key : fields['part'] === i && fields['id'] === undefined && name['was'] === part.label;
|
|
79
|
+
if (!matches)
|
|
80
|
+
continue;
|
|
81
|
+
const was = length['was'];
|
|
82
|
+
if (stretch && typeof was === 'number' && Math.abs(was - part.nat) <= NEAR_S && fits(part.nat, length['v']))
|
|
83
|
+
dur = length['v'];
|
|
84
|
+
const given = name['v'];
|
|
85
|
+
if (name['was'] === part.label && typeof given === 'string' && tidyName(given) !== '')
|
|
86
|
+
label = tidyName(given);
|
|
87
|
+
}
|
|
88
|
+
const played = { key: part.key, label, own: part.label, a0: part.a0, nat: part.nat, p0: play(p0), dur };
|
|
89
|
+
p0 = round6(p0 + dur);
|
|
90
|
+
return played;
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
const endOf = (parts) => {
|
|
94
|
+
const last = parts[parts.length - 1];
|
|
95
|
+
return last ? round6(last.p0 + last.dur) : 0;
|
|
96
|
+
};
|
|
97
|
+
const isMoved = (parts) => parts.some((part) => part.dur !== part.nat);
|
|
98
|
+
let parts = lay(true);
|
|
99
|
+
const aside = endOf(parts) > MOST_FILM_S && isMoved(parts);
|
|
100
|
+
if (aside)
|
|
101
|
+
parts = lay(false);
|
|
102
|
+
// Where nothing is moved the two clocks are one, to the last digit.
|
|
103
|
+
if (!isMoved(parts))
|
|
104
|
+
for (const part of parts)
|
|
105
|
+
part.p0 = play(part.a0);
|
|
106
|
+
const last = parts[parts.length - 1];
|
|
107
|
+
const filmEnd = last ? last.a0 + last.nat : 0;
|
|
108
|
+
const playEnd = endOf(parts);
|
|
109
|
+
const find = (by, value) => {
|
|
110
|
+
let found = parts[0];
|
|
111
|
+
for (const part of parts.slice(1))
|
|
112
|
+
if (part[by] <= value)
|
|
113
|
+
found = part;
|
|
114
|
+
return found;
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
parts,
|
|
118
|
+
duration: play(playEnd),
|
|
119
|
+
moved: isMoved(parts),
|
|
120
|
+
aside,
|
|
121
|
+
// A part stops just short of the next one's first instant.
|
|
122
|
+
at(t) {
|
|
123
|
+
const clamped = t < 0 ? 0 : t > playEnd ? playEnd : t;
|
|
124
|
+
const part = find('p0', clamped);
|
|
125
|
+
if (!part)
|
|
126
|
+
return { T: film(clamped), rate: 1 };
|
|
127
|
+
return {
|
|
128
|
+
T: film(part.a0 + Math.min(part === last ? part.nat : part.nat - EDGE_S, (clamped - part.p0) * (part.nat / part.dur))),
|
|
129
|
+
rate: part.nat / part.dur,
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
// Before 0 and past the end it runs on at normal speed: a score's lead-in and tail.
|
|
133
|
+
back(T) {
|
|
134
|
+
if (T < 0)
|
|
135
|
+
return play(T);
|
|
136
|
+
if (T >= filmEnd)
|
|
137
|
+
return play(playEnd + (T - filmEnd));
|
|
138
|
+
const part = find('a0', T);
|
|
139
|
+
if (!part)
|
|
140
|
+
return play(T);
|
|
141
|
+
return play(part.p0 + (T - part.a0) * (part.dur / part.nat));
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** The clock of a film nobody retimed. */
|
|
146
|
+
export function plainClock(duration) {
|
|
147
|
+
return sceneClock(filmParts(duration, [], null), null);
|
|
148
|
+
}
|
|
149
|
+
/** The score is recorded on the film's own time and played on played time; a stretched part's sound slows with it, at the same pitch. */
|
|
150
|
+
export function playedScore(score, clock) {
|
|
151
|
+
return {
|
|
152
|
+
...score,
|
|
153
|
+
events: score.events.map((event) => {
|
|
154
|
+
const t = clock.back(event.t);
|
|
155
|
+
return { ...event, t, dur: Math.max(0.01, clock.back(film(event.t + event.dur)) - t) };
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { PlaySeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import type { VideoMode } from './video-sync.ts';
|
|
3
|
+
/** A render promise that never settled: the frame that waits for it fails for good. */
|
|
4
|
+
export declare class RenderStuck extends Error {
|
|
5
|
+
/** A tab in the background, not the film, may be why. */
|
|
6
|
+
inBackground: boolean;
|
|
7
|
+
constructor(message: string, inBackground: boolean);
|
|
8
|
+
}
|
|
9
|
+
interface Unsettled {
|
|
10
|
+
q: Promise<void>;
|
|
11
|
+
/** A wait has given up on it after its full second. */
|
|
12
|
+
waited: boolean;
|
|
13
|
+
done: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const renderState: {
|
|
16
|
+
ok: boolean;
|
|
17
|
+
error: unknown;
|
|
18
|
+
/** How many renders have been begun. */
|
|
19
|
+
count: number;
|
|
20
|
+
warnedAsync: boolean;
|
|
21
|
+
/** A render of a moment drawn for a look alone (drawOwn) has given a promise in this load. */
|
|
22
|
+
gavePromise: boolean;
|
|
23
|
+
/** The played moment renderAt drew last. */
|
|
24
|
+
drawnAt: PlaySeconds | null;
|
|
25
|
+
};
|
|
26
|
+
export declare const unsettledNow: () => Unsettled[];
|
|
27
|
+
/**
|
|
28
|
+
* null: nothing to wait for. Else settles when the pending renders no wait has given up on have settled, or after
|
|
29
|
+
* a second: a render that stays pending costs the waits one second in all, not one each.
|
|
30
|
+
*/
|
|
31
|
+
export declare function rendersSettle(): Promise<unknown> | null;
|
|
32
|
+
/** One turn of the event loop, by a message: a tab in the background slows timers to a second. */
|
|
33
|
+
export declare function nextTask(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* One moment of the film's own time, drawn for a look at the stage alone. false: the film threw there. null: a
|
|
36
|
+
* render has given a promise in this load, so nothing more is drawn here: a render for another moment that finished
|
|
37
|
+
* late could draw that moment over the frame on show.
|
|
38
|
+
*/
|
|
39
|
+
export declare function drawOwn(T: number): boolean | null;
|
|
40
|
+
/**
|
|
41
|
+
* A sample a draw began (drawOwn) that gave a promise paints its own moment when it settles, after that draw. So what
|
|
42
|
+
* relies on the draw waits for it and has its moment drawn again. before: gavePromise ahead of the draw. again:
|
|
43
|
+
* whether the redraw is still wanted then. null: no such sample; else the redraw's own promise, wrapped so it is
|
|
44
|
+
* handed over as it is, or null where it drew at once or was not wanted.
|
|
45
|
+
*/
|
|
46
|
+
export declare function afterSample(before: boolean, t: PlaySeconds | (() => PlaySeconds), again?: () => boolean): Promise<{
|
|
47
|
+
q: Promise<unknown> | null;
|
|
48
|
+
}> | null;
|
|
49
|
+
/** A fault in laying what a person changed by hand costs their changes, not the film. */
|
|
50
|
+
export declare function unretouched(): void;
|
|
51
|
+
/** Draws played moment `t`. Gives back the render's promise, bounded, if it gave one. */
|
|
52
|
+
export declare function renderAt(t: PlaySeconds): Promise<unknown> | null;
|
|
53
|
+
export declare function syncClips(mode: VideoMode, quick?: boolean): Promise<unknown> | null;
|
|
54
|
+
export declare const busy: {
|
|
55
|
+
framing: boolean;
|
|
56
|
+
};
|
|
57
|
+
/** A turn draws moments of its own: live renders wait until {@link releaseStage}. */
|
|
58
|
+
export declare function holdStage(): void;
|
|
59
|
+
export declare function releaseStage(): void;
|
|
60
|
+
export declare function takeTurn(job: () => Promise<void>): void;
|
|
61
|
+
interface Asked {
|
|
62
|
+
t: PlaySeconds;
|
|
63
|
+
seq: number;
|
|
64
|
+
playing: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The stage shows, still, the moment the page last asked for, with nothing to draw over it: what its words are is
|
|
68
|
+
* worth telling. Not before the page's first render of a load, whose own first frame is at 0.
|
|
69
|
+
*/
|
|
70
|
+
export declare const showsAsked: () => boolean;
|
|
71
|
+
/** The moment the page last asked to see, drawn yet or not. */
|
|
72
|
+
export declare const onShow: () => PlaySeconds;
|
|
73
|
+
export declare const isPlaying: () => boolean;
|
|
74
|
+
export declare const hasPending: () => boolean;
|
|
75
|
+
/** A moment a question drew again is the one on show. */
|
|
76
|
+
export declare const setShown: (t: PlaySeconds) => void;
|
|
77
|
+
export declare function askRender(next: Asked): void;
|
|
78
|
+
/** Draws the moment on show again under the same ask, after a change the film reads, such as the render mode. */
|
|
79
|
+
export declare function redraw(): void;
|
|
80
|
+
export {};
|