@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,154 @@
|
|
|
1
|
+
import { RealMutationObserver, realNow } from "./determinism.js";
|
|
2
|
+
import { attachFontStyle, stackCss } from "./fonts.js";
|
|
3
|
+
import { el } from "./kit/dom.js";
|
|
4
|
+
import { stageSize } from "./stage-size.js";
|
|
5
|
+
/*
|
|
6
|
+
* Reading #fx back for a snapshot is slow, so its last PNG is used again until something touches the canvas: its
|
|
7
|
+
* width or height attribute, the loss or return of its context, or a call of any context method but these, which
|
|
8
|
+
* change neither the pixels nor the clip. A method a browser adds later is thus a touch.
|
|
9
|
+
*/
|
|
10
|
+
const FX_HARMLESS = new Set(('constructor beginPath moveTo lineTo bezierCurveTo quadraticCurveTo arc arcTo ellipse rect roundRect closePath measureText getImageData createImageData createLinearGradient createRadialGradient ' +
|
|
11
|
+
'createConicGradient createPattern getTransform getLineDash setLineDash isPointInPath isPointInStroke getContextAttributes isContextLost save translate scale rotate transform setTransform resetTransform').split(' '));
|
|
12
|
+
function watchFx(canvas, context) {
|
|
13
|
+
let touches = 0;
|
|
14
|
+
let clearedAt = -1;
|
|
15
|
+
let blankAt = -1;
|
|
16
|
+
let kept = null;
|
|
17
|
+
const proto = Object.getPrototypeOf(context);
|
|
18
|
+
const clearRect = context.clearRect;
|
|
19
|
+
const touch = () => {
|
|
20
|
+
touches++;
|
|
21
|
+
};
|
|
22
|
+
// On this one context, which every getContext('2d') on #fx returns: a film's own canvases keep the browser's methods.
|
|
23
|
+
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
24
|
+
const method = Object.getOwnPropertyDescriptor(proto, name)?.value;
|
|
25
|
+
if (typeof method !== 'function' || FX_HARMLESS.has(name))
|
|
26
|
+
continue;
|
|
27
|
+
Object.defineProperty(context, name, {
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
enumerable: true,
|
|
31
|
+
value(...args) {
|
|
32
|
+
touches++;
|
|
33
|
+
return Reflect.apply(method, this, args);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const sized = new RealMutationObserver(touch);
|
|
38
|
+
const sizedNow = () => {
|
|
39
|
+
if (sized.takeRecords().length)
|
|
40
|
+
touches++;
|
|
41
|
+
};
|
|
42
|
+
sized.observe(canvas, { attributes: true, attributeFilter: ['width', 'height'] });
|
|
43
|
+
canvas.addEventListener('contextlost', touch);
|
|
44
|
+
canvas.addEventListener('contextrestored', touch);
|
|
45
|
+
// The stage's own wipes are touches only where they may change something: a clip the film left on narrows a clear, and a reset drops it.
|
|
46
|
+
return {
|
|
47
|
+
canvas,
|
|
48
|
+
clear(width, height) {
|
|
49
|
+
sizedNow();
|
|
50
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
51
|
+
clearRect.call(context, 0, 0, width, height);
|
|
52
|
+
if (touches !== clearedAt)
|
|
53
|
+
clearedAt = ++touches;
|
|
54
|
+
},
|
|
55
|
+
reset() {
|
|
56
|
+
sizedNow();
|
|
57
|
+
// Giving a canvas its own width again blanks it and its state.
|
|
58
|
+
canvas.width = canvas.width;
|
|
59
|
+
sized.takeRecords();
|
|
60
|
+
if (touches !== blankAt)
|
|
61
|
+
blankAt = clearedAt = ++touches;
|
|
62
|
+
},
|
|
63
|
+
png() {
|
|
64
|
+
sizedNow();
|
|
65
|
+
if (kept === null || kept.at !== touches)
|
|
66
|
+
kept = { at: touches, url: canvas.toDataURL('image/png') };
|
|
67
|
+
return kept.url;
|
|
68
|
+
},
|
|
69
|
+
end() {
|
|
70
|
+
sized.disconnect();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Inside .words a heading or paragraph has no margin unless the film gives it one; alone in the first layer, any rule of the film's wins. */
|
|
75
|
+
export const noMargin = (within) => '@layer film-reset{' + within + '.words :is(h1,h2,h3,h4,h5,h6,p){margin:0}}';
|
|
76
|
+
const RESET_CSS = '#stage{text-rendering:geometricPrecision;-webkit-font-smoothing:antialiased;font-kerning:normal}' +
|
|
77
|
+
'#stage,#stage *,#stage *::before,#stage *::after{animation:none!important;transition:none!important;caret-color:transparent!important;scroll-behavior:auto!important}' +
|
|
78
|
+
'#stage ::-webkit-scrollbar{display:none}';
|
|
79
|
+
let parts = null;
|
|
80
|
+
/** The frame's size at the last fit. */
|
|
81
|
+
let fittedTo = { width: -1, height: -1 };
|
|
82
|
+
export const stageParts = () => parts;
|
|
83
|
+
export function builtStage() {
|
|
84
|
+
if (!parts)
|
|
85
|
+
throw new Error('the stage is not built');
|
|
86
|
+
return parts;
|
|
87
|
+
}
|
|
88
|
+
export function buildStage(width, height, background) {
|
|
89
|
+
parts?.root.remove();
|
|
90
|
+
parts?.fxWatch.end();
|
|
91
|
+
stageSize.width = width;
|
|
92
|
+
stageSize.height = height;
|
|
93
|
+
const root = el('div', { attrs: { id: 'film-root' }, style: 'position:fixed;inset:0;overflow:hidden;background:' + background });
|
|
94
|
+
const stage = el('div', {
|
|
95
|
+
attrs: { id: 'stage' },
|
|
96
|
+
style: `position:absolute;left:0;top:0;width:${width}px;height:${height}px;overflow:hidden;transform-origin:0 0;background:${background}`,
|
|
97
|
+
});
|
|
98
|
+
const fontStyle = document.createElement('style');
|
|
99
|
+
fontStyle.id = 'film-fonts';
|
|
100
|
+
attachFontStyle(fontStyle);
|
|
101
|
+
const reset = el('style', { attrs: { id: 'film-reset' }, text: stackCss() + RESET_CSS + noMargin('#stage ') });
|
|
102
|
+
const fxCanvas = document.createElement('canvas');
|
|
103
|
+
fxCanvas.id = 'fx';
|
|
104
|
+
fxCanvas.width = width;
|
|
105
|
+
fxCanvas.height = height;
|
|
106
|
+
fxCanvas.style.cssText = `position:absolute;left:0;top:0;width:${width}px;height:${height}px;pointer-events:none;z-index:2147483647`;
|
|
107
|
+
const fx = fxCanvas.getContext('2d');
|
|
108
|
+
if (!fx)
|
|
109
|
+
throw new Error('this browser gives no 2d canvas');
|
|
110
|
+
stage.append(fontStyle, reset, fxCanvas);
|
|
111
|
+
root.appendChild(stage);
|
|
112
|
+
document.body.appendChild(root);
|
|
113
|
+
parts = { root, stage, fontStyle, fxCanvas, fx, fxWatch: watchFx(fxCanvas, fx) };
|
|
114
|
+
fit();
|
|
115
|
+
return parts;
|
|
116
|
+
}
|
|
117
|
+
/** Scales the stage to the frame, centred. */
|
|
118
|
+
export function fit() {
|
|
119
|
+
if (!parts)
|
|
120
|
+
return;
|
|
121
|
+
fittedTo = { width: window.innerWidth, height: window.innerHeight };
|
|
122
|
+
const frameWidth = fittedTo.width || stageSize.width;
|
|
123
|
+
const frameHeight = fittedTo.height || stageSize.height;
|
|
124
|
+
const scale = Math.min(frameWidth / stageSize.width, frameHeight / stageSize.height) || 1;
|
|
125
|
+
stageSize.fit = scale;
|
|
126
|
+
const style = parts.stage.style;
|
|
127
|
+
style.transform = 'scale(' + scale + ')';
|
|
128
|
+
style.left = (frameWidth - stageSize.width * scale) / 2 + 'px';
|
|
129
|
+
style.top = (frameHeight - stageSize.height * scale) / 2 + 'px';
|
|
130
|
+
}
|
|
131
|
+
/** A new frame has no layout until it has its size: there is nothing to measure on the stage before then. */
|
|
132
|
+
export function laidOut() {
|
|
133
|
+
const stage = parts?.stage;
|
|
134
|
+
return !!stage && stage.getBoundingClientRect().width >= 1;
|
|
135
|
+
}
|
|
136
|
+
const LAYOUT_POLL_MS = 50;
|
|
137
|
+
/** Settles once the stage has a layout, or after `ms` without one. Timers, not animation frames: a hidden frame gets none. */
|
|
138
|
+
export function whenLaidOut(ms) {
|
|
139
|
+
return new Promise((resolve) => {
|
|
140
|
+
const until = realNow() + ms;
|
|
141
|
+
const look = () => {
|
|
142
|
+
if (laidOut() || realNow() >= until)
|
|
143
|
+
resolve();
|
|
144
|
+
else
|
|
145
|
+
setTimeout(look, LAYOUT_POLL_MS);
|
|
146
|
+
};
|
|
147
|
+
look();
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/** A film may have placed the stage itself: at an unchanged frame size it is left alone. */
|
|
151
|
+
export function fitIfResized() {
|
|
152
|
+
if (fittedTo.width !== window.innerWidth || fittedTo.height !== window.innerHeight)
|
|
153
|
+
fit();
|
|
154
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FilmSeconds } from '@open-artifact/motion-core';
|
|
2
|
+
/**
|
|
3
|
+
* The stretch of the film a moment lies in, as the record of changes made by hand counts it. name: a scene's id;
|
|
4
|
+
* 'film' in a film not made of scenes, whatever the chapter; null: a scene that is not final, where nothing is laid.
|
|
5
|
+
* lo and hi are its ends in seconds from base: a scene's count from its own start, a chapter's from the film's.
|
|
6
|
+
*/
|
|
7
|
+
export interface Stretch {
|
|
8
|
+
name: string | null;
|
|
9
|
+
base: number;
|
|
10
|
+
lo: number;
|
|
11
|
+
hi: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function stretchOf(T: FilmSeconds): Stretch;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { film } from "./film.js";
|
|
2
|
+
export function stretchOf(T) {
|
|
3
|
+
const parts = film.meta?.parts ?? [];
|
|
4
|
+
let i = 0;
|
|
5
|
+
while (i + 1 < parts.length && T >= (parts[i + 1]?.a0 ?? Infinity))
|
|
6
|
+
i++;
|
|
7
|
+
const part = parts[i];
|
|
8
|
+
if (!part || part.key === null) {
|
|
9
|
+
const a0 = part ? part.a0 : 0;
|
|
10
|
+
const nat = part ? part.nat : (film.cut?.duration ?? 0);
|
|
11
|
+
return { name: 'film', base: 0, lo: a0, hi: a0 + nat };
|
|
12
|
+
}
|
|
13
|
+
const key = part.key;
|
|
14
|
+
const scene = film.meta?.scenes?.find((each) => each.id === key);
|
|
15
|
+
return { name: scene && scene.state === 'final' ? key : null, base: part.a0, lo: 0, hi: part.nat };
|
|
16
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { StageText } from '../protocol.ts';
|
|
2
|
+
export declare function setTextsGuard(guard: () => boolean): void;
|
|
3
|
+
/** The words go with the next report: the one at 'watch' itself is the caller's to make. */
|
|
4
|
+
export declare function setWatch(on: boolean, places: boolean): void;
|
|
5
|
+
/** Sends the words; a frame without a layout yet has no places to tell, so they wait for it (see {@link retell}). */
|
|
6
|
+
export declare function tellTexts(): void;
|
|
7
|
+
/**
|
|
8
|
+
* One report when the resizing stops and the frame is laid out, not one for each resize: a report's hit tests are
|
|
9
|
+
* slow. Timers, not animation frames: a frame that is hidden gets none.
|
|
10
|
+
*/
|
|
11
|
+
export declare function retell(): void;
|
|
12
|
+
/** It hides what lies under it: a picture, or an opaque background, under no faded holder. */
|
|
13
|
+
export declare function paintsOver(element: Element, stage: HTMLElement, memo: Map<Element, boolean>): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Nothing opaque lies over the line: tried at its middle, then near its ends, where long words may run under a
|
|
16
|
+
* neighbour. `drawn`: a part of an inline svg, whose thin ink the points can miss; a hit on its own svg counts.
|
|
17
|
+
*/
|
|
18
|
+
export declare function seenAt(element: Element, line: DOMRect, stage: HTMLElement, memo: Map<Element, boolean>, drawn?: boolean): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Measured in the film's own pixels: on a small screen the whole picture may be a few pixels wide. places: each node
|
|
21
|
+
* is told the line of words it is part of, as a change by hand to its look would name it; not in a scene that is not
|
|
22
|
+
* final, where nothing of a person's is laid. The time this takes is not counted toward the time for hit tests.
|
|
23
|
+
*/
|
|
24
|
+
export declare function textNodes(stage: HTMLElement, places: boolean): StageText[];
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { filmTime, realNow } from "./determinism.js";
|
|
2
|
+
import { srect } from "./kit/geometry.js";
|
|
3
|
+
import { say } from "./port.js";
|
|
4
|
+
import { inOwnColors } from "./snapshot/serialize.js";
|
|
5
|
+
import { lineFor } from "./retouch/retouch.js";
|
|
6
|
+
import { fitIfResized, laidOut, stageParts } from "./stage.js";
|
|
7
|
+
import { stretchOf } from "./stretch.js";
|
|
8
|
+
/*
|
|
9
|
+
* While the page watches, it is sent the stage's text nodes in document order: at 'watch', after each paused frame,
|
|
10
|
+
* after a turn that drew moments of its own, and after a resize. Each time, only while the stage shows the moment the
|
|
11
|
+
* page asked for, and once the frame is laid out. Hidden nodes are sent too: without them, nodes that are apart would
|
|
12
|
+
* sit side by side, and a line of the script could be found across the gap.
|
|
13
|
+
*/
|
|
14
|
+
const MOST_NODES = 2000;
|
|
15
|
+
const MOST_CHARS = 60000;
|
|
16
|
+
const LONGEST_NODE = 301;
|
|
17
|
+
/** Hit testing slows as the stage fills: after this long in all, whatever is not hidden counts as seen. */
|
|
18
|
+
const HIT_TEST_MS = 40;
|
|
19
|
+
/** A wait for the resizing to stop, or for a layout, looks again after this long. */
|
|
20
|
+
const RETELL_MS = 100;
|
|
21
|
+
/** A frame that is still not laid out after this many looks has its words told as they are. */
|
|
22
|
+
const MOST_LOOKS = 30;
|
|
23
|
+
const watch = { on: false, places: false, resized: false, retelling: false };
|
|
24
|
+
/** Whether the stage shows the moment the page asked for; set at startup (render.ts knows). */
|
|
25
|
+
let showsAsked = () => true;
|
|
26
|
+
export function setTextsGuard(guard) {
|
|
27
|
+
showsAsked = guard;
|
|
28
|
+
}
|
|
29
|
+
/** The words go with the next report: the one at 'watch' itself is the caller's to make. */
|
|
30
|
+
export function setWatch(on, places) {
|
|
31
|
+
watch.on = on;
|
|
32
|
+
watch.places = on && places;
|
|
33
|
+
}
|
|
34
|
+
/** Sends the words; a frame without a layout yet has no places to tell, so they wait for it (see {@link retell}). */
|
|
35
|
+
export function tellTexts() {
|
|
36
|
+
const stage = stageParts()?.stage;
|
|
37
|
+
if (!watch.on || !stage)
|
|
38
|
+
return;
|
|
39
|
+
// Seen is a hit test, so the stage must fit the frame, which has its new size before it hears 'resize'.
|
|
40
|
+
fitIfResized();
|
|
41
|
+
if (!laidOut()) {
|
|
42
|
+
retell();
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
sendTexts(stage);
|
|
46
|
+
}
|
|
47
|
+
function sendTexts(stage) {
|
|
48
|
+
const nodes = watch.places
|
|
49
|
+
? inOwnColors(() => {
|
|
50
|
+
// The styles are worked out now, not on the time textNodes allows for hit tests.
|
|
51
|
+
void getComputedStyle(stage).display;
|
|
52
|
+
return textNodes(stage, true);
|
|
53
|
+
})
|
|
54
|
+
: textNodes(stage, false);
|
|
55
|
+
say({ type: 'texts', nodes });
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* One report when the resizing stops and the frame is laid out, not one for each resize: a report's hit tests are
|
|
59
|
+
* slow. Timers, not animation frames: a frame that is hidden gets none.
|
|
60
|
+
*/
|
|
61
|
+
export function retell() {
|
|
62
|
+
watch.resized = true;
|
|
63
|
+
if (watch.retelling || !watch.on)
|
|
64
|
+
return;
|
|
65
|
+
watch.retelling = true;
|
|
66
|
+
let looks = 0;
|
|
67
|
+
const rest = () => {
|
|
68
|
+
looks += 1;
|
|
69
|
+
if (watch.resized || (!laidOut() && looks < MOST_LOOKS)) {
|
|
70
|
+
watch.resized = false;
|
|
71
|
+
setTimeout(rest, RETELL_MS);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
watch.retelling = false;
|
|
75
|
+
const stage = stageParts()?.stage;
|
|
76
|
+
if (watch.on && stage && showsAsked()) {
|
|
77
|
+
fitIfResized();
|
|
78
|
+
sendTexts(stage);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
setTimeout(rest, RETELL_MS);
|
|
82
|
+
}
|
|
83
|
+
function alphaOf(colour) {
|
|
84
|
+
if (/^rgba\(/.test(colour))
|
|
85
|
+
return parseFloat(colour.split(',')[3] ?? '1');
|
|
86
|
+
if (colour.indexOf('/') > 0)
|
|
87
|
+
return parseFloat(colour.split('/')[1] ?? '1');
|
|
88
|
+
return 1;
|
|
89
|
+
}
|
|
90
|
+
/** It hides what lies under it: a picture, or an opaque background, under no faded holder. */
|
|
91
|
+
export function paintsOver(element, stage, memo) {
|
|
92
|
+
const known = memo.get(element);
|
|
93
|
+
if (known !== undefined)
|
|
94
|
+
return known;
|
|
95
|
+
const style = getComputedStyle(element);
|
|
96
|
+
let solid = /^(IMG|VIDEO|CANVAS)$/.test(element.tagName) || style.backgroundImage.indexOf('url(') >= 0 || alphaOf(style.backgroundColor) > 0.9;
|
|
97
|
+
for (let up = element; solid && up && up !== stage; up = up.parentElement)
|
|
98
|
+
if (parseFloat(getComputedStyle(up).opacity) < 0.9)
|
|
99
|
+
solid = false;
|
|
100
|
+
memo.set(element, solid);
|
|
101
|
+
return solid;
|
|
102
|
+
}
|
|
103
|
+
/** The point lies outside a box that clips the element. Any clip-path counts as clipping it, whatever its shape. */
|
|
104
|
+
function clippedAt(element, stage, x, y) {
|
|
105
|
+
for (let up = element; up && up !== stage.parentElement; up = up.parentElement) {
|
|
106
|
+
const style = getComputedStyle(up);
|
|
107
|
+
if (style.clipPath !== 'none')
|
|
108
|
+
return true;
|
|
109
|
+
if (up === element || (style.overflowX === 'visible' && style.overflowY === 'visible'))
|
|
110
|
+
continue;
|
|
111
|
+
const box = up.getBoundingClientRect();
|
|
112
|
+
if (x < box.left || x > box.right || y < box.top || y > box.bottom)
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Nothing opaque lies over the line: tried at its middle, then near its ends, where long words may run under a
|
|
119
|
+
* neighbour. `drawn`: a part of an inline svg, whose thin ink the points can miss; a hit on its own svg counts.
|
|
120
|
+
*/
|
|
121
|
+
export function seenAt(element, line, stage, memo, drawn = false) {
|
|
122
|
+
// Hit testing skips it, so it counts as seen.
|
|
123
|
+
if (getComputedStyle(element).pointerEvents === 'none')
|
|
124
|
+
return true;
|
|
125
|
+
const box = stage.getBoundingClientRect();
|
|
126
|
+
const left = Math.max(line.left, box.left);
|
|
127
|
+
const right = Math.min(line.right, box.right);
|
|
128
|
+
const y = (Math.max(line.top, box.top) + Math.min(line.bottom, box.bottom)) / 2;
|
|
129
|
+
if (!(right > left))
|
|
130
|
+
return false;
|
|
131
|
+
return [0.5, 0.1, 0.9].some((share) => {
|
|
132
|
+
const x = left + (right - left) * share;
|
|
133
|
+
for (const hit of document.elementsFromPoint(x, y)) {
|
|
134
|
+
if (hit === element || element.contains(hit))
|
|
135
|
+
return true;
|
|
136
|
+
if (hit.contains(element)) {
|
|
137
|
+
const svg = element instanceof SVGElement ? element.ownerSVGElement : null;
|
|
138
|
+
return (drawn && !!svg && svg.contains(hit)) || !clippedAt(element, stage, x, y);
|
|
139
|
+
}
|
|
140
|
+
if (paintsOver(hit, stage, memo))
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const checksVisibility = (element) => typeof element.checkVisibility !== 'function' || element.checkVisibility({ opacityProperty: true, visibilityProperty: true, checkOpacity: true, checkVisibilityCSS: true });
|
|
147
|
+
/**
|
|
148
|
+
* Measured in the film's own pixels: on a small screen the whole picture may be a few pixels wide. places: each node
|
|
149
|
+
* is told the line of words it is part of, as a change by hand to its look would name it; not in a scene that is not
|
|
150
|
+
* final, where nothing of a person's is laid. The time this takes is not counted toward the time for hit tests.
|
|
151
|
+
*/
|
|
152
|
+
export function textNodes(stage, places) {
|
|
153
|
+
const out = [];
|
|
154
|
+
let chars = 0;
|
|
155
|
+
let began = realNow();
|
|
156
|
+
let within = places ? stretchOf(filmTime()).name : null;
|
|
157
|
+
const lines = new Map();
|
|
158
|
+
const place = (node) => {
|
|
159
|
+
const p0 = realNow();
|
|
160
|
+
let at = null;
|
|
161
|
+
try {
|
|
162
|
+
at = lineFor(node, lines);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
within = null;
|
|
166
|
+
}
|
|
167
|
+
began += realNow() - p0;
|
|
168
|
+
return at;
|
|
169
|
+
};
|
|
170
|
+
const memo = new Map();
|
|
171
|
+
const walker = document.createTreeWalker(stage, NodeFilter.SHOW_TEXT);
|
|
172
|
+
const range = document.createRange();
|
|
173
|
+
for (let node = walker.nextNode(); node && out.length < MOST_NODES && chars < MOST_CHARS; node = walker.nextNode()) {
|
|
174
|
+
if (!(node instanceof Text))
|
|
175
|
+
continue;
|
|
176
|
+
let holder = node.parentElement;
|
|
177
|
+
// A caption is skipped, the stage's or one the cut draws with that class: its words are spoken.
|
|
178
|
+
if (!holder || !/\S/.test(node.data) || holder.closest('style,script,.film-caption'))
|
|
179
|
+
continue;
|
|
180
|
+
// display: contents has no box of its own to hide or to hit.
|
|
181
|
+
while (holder && holder !== stage && getComputedStyle(holder).display === 'contents')
|
|
182
|
+
holder = holder.parentElement;
|
|
183
|
+
if (!holder)
|
|
184
|
+
continue;
|
|
185
|
+
const item = { text: node.data.slice(0, LONGEST_NODE) };
|
|
186
|
+
chars += item.text.length;
|
|
187
|
+
out.push(item);
|
|
188
|
+
range.selectNodeContents(node);
|
|
189
|
+
const bounds = range.getBoundingClientRect();
|
|
190
|
+
if (!(bounds.width > 0 && bounds.height > 0))
|
|
191
|
+
continue;
|
|
192
|
+
const rect = srect(range);
|
|
193
|
+
const line = range.getClientRects()[0];
|
|
194
|
+
if (rect.w < 1 || rect.h < 1 || !line)
|
|
195
|
+
continue;
|
|
196
|
+
item.x = rect.x;
|
|
197
|
+
item.y = rect.y;
|
|
198
|
+
item.w = rect.w;
|
|
199
|
+
item.h = rect.h;
|
|
200
|
+
item.line = (rect.h * line.height) / bounds.height;
|
|
201
|
+
if (within !== null) {
|
|
202
|
+
const at = place(node);
|
|
203
|
+
if (at !== null)
|
|
204
|
+
item.id = within + ':' + at;
|
|
205
|
+
}
|
|
206
|
+
const spent = realNow() - began;
|
|
207
|
+
item.seen = checksVisibility(holder) && (spent > HIT_TEST_MS || seenAt(holder, line, stage, memo));
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { FilmSeconds } from '@open-artifact/motion-core';
|
|
2
|
+
/** What `kit.seek(video, s)` asked a clip to show in this render, instead of film time less its start. */
|
|
3
|
+
export declare const videoSeeks: Map<HTMLVideoElement, number>;
|
|
4
|
+
export declare function warnOnce(video: HTMLVideoElement, key: string, message: string): void;
|
|
5
|
+
/**
|
|
6
|
+
* The clip's own second at film time `t`, looped or held at its end once its duration is known.
|
|
7
|
+
* Half a millisecond on, so a frame boundary reached through float arithmetic lands on that frame.
|
|
8
|
+
*/
|
|
9
|
+
export declare function videoTime(video: HTMLVideoElement, t: FilmSeconds): {
|
|
10
|
+
at: number;
|
|
11
|
+
live: boolean;
|
|
12
|
+
explicit: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare function watchVideo(video: HTMLVideoElement): void;
|
|
15
|
+
export type VideoMode = 'play' | 'hold' | 'frame';
|
|
16
|
+
export interface VideoClock {
|
|
17
|
+
fps: number;
|
|
18
|
+
/** Film seconds per played second at this moment; clips play at it while the scenes are retimed. */
|
|
19
|
+
rate: number;
|
|
20
|
+
retimed: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* play: clips play along, corrected when they drift; hold: paused on their frame; frame: an exported frame,
|
|
24
|
+
* with a promise that settles once every clip shows its own.
|
|
25
|
+
*/
|
|
26
|
+
export declare function syncVideos(stage: HTMLElement | null, t: FilmSeconds, mode: VideoMode, clock: VideoClock, quick?: boolean): Promise<unknown> | null;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** What `kit.seek(video, s)` asked a clip to show in this render, instead of film time less its start. */
|
|
2
|
+
export const videoSeeks = new Map();
|
|
3
|
+
const watched = new WeakSet();
|
|
4
|
+
const warned = new WeakMap();
|
|
5
|
+
const slow = new WeakSet();
|
|
6
|
+
export function warnOnce(video, key, message) {
|
|
7
|
+
const said = warned.get(video) ?? new Set();
|
|
8
|
+
warned.set(video, said);
|
|
9
|
+
if (said.has(key))
|
|
10
|
+
return;
|
|
11
|
+
said.add(key);
|
|
12
|
+
console.warn('video ' + (video.id ? '#' + video.id + ' ' : '') + message);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The clip's own second at film time `t`, looped or held at its end once its duration is known.
|
|
16
|
+
* Half a millisecond on, so a frame boundary reached through float arithmetic lands on that frame.
|
|
17
|
+
*/
|
|
18
|
+
export function videoTime(video, t) {
|
|
19
|
+
const explicit = videoSeeks.has(video);
|
|
20
|
+
let at = explicit ? (videoSeeks.get(video) ?? 0) : t - (Number(video.getAttribute('data-start')) || 0);
|
|
21
|
+
const duration = video.duration;
|
|
22
|
+
let live = at >= 0;
|
|
23
|
+
if (duration > 0 && isFinite(duration)) {
|
|
24
|
+
if (video.loop) {
|
|
25
|
+
if (at >= 0)
|
|
26
|
+
at = at % duration;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
live = live && at < duration;
|
|
30
|
+
at = Math.min(at, duration - 0.001);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { at: Math.max(0, at) + 0.0005, live, explicit };
|
|
34
|
+
}
|
|
35
|
+
export function watchVideo(video) {
|
|
36
|
+
if (watched.has(video))
|
|
37
|
+
return;
|
|
38
|
+
watched.add(video);
|
|
39
|
+
video.playsInline = true;
|
|
40
|
+
video.autoplay = false;
|
|
41
|
+
const failed = () => {
|
|
42
|
+
console.error('video ' +
|
|
43
|
+
(video.id ? '#' + video.id + ' ' : '') +
|
|
44
|
+
'did not load (' +
|
|
45
|
+
(video.error ? 'code ' + video.error.code : 'no source') +
|
|
46
|
+
'): use an uploaded .mp4/.webm asset, a data: or a blob: URL');
|
|
47
|
+
};
|
|
48
|
+
video.addEventListener('error', failed);
|
|
49
|
+
if (video.error)
|
|
50
|
+
failed();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Resolves once the clip shows the frame it was sought to, or failed. A clip that misses the deadline is warned
|
|
54
|
+
* about once and afterwards waited on only briefly. quick: wait 250 ms at most, and leave its slow mark alone.
|
|
55
|
+
*/
|
|
56
|
+
function settled(video, quick) {
|
|
57
|
+
if ((!video.currentSrc && !video.src) || video.error || (!video.seeking && video.readyState >= 2))
|
|
58
|
+
return null;
|
|
59
|
+
const ms = quick || slow.has(video) ? 250 : 4000;
|
|
60
|
+
const events = ['seeked', 'loadeddata', 'error'];
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
const done = (ok) => {
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
for (const name of events)
|
|
65
|
+
video.removeEventListener(name, check);
|
|
66
|
+
if (!quick) {
|
|
67
|
+
if (ok)
|
|
68
|
+
slow.delete(video);
|
|
69
|
+
else {
|
|
70
|
+
slow.add(video);
|
|
71
|
+
warnOnce(video, 'slow', 'did not reach its frame within ' + ms + ' ms; exported frames may show a stale picture of it');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
resolve();
|
|
75
|
+
};
|
|
76
|
+
const check = () => {
|
|
77
|
+
if (video.error || (!video.seeking && video.readyState >= 2))
|
|
78
|
+
done(true);
|
|
79
|
+
};
|
|
80
|
+
const timer = setTimeout(() => done(false), ms);
|
|
81
|
+
for (const name of events)
|
|
82
|
+
video.addEventListener(name, check);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* play: clips play along, corrected when they drift; hold: paused on their frame; frame: an exported frame,
|
|
87
|
+
* with a promise that settles once every clip shows its own.
|
|
88
|
+
*/
|
|
89
|
+
export function syncVideos(stage, t, mode, clock, quick = false) {
|
|
90
|
+
if (!stage)
|
|
91
|
+
return null;
|
|
92
|
+
const waits = [];
|
|
93
|
+
const eps = 0.5 / (clock.fps || 60);
|
|
94
|
+
for (const video of stage.querySelectorAll('video')) {
|
|
95
|
+
const wait = syncVideo(video, t, mode, eps, clock, quick);
|
|
96
|
+
if (wait)
|
|
97
|
+
waits.push(wait);
|
|
98
|
+
}
|
|
99
|
+
return waits.length ? Promise.all(waits) : null;
|
|
100
|
+
}
|
|
101
|
+
function syncVideo(video, t, mode, eps, clock, quick) {
|
|
102
|
+
watchVideo(video);
|
|
103
|
+
video.muted = true;
|
|
104
|
+
if (video.error)
|
|
105
|
+
return null;
|
|
106
|
+
const time = videoTime(video, t);
|
|
107
|
+
if (mode === 'play' && time.live && !time.explicit) {
|
|
108
|
+
if (clock.retimed && Math.abs(video.playbackRate - clock.rate) > 1e-4)
|
|
109
|
+
video.playbackRate = clock.rate;
|
|
110
|
+
if ((video.paused || (!video.seeking && video.readyState >= 3)) && Math.abs(video.currentTime - time.at) > 0.25)
|
|
111
|
+
video.currentTime = time.at;
|
|
112
|
+
if (video.paused) {
|
|
113
|
+
video.play().catch((error) => {
|
|
114
|
+
const name = error instanceof Error ? error.name : String(error);
|
|
115
|
+
if (name !== 'AbortError')
|
|
116
|
+
warnOnce(video, 'play', 'could not play: ' + name);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (!video.paused)
|
|
122
|
+
video.pause();
|
|
123
|
+
if (Math.abs(video.currentTime - time.at) > eps)
|
|
124
|
+
video.currentTime = time.at;
|
|
125
|
+
return mode === 'frame' ? settled(video, quick) : null;
|
|
126
|
+
}
|
package/src/stills.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stills of a video artifact, drawn as its export draws them, by a headless Chromium driving the tools page (see
|
|
3
|
+
* `./tools`). It needs only Node (22 or later: it uses the built-in WebSocket) and a Chromium: `CHROME_PATH`, else one
|
|
4
|
+
* from Playwright's browser cache, else Chrome or Chromium where the system keeps it. It speaks the DevTools protocol
|
|
5
|
+
* itself, so it installs nothing. Shared by `scripts/stills.mjs` and the `open-artifact stills` command.
|
|
6
|
+
*/
|
|
7
|
+
import type { RenderMode } from './protocol.ts';
|
|
8
|
+
import type { ToolsFiles } from './tools/types.ts';
|
|
9
|
+
/** A stills request that cannot be met: printed as it is, without a stack. */
|
|
10
|
+
export declare class StillsError extends Error {
|
|
11
|
+
}
|
|
12
|
+
export interface StillsRequest {
|
|
13
|
+
/** The artifact folder: db/ (film/, modules/, assets/) and _blobs/. */
|
|
14
|
+
root: string;
|
|
15
|
+
/** Played seconds. */
|
|
16
|
+
times: number[];
|
|
17
|
+
/** Times the film's own size, above 0 and at most 4. */
|
|
18
|
+
scale: number;
|
|
19
|
+
mode: RenderMode;
|
|
20
|
+
/** Where the PNGs go; a new temporary folder when `null`. */
|
|
21
|
+
out: string | null;
|
|
22
|
+
}
|
|
23
|
+
export declare const STILLS_MAX_SCALE = 4;
|
|
24
|
+
/** The tools page's file name beside a built `open-artifact` command. */
|
|
25
|
+
export declare const STILLS_TOOLS_PAGE = "motion-tools.html";
|
|
26
|
+
/** Played seconds, comma-separated (`0.8,2.4,5`); `null` when any is not a second of the film. */
|
|
27
|
+
export declare function parseStillTimes(text: string): number[] | null;
|
|
28
|
+
/** The scale a still is drawn at, or `null` outside (0, 4]. */
|
|
29
|
+
export declare function parseStillScale(text: string | undefined): number | null;
|
|
30
|
+
/** The PNG a time is written to. */
|
|
31
|
+
export declare const stillFileName: (t: number) => string;
|
|
32
|
+
/** The film's documents (film/, modules/, assets/; never people's own data/) and the uploads its assets name. */
|
|
33
|
+
export declare function readStillsFilm(root: string, warn?: (line: string) => void): Required<Pick<ToolsFiles, 'documents' | 'blobs'>>;
|
|
34
|
+
/** A Chrome or Chromium to draw with: `CHROME_PATH`, Playwright's cache, then the system's. */
|
|
35
|
+
export declare function findChromium(env?: NodeJS.ProcessEnv): string;
|
|
36
|
+
/**
|
|
37
|
+
* Draws the stills asked for with the tools page's HTML (`buildToolsPage`, or the copy a build ships), printing
|
|
38
|
+
* through `say`: the film's problems, its size and length, each PNG's path, the score's counts and the film's own
|
|
39
|
+
* warnings and errors. Resolves to the exit code: 1 when the film did not load.
|
|
40
|
+
*/
|
|
41
|
+
export declare function drawStills(request: StillsRequest, toolsPage: string, say?: (line: string) => void): Promise<number>;
|