@motionscript/react 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.md +104 -3
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/manifest.json +13 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/motionscript-react.js +452 -0
- package/dist/motionscript-react.js.map +1 -0
- package/dist/ui/playhead.d.ts +26 -0
- package/dist/ui/playhead.d.ts.map +1 -0
- package/dist/ui/stage.d.ts +98 -0
- package/dist/ui/stage.d.ts.map +1 -0
- package/dist/ui/use-motion-player.d.ts +156 -0
- package/dist/ui/use-motion-player.d.ts.map +1 -0
- package/package.json +63 -3
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { isProjectDocument } from "@motionscript/core";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/ui/playhead.ts
|
|
5
|
+
function createPlayheadStore() {
|
|
6
|
+
let frame = 0;
|
|
7
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
8
|
+
return {
|
|
9
|
+
subscribe(onChange) {
|
|
10
|
+
listeners.add(onChange);
|
|
11
|
+
return () => {
|
|
12
|
+
listeners.delete(onChange);
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
getSnapshot: () => frame,
|
|
16
|
+
set(next) {
|
|
17
|
+
if (next === frame) return;
|
|
18
|
+
frame = next;
|
|
19
|
+
for (const notify of listeners) notify();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Subscribe *this* component to the playhead.
|
|
25
|
+
*
|
|
26
|
+
* Typed structurally rather than against `Player` so the store and the hook
|
|
27
|
+
* that reads it don't import each other.
|
|
28
|
+
*/
|
|
29
|
+
function usePlayhead(player) {
|
|
30
|
+
const { playhead } = player;
|
|
31
|
+
return useSyncExternalStore(playhead.subscribe, playhead.getSnapshot, playhead.getSnapshot);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/ui/use-motion-player.ts
|
|
35
|
+
/** Used only when neither the document nor the engine says otherwise. */
|
|
36
|
+
var FALLBACK_VIEWPORT = {
|
|
37
|
+
width: 1920,
|
|
38
|
+
height: 1080
|
|
39
|
+
};
|
|
40
|
+
var FALLBACK_FPS = 60;
|
|
41
|
+
var INITIAL_PRECOMP = {
|
|
42
|
+
discoveredTracks: 0,
|
|
43
|
+
totalTracks: 0,
|
|
44
|
+
complete: false
|
|
45
|
+
};
|
|
46
|
+
var NO_ERRORS = [];
|
|
47
|
+
function precompProgressOf(result) {
|
|
48
|
+
return {
|
|
49
|
+
discoveredTracks: result.tracks.filter((track) => track.discovered).length,
|
|
50
|
+
totalTracks: result.tracks.length,
|
|
51
|
+
complete: result.complete
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function samePrecomp(a, b) {
|
|
55
|
+
return a.discoveredTracks === b.discoveredTracks && a.totalTracks === b.totalTracks && a.complete === b.complete;
|
|
56
|
+
}
|
|
57
|
+
function sameErrors(a, b) {
|
|
58
|
+
return a.length === b.length && a.every((error, i) => {
|
|
59
|
+
const other = b[i];
|
|
60
|
+
return error.trackId === other.trackId && error.message === other.message && error.nodeId === other.nodeId && error.prop === other.prop && error.stack === other.stack;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** A state updater that keeps the held errors when the report says the same thing. */
|
|
64
|
+
function errorsFrom(reported) {
|
|
65
|
+
const next = reported.length === 0 ? NO_ERRORS : reported.slice();
|
|
66
|
+
return (held) => sameErrors(held, next) ? held : next;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Opens a session on `engine` and gives you the transport for it.
|
|
70
|
+
*
|
|
71
|
+
* ```tsx
|
|
72
|
+
* const player = useMotionPlayer({ engine, document: doc, loop: true });
|
|
73
|
+
* <MotionStage player={player} />
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function useMotionPlayer(options) {
|
|
77
|
+
const { engine, document, onFramePainted } = options;
|
|
78
|
+
const projectDoc = document && isProjectDocument(document) ? document : void 0;
|
|
79
|
+
const resolvedViewport = projectDoc?.viewport ?? options.viewport ?? engine?.viewport ?? FALLBACK_VIEWPORT;
|
|
80
|
+
const fps = projectDoc?.fps ?? options.fps ?? engine?.fps ?? FALLBACK_FPS;
|
|
81
|
+
const [viewport, setViewport] = useState(resolvedViewport);
|
|
82
|
+
if (viewport.width !== resolvedViewport.width || viewport.height !== resolvedViewport.height) setViewport(resolvedViewport);
|
|
83
|
+
const hasDocument = document !== void 0;
|
|
84
|
+
const [session, setSession] = useState(null);
|
|
85
|
+
/** Created once; stable for the hook's life, so `usePlayhead` never resubscribes. */
|
|
86
|
+
const [playhead] = useState(createPlayheadStore);
|
|
87
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
88
|
+
const [speed, setSpeedState] = useState(options.speed ?? 1);
|
|
89
|
+
const [muted, setMutedState] = useState(options.muted ?? false);
|
|
90
|
+
const [loop, setLoopState] = useState(options.loop ?? false);
|
|
91
|
+
const [loading, setLoading] = useState(true);
|
|
92
|
+
const [duration, setDuration] = useState(0);
|
|
93
|
+
const [errors, setErrors] = useState(NO_ERRORS);
|
|
94
|
+
const [precomp, setPrecomp] = useState(INITIAL_PRECOMP);
|
|
95
|
+
const totalFrames = Math.max(1, Math.round(duration * fps));
|
|
96
|
+
const controllerRef = useRef(null);
|
|
97
|
+
const sessionRef = useRef(null);
|
|
98
|
+
const documentRef = useRef(document);
|
|
99
|
+
const onFramePaintedRef = useRef(onFramePainted);
|
|
100
|
+
/** Monotonic seek id; only the latest may clear the loading flag. */
|
|
101
|
+
const seekTokenRef = useRef(0);
|
|
102
|
+
/** Read by the tick handler, which is installed once and must see the latest. */
|
|
103
|
+
const loopRef = useRef(loop);
|
|
104
|
+
const fpsRef = useRef(fps);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
onFramePaintedRef.current = onFramePainted;
|
|
107
|
+
loopRef.current = loop;
|
|
108
|
+
fpsRef.current = fps;
|
|
109
|
+
});
|
|
110
|
+
const [prevDocument, setPrevDocument] = useState(document);
|
|
111
|
+
if (document !== prevDocument) {
|
|
112
|
+
setPrevDocument(document);
|
|
113
|
+
if (document === void 0) {
|
|
114
|
+
setIsPlaying(false);
|
|
115
|
+
setDuration(0);
|
|
116
|
+
if (errors.length > 0) setErrors(NO_ERRORS);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Pull what the controller now knows: timeline length, errors, progress.
|
|
121
|
+
* Runs on every swap, so it keeps whatever did not change — a fresh array or
|
|
122
|
+
* object here is a new `Player` for every consumer on every edit.
|
|
123
|
+
*/
|
|
124
|
+
const sync = useCallback((pc) => {
|
|
125
|
+
setDuration(pc.totalDuration);
|
|
126
|
+
setErrors(errorsFrom(pc.buildErrors));
|
|
127
|
+
const progress = precompProgressOf(pc.precomp);
|
|
128
|
+
setPrecomp((held) => samePrecomp(held, progress) ? held : progress);
|
|
129
|
+
}, []);
|
|
130
|
+
/**
|
|
131
|
+
* Move the playhead and repaint, with the loading flag the UI paces off.
|
|
132
|
+
*
|
|
133
|
+
* Created once so every caller — the transport, the reconcile effect and
|
|
134
|
+
* the first paint after a session opens — shares one seek discipline.
|
|
135
|
+
*/
|
|
136
|
+
const [seekTo] = useState(() => (frame) => {
|
|
137
|
+
const pc = controllerRef.current;
|
|
138
|
+
if (!pc) return;
|
|
139
|
+
const last = Math.max(0, Math.round(pc.totalDuration * fpsRef.current) - 1);
|
|
140
|
+
const clamped = Math.min(Math.max(0, Math.round(frame)), last);
|
|
141
|
+
playhead.set(clamped);
|
|
142
|
+
const token = ++seekTokenRef.current;
|
|
143
|
+
setLoading(true);
|
|
144
|
+
pc.seek(clamped).then(() => {
|
|
145
|
+
if (token === seekTokenRef.current) setLoading(false);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
if (!engine || !document) return;
|
|
150
|
+
const opened = engine.open({
|
|
151
|
+
document,
|
|
152
|
+
viewport,
|
|
153
|
+
fps,
|
|
154
|
+
onPrecompProgress: () => sync(opened.controller)
|
|
155
|
+
});
|
|
156
|
+
const pc = opened.controller;
|
|
157
|
+
pc.onRenderError = (errs) => setErrors(errorsFrom(errs));
|
|
158
|
+
pc.onFramePainted = (frame) => onFramePaintedRef.current?.(frame);
|
|
159
|
+
pc.onTime((t) => {
|
|
160
|
+
const frame = Math.trunc(t * fpsRef.current);
|
|
161
|
+
playhead.set(frame);
|
|
162
|
+
if (frame < Math.round(pc.totalDuration * fpsRef.current) - 1) return;
|
|
163
|
+
if (loopRef.current) pc.seekWhilePlaying(0);
|
|
164
|
+
else setIsPlaying(false);
|
|
165
|
+
});
|
|
166
|
+
controllerRef.current = pc;
|
|
167
|
+
sessionRef.current = opened;
|
|
168
|
+
documentRef.current = document;
|
|
169
|
+
setSession(opened);
|
|
170
|
+
sync(pc);
|
|
171
|
+
setLoading(true);
|
|
172
|
+
return () => {
|
|
173
|
+
controllerRef.current = null;
|
|
174
|
+
sessionRef.current = null;
|
|
175
|
+
setSession(null);
|
|
176
|
+
opened.dispose();
|
|
177
|
+
};
|
|
178
|
+
}, [
|
|
179
|
+
engine,
|
|
180
|
+
hasDocument,
|
|
181
|
+
viewport.width,
|
|
182
|
+
viewport.height,
|
|
183
|
+
fps,
|
|
184
|
+
sync
|
|
185
|
+
]);
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
if (documentRef.current === document) return;
|
|
188
|
+
documentRef.current = document;
|
|
189
|
+
const live = sessionRef.current;
|
|
190
|
+
if (!live || !document) return;
|
|
191
|
+
const pc = live.controller;
|
|
192
|
+
live.replace(document);
|
|
193
|
+
sync(pc);
|
|
194
|
+
const last = Math.max(0, Math.round(pc.totalDuration * fpsRef.current) - 1);
|
|
195
|
+
playhead.set(Math.min(playhead.getSnapshot(), last));
|
|
196
|
+
const token = ++seekTokenRef.current;
|
|
197
|
+
setLoading(true);
|
|
198
|
+
pc.whenReplaced().then(() => {
|
|
199
|
+
if (token === seekTokenRef.current) setLoading(false);
|
|
200
|
+
});
|
|
201
|
+
}, [
|
|
202
|
+
document,
|
|
203
|
+
sync,
|
|
204
|
+
playhead
|
|
205
|
+
]);
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
const pc = controllerRef.current;
|
|
208
|
+
if (!pc) return;
|
|
209
|
+
if (isPlaying) pc.play(speed);
|
|
210
|
+
else pc.pause();
|
|
211
|
+
}, [
|
|
212
|
+
session,
|
|
213
|
+
isPlaying,
|
|
214
|
+
speed
|
|
215
|
+
]);
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
controllerRef.current?.setMuted(muted);
|
|
218
|
+
}, [session, muted]);
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
seekTo(playhead.getSnapshot());
|
|
221
|
+
}, [
|
|
222
|
+
session,
|
|
223
|
+
playhead,
|
|
224
|
+
seekTo
|
|
225
|
+
]);
|
|
226
|
+
const [actions] = useState(() => {
|
|
227
|
+
const seek = (frame) => {
|
|
228
|
+
setIsPlaying(false);
|
|
229
|
+
seekTo(frame);
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
playhead,
|
|
233
|
+
play: () => setIsPlaying(true),
|
|
234
|
+
pause: () => setIsPlaying(false),
|
|
235
|
+
toggle: () => setIsPlaying((playing) => {
|
|
236
|
+
const pc = controllerRef.current;
|
|
237
|
+
if (!playing && pc) {
|
|
238
|
+
const last = Math.round(pc.totalDuration * fpsRef.current) - 1;
|
|
239
|
+
if (playhead.getSnapshot() >= last) pc.seek(0);
|
|
240
|
+
}
|
|
241
|
+
return !playing;
|
|
242
|
+
}),
|
|
243
|
+
seek,
|
|
244
|
+
step: (delta) => seek(playhead.getSnapshot() + delta),
|
|
245
|
+
seekWhilePlaying: (frame) => controllerRef.current?.seekWhilePlaying(frame),
|
|
246
|
+
setSpeed: (next) => setSpeedState(next),
|
|
247
|
+
setMuted: (next) => setMutedState(next),
|
|
248
|
+
setLoop: (next) => setLoopState(next),
|
|
249
|
+
screenshot: async () => controllerRef.current?.screenshot() ?? void 0,
|
|
250
|
+
repaint: () => controllerRef.current?.repaint(),
|
|
251
|
+
getTreeState: () => controllerRef.current?.getTreeState() ?? null,
|
|
252
|
+
getNodeProps: (nodeId) => controllerRef.current?.getNodeState(nodeId) ?? null,
|
|
253
|
+
getTrackSpans: () => controllerRef.current?.spans ?? [],
|
|
254
|
+
whenReplaced: async () => {
|
|
255
|
+
await controllerRef.current?.whenReplaced();
|
|
256
|
+
},
|
|
257
|
+
getNodeBox: (path) => controllerRef.current?.getNodeBox(path) ?? null,
|
|
258
|
+
getNodePropsAt: (path) => controllerRef.current?.getNodePropsAt(path) ?? null,
|
|
259
|
+
getNodeBoxes: () => controllerRef.current?.getNodeBoxes() ?? [],
|
|
260
|
+
projectNode3D: (path, parentPoints, localPoints) => controllerRef.current?.projectNode3D(path, parentPoints, localPoints) ?? null,
|
|
261
|
+
parentPlanePoint: (path, point) => controllerRef.current?.parentPlanePoint(path, point) ?? null,
|
|
262
|
+
getTextLayout: (path) => controllerRef.current?.getTextLayout(path) ?? null,
|
|
263
|
+
pickNode: (point, tolerance) => controllerRef.current?.pickNode(point, tolerance) ?? null,
|
|
264
|
+
setNodeOverride: (path, props) => controllerRef.current?.setNodeOverride(path, props),
|
|
265
|
+
clearNodeOverrides: (path) => controllerRef.current?.clearNodeOverrides(path)
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
return useMemo(() => ({
|
|
269
|
+
...actions,
|
|
270
|
+
session,
|
|
271
|
+
viewport,
|
|
272
|
+
fps,
|
|
273
|
+
isPlaying,
|
|
274
|
+
speed,
|
|
275
|
+
muted,
|
|
276
|
+
loop,
|
|
277
|
+
loading,
|
|
278
|
+
duration,
|
|
279
|
+
totalFrames,
|
|
280
|
+
errors,
|
|
281
|
+
precomp
|
|
282
|
+
}), [
|
|
283
|
+
actions,
|
|
284
|
+
session,
|
|
285
|
+
viewport,
|
|
286
|
+
fps,
|
|
287
|
+
isPlaying,
|
|
288
|
+
speed,
|
|
289
|
+
muted,
|
|
290
|
+
loop,
|
|
291
|
+
loading,
|
|
292
|
+
duration,
|
|
293
|
+
totalFrames,
|
|
294
|
+
errors,
|
|
295
|
+
precomp
|
|
296
|
+
]);
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/ui/stage.tsx
|
|
300
|
+
/**
|
|
301
|
+
* What every stage that doesn't ask for a view gets: the whole frame, 1:1.
|
|
302
|
+
*
|
|
303
|
+
* A shared frozen object rather than a literal, so the view effect's dependency
|
|
304
|
+
* on its fields is stable across renders for the overwhelming majority of
|
|
305
|
+
* stages, which never pass `view` at all.
|
|
306
|
+
*/
|
|
307
|
+
var IDENTITY_VIEW = Object.freeze({
|
|
308
|
+
zoom: 1,
|
|
309
|
+
x: 0,
|
|
310
|
+
y: 0
|
|
311
|
+
});
|
|
312
|
+
/**
|
|
313
|
+
* A canvas bound to a {@link Player}'s session.
|
|
314
|
+
*
|
|
315
|
+
* The stage owns how the frame is rasterized — resolution, view, clipping — and
|
|
316
|
+
* nothing about what is in it or when. A session drives **one** surface, so one
|
|
317
|
+
* stage per player: a second would take the surface from the first.
|
|
318
|
+
*/
|
|
319
|
+
function MotionStage({ player, renderScale = 1, surfaceSize, view, clipToFrame, frameBackground, className, style }) {
|
|
320
|
+
const canvasRef = useRef(null);
|
|
321
|
+
const { session, viewport } = player;
|
|
322
|
+
/**
|
|
323
|
+
* The canvas's backing store, in device pixels. `viewport × renderScale` in
|
|
324
|
+
* the default case, where the canvas *is* the frame; with `surfaceSize` it
|
|
325
|
+
* is the viewing area instead.
|
|
326
|
+
*
|
|
327
|
+
* Floored at 1 so a degenerate scale can't ask CanvasKit for a zero-sized
|
|
328
|
+
* surface, which fails the surface creation outright.
|
|
329
|
+
*/
|
|
330
|
+
const bufferWidth = Math.max(1, Math.round((surfaceSize?.width ?? viewport.width) * renderScale));
|
|
331
|
+
const bufferHeight = Math.max(1, Math.round((surfaceSize?.height ?? viewport.height) * renderScale));
|
|
332
|
+
/**
|
|
333
|
+
* The view transform to draw with, and the frame to clip and anchor against.
|
|
334
|
+
* Only meaningful together, so they resolve in one place: no `surfaceSize`
|
|
335
|
+
* means the canvas is the frame, and both revert to identity.
|
|
336
|
+
*/
|
|
337
|
+
const activeView = surfaceSize && view ? view : IDENTITY_VIEW;
|
|
338
|
+
const activeFrame = surfaceSize && view ? viewport : null;
|
|
339
|
+
const activeClipToFrame = surfaceSize && view ? clipToFrame ?? true : true;
|
|
340
|
+
const activeFrameBackground = surfaceSize && view ? frameBackground ?? "black" : "black";
|
|
341
|
+
const viewRef = useRef(activeView);
|
|
342
|
+
const frameRectRef = useRef(activeFrame);
|
|
343
|
+
const clipToFrameRef = useRef(activeClipToFrame);
|
|
344
|
+
const frameBackgroundRef = useRef(activeFrameBackground);
|
|
345
|
+
/**
|
|
346
|
+
* The live render context.
|
|
347
|
+
*
|
|
348
|
+
* Held here rather than reached through `player.session` at each use because
|
|
349
|
+
* it is mutable external state — a Skia context whose knobs are written, not
|
|
350
|
+
* a value derived from a prop — and writing through the prop is what the
|
|
351
|
+
* immutability lint is there to catch everywhere it *isn't* one of these.
|
|
352
|
+
*/
|
|
353
|
+
const ctxRef = useRef(null);
|
|
354
|
+
useEffect(() => {
|
|
355
|
+
viewRef.current = activeView;
|
|
356
|
+
frameRectRef.current = activeFrame;
|
|
357
|
+
clipToFrameRef.current = activeClipToFrame;
|
|
358
|
+
frameBackgroundRef.current = activeFrameBackground;
|
|
359
|
+
ctxRef.current = session ? session.surface : null;
|
|
360
|
+
});
|
|
361
|
+
/**
|
|
362
|
+
* Bind the surface, and rebuild it when the backing store changes.
|
|
363
|
+
*
|
|
364
|
+
* The order matters and is why this is an effect rather than a callback:
|
|
365
|
+
* React has already written the canvas's new `width`/`height` by the time
|
|
366
|
+
* effects run, and CanvasKit reads exactly those when it builds the surface
|
|
367
|
+
* (`MakeOnScreenGLSurface(ctx, canvas.width, canvas.height)`). Mounting
|
|
368
|
+
* before the resize would produce a surface of the previous size.
|
|
369
|
+
*
|
|
370
|
+
* `repaint()` afterwards because a new surface starts blank and the clock is
|
|
371
|
+
* not necessarily running to fill it — a paused editor would otherwise show
|
|
372
|
+
* black until something else moved.
|
|
373
|
+
*/
|
|
374
|
+
useEffect(() => {
|
|
375
|
+
const canvas = canvasRef.current;
|
|
376
|
+
const ctx = ctxRef.current;
|
|
377
|
+
if (!session || !canvas || !ctx) return;
|
|
378
|
+
ctx.pixelRatio = renderScale;
|
|
379
|
+
ctx.view = viewRef.current;
|
|
380
|
+
ctx.frame = frameRectRef.current;
|
|
381
|
+
ctx.clipToFrame = clipToFrameRef.current;
|
|
382
|
+
ctx.frameBackground = frameBackgroundRef.current;
|
|
383
|
+
ctx.mount(canvas);
|
|
384
|
+
session.controller.repaint();
|
|
385
|
+
}, [
|
|
386
|
+
session,
|
|
387
|
+
renderScale,
|
|
388
|
+
bufferWidth,
|
|
389
|
+
bufferHeight
|
|
390
|
+
]);
|
|
391
|
+
/**
|
|
392
|
+
* Apply {@link MotionStageProps.view} — a repaint and nothing more.
|
|
393
|
+
*
|
|
394
|
+
* Depends on the fields rather than the object so a caller passing a fresh
|
|
395
|
+
* `{zoom, x, y}` each render — which is every caller, since it comes from a
|
|
396
|
+
* gesture — doesn't repaint on renders where the view didn't move.
|
|
397
|
+
*/
|
|
398
|
+
useEffect(() => {
|
|
399
|
+
const ctx = ctxRef.current;
|
|
400
|
+
if (!session || !ctx) return;
|
|
401
|
+
ctx.view = {
|
|
402
|
+
zoom: activeView.zoom,
|
|
403
|
+
x: activeView.x,
|
|
404
|
+
y: activeView.y
|
|
405
|
+
};
|
|
406
|
+
ctx.frame = activeFrame;
|
|
407
|
+
ctx.clipToFrame = activeClipToFrame;
|
|
408
|
+
ctx.frameBackground = activeFrameBackground;
|
|
409
|
+
session.controller.repaint();
|
|
410
|
+
}, [
|
|
411
|
+
session,
|
|
412
|
+
activeView.zoom,
|
|
413
|
+
activeView.x,
|
|
414
|
+
activeView.y,
|
|
415
|
+
activeFrame?.width,
|
|
416
|
+
activeFrame?.height,
|
|
417
|
+
activeClipToFrame,
|
|
418
|
+
activeFrameBackground
|
|
419
|
+
]);
|
|
420
|
+
return /* @__PURE__ */ jsx("div", {
|
|
421
|
+
className,
|
|
422
|
+
style: {
|
|
423
|
+
position: "relative",
|
|
424
|
+
width: "100%",
|
|
425
|
+
height: "100%",
|
|
426
|
+
...style
|
|
427
|
+
},
|
|
428
|
+
children: /* @__PURE__ */ jsx("canvas", {
|
|
429
|
+
ref: canvasRef,
|
|
430
|
+
width: bufferWidth,
|
|
431
|
+
height: bufferHeight,
|
|
432
|
+
style: {
|
|
433
|
+
display: session ? "block" : "none",
|
|
434
|
+
...surfaceSize ? {
|
|
435
|
+
position: "absolute",
|
|
436
|
+
left: "50%",
|
|
437
|
+
top: "50%",
|
|
438
|
+
width: `${surfaceSize.width}px`,
|
|
439
|
+
height: `${surfaceSize.height}px`,
|
|
440
|
+
transform: "translate(-50%, -50%)"
|
|
441
|
+
} : {
|
|
442
|
+
width: "100%",
|
|
443
|
+
height: "100%"
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
})
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
//#endregion
|
|
450
|
+
export { MotionStage, useMotionPlayer, usePlayhead };
|
|
451
|
+
|
|
452
|
+
//# sourceMappingURL=motionscript-react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motionscript-react.js","names":[],"sources":["../src/ui/playhead.ts","../src/ui/use-motion-player.ts","../src/ui/stage.tsx"],"sourcesContent":["import { useSyncExternalStore } from \"react\";\n\n/**\n * The playhead, held outside React.\n *\n * A frame number written at clock rate through `useState` re-renders whoever\n * owns the player, and on a high-refresh display that is faster than the\n * project's own frame rate. Nothing in a render depends on it: the controller\n * paints straight onto the canvas and React is not involved. So the number\n * lives here, and only the components that actually display it subscribe.\n */\nexport interface PlayheadStore {\n subscribe(onChange: () => void): () => void;\n getSnapshot(): number;\n /** @internal — written by the player that owns this store. */\n set(frame: number): void;\n}\n\nexport function createPlayheadStore(): PlayheadStore {\n let frame = 0;\n const listeners = new Set<() => void>();\n return {\n subscribe(onChange) {\n listeners.add(onChange);\n return () => {\n listeners.delete(onChange);\n };\n },\n getSnapshot: () => frame,\n set(next) {\n if (next === frame) return;\n frame = next;\n for (const notify of listeners) notify();\n },\n };\n}\n\n/**\n * Subscribe *this* component to the playhead.\n *\n * Typed structurally rather than against `Player` so the store and the hook\n * that reads it don't import each other.\n */\nexport function usePlayhead(player: { playhead: PlayheadStore }): number {\n const { playhead } = player;\n return useSyncExternalStore(playhead.subscribe, playhead.getSnapshot, playhead.getSnapshot);\n}\n","import { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\n\nimport { isProjectDocument } from \"@motionscript/core\";\n\nimport type {\n BuildError,\n Engine,\n NodeBox,\n Node3DFrame,\n NodeOverride,\n NodeState,\n NodeTextLayout,\n PlaybackController,\n AssetTimelineResult,\n ProjectDocument,\n SceneDocument,\n Session,\n Size2D,\n TrackSpan,\n TreeState,\n Vector2,\n Vector3,\n} from \"@motionscript/core\";\n\nimport { createPlayheadStore, type PlayheadStore } from \"./playhead\";\n\n/** Used only when neither the document nor the engine says otherwise. */\nconst FALLBACK_VIEWPORT: Size2D = { width: 1920, height: 1080 };\nconst FALLBACK_FPS = 60;\n\n/**\n * How far background discovery has got. Only the tracks on frame 0 are\n * discovered before the player mounts; the rest stream in behind it (see\n * `AssetTimeline.runAsync`). The timeline's length never waits on it — every track\n * declares its own.\n */\nexport interface PrecompProgress {\n /** Tracks discovered so far. */\n discoveredTracks: number;\n /** Tracks in the project. */\n totalTracks: number;\n /** True once every track has been discovered. */\n complete: boolean;\n}\n\nconst INITIAL_PRECOMP: PrecompProgress = { discoveredTracks: 0, totalTracks: 0, complete: false };\nconst NO_ERRORS: BuildError[] = [];\n\nfunction precompProgressOf(result: AssetTimelineResult): PrecompProgress {\n return {\n discoveredTracks: result.tracks.filter(track => track.discovered).length,\n totalTracks: result.tracks.length,\n complete: result.complete,\n };\n}\n\nfunction samePrecomp(a: PrecompProgress, b: PrecompProgress): boolean {\n return a.discoveredTracks === b.discoveredTracks && a.totalTracks === b.totalTracks && a.complete === b.complete;\n}\n\nfunction sameErrors(a: readonly BuildError[], b: readonly BuildError[]): boolean {\n return a.length === b.length && a.every((error, i) => {\n const other = b[i];\n return error.trackId === other.trackId && error.message === other.message\n && error.nodeId === other.nodeId && error.prop === other.prop && error.stack === other.stack;\n });\n}\n\n/** A state updater that keeps the held errors when the report says the same thing. */\nfunction errorsFrom(reported: readonly BuildError[]): (held: BuildError[]) => BuildError[] {\n const next = reported.length === 0 ? NO_ERRORS : reported.slice();\n return held => (sameErrors(held, next) ? held : next);\n}\n\nexport interface MotionPlayerOptions {\n /**\n * The engine this player opens its session on.\n *\n * Nullable on purpose: building one is asynchronous\n * (`await createBrowserEngine(...)`), so a host holds it in state and it is\n * `null` for the first render or two. The player no-ops until it arrives,\n * which is what keeps the hook out of a conditional.\n *\n * **Create it once, and share it.** It holds CanvasKit and the exporter, so\n * a fresh one per render rebuilds the WebGL surface behind every player on\n * it. Identity-compared: a different engine is a different CanvasKit, and so\n * a full rebuild.\n */\n engine: Engine | null | undefined;\n /**\n * What to play. Nothing opens until there is one.\n *\n * A **project** document is the whole answer: it carries its own viewport,\n * fps, theme and tracks. A **scene** document (still or composition) is one\n * clip and carries none of that, so `viewport` and `fps` must come from here\n * or from the engine.\n *\n * Content-diffed, with identity as the fast path: a new document is reconciled\n * onto the running session, each track's built tree patched in place and\n * rebuilt only when its change cannot be patched. Treat documents as\n * immutable — an edit made in place to one the player already holds is not\n * seen.\n *\n * A new document keeps the player's place: the playhead stays where it was\n * (clamped when the timeline got shorter), and playback carries on if it was\n * running. Seek or pause yourself when a swap means \"start over\".\n */\n document?: ProjectDocument | SceneDocument;\n /** Overrides the engine's, for a source that carries none. */\n viewport?: Size2D;\n /** Overrides the engine's, for a source that carries none. */\n fps?: number;\n /** Initial playback rate. Change it later with {@link Player.setSpeed}. */\n speed?: number;\n /** Initial mute state. Change it later with {@link Player.setMuted}. */\n muted?: boolean;\n /** Initial loop state. Change it later with {@link Player.setLoop}. */\n loop?: boolean;\n /**\n * Called **synchronously**, on the same task as the draw, each time a frame\n * has been painted — whether by the clock, a seek, or a document swap.\n *\n * Not the playhead, which reports where the *clock* is. A seek is\n * asynchronous, so between the two there is a window in which the number has\n * moved and the canvas has not. Reach for this to keep an overlay — a\n * selection box, a caret, a handle — in step with the pixels, and keep the\n * listener cheap: it runs on the render loop, before the browser has had a\n * chance to composite.\n *\n * Read through a ref, so changing it costs nothing.\n */\n onFramePainted?: (frame: number) => void;\n}\n\n/**\n * One document, its transport, and the session driving it. No DOM.\n *\n * Hand it to a {@link MotionStage} to put pixels on screen. Two calls to\n * {@link useMotionPlayer} produce two of these with nothing shared but the\n * engine — separate playheads, clocks, audio devices and transports.\n */\nexport interface Player {\n /** The live session, or `null` before the engine and a document arrive. */\n readonly session: Session | null;\n /** The playhead, read with `usePlayhead(player)`. Stable for the hook's life. */\n readonly playhead: PlayheadStore;\n /** Resolved from the document, else the engine, else 1920×1080. */\n readonly viewport: Size2D;\n /** Resolved from the document, else the engine, else 60. */\n readonly fps: number;\n\n readonly isPlaying: boolean;\n readonly speed: number;\n readonly muted: boolean;\n readonly loop: boolean;\n /** True while a seek or a document swap is in flight. */\n readonly loading: boolean;\n /** The project's length, known from the document before any track is discovered. */\n readonly duration: number;\n /** {@link duration} in frames, floored at 1. */\n readonly totalFrames: number;\n readonly errors: BuildError[];\n readonly precomp: PrecompProgress;\n\n play(): void;\n pause(): void;\n toggle(): void;\n /** Seek while paused. Clamped to the timeline. */\n seek(frame: number): void;\n /** Seek relative to where the playhead is now. */\n step(delta: number): void;\n /** Seek without interrupting playback. */\n seekWhilePlaying(frame: number): void;\n setSpeed(speed: number): void;\n setMuted(muted: boolean): void;\n setLoop(loop: boolean): void;\n\n screenshot(): Promise<string | undefined>;\n /** Re-layout and repaint the current frame — no re-evaluation. */\n repaint(): void;\n\n getTreeState(): TreeState | null;\n getNodeProps(nodeId: string): NodeState | null;\n /** Where each track sits on the timeline, in document order. */\n getTrackSpans(): readonly TrackSpan[];\n /**\n * Resolves once the paint a document swap scheduled has landed. The swap\n * does not paint before its assets have loaded, so await this before\n * screenshotting or reading the tree.\n */\n whenReplaced(): Promise<void>;\n\n // ---- Direct manipulation ----------------------------------------------\n // Points are in viewport space — origin at the viewport centre, y-up, in\n // this player's `viewport` pixels.\n\n getNodeBox(path: string): NodeBox | null;\n getNodePropsAt(path: string): NodeState[\"properties\"] | null;\n getNodeBoxes(): NodeBox[];\n projectNode3D(\n path: string,\n parentPoints: readonly Vector3[],\n localPoints: readonly Vector3[],\n ): Node3DFrame | null;\n /**\n * Where `point` lands on the plane the node at `path` is positioned in, in\n * that plane's own y-up frame. Two answers differ by the `x`/`y` change that\n * moves the node between them — which a screen delta no longer is once the\n * node sits in a 2.5D space.\n */\n parentPlanePoint(path: string, point: Vector2): Vector2 | null;\n getTextLayout(path: string): NodeTextLayout | null;\n /** `tolerance` is grab-slop in scene units. */\n pickNode(point: Vector2, tolerance?: number): NodeBox | null;\n /** Layer transient props over a node without rebuilding its track. */\n setNodeOverride(path: string, props: NodeOverride): void;\n clearNodeOverrides(path?: string): void;\n}\n\n/**\n * Opens a session on `engine` and gives you the transport for it.\n *\n * ```tsx\n * const player = useMotionPlayer({ engine, document: doc, loop: true });\n * <MotionStage player={player} />\n * ```\n */\nexport function useMotionPlayer(options: MotionPlayerOptions): Player {\n const { engine, document, onFramePainted } = options;\n\n // Read straight off the document where it says so, else the caller's, else\n // the engine's. All plain JSON, so this resolves during render, before any\n // session exists — which is what lets a stage size its canvas on the first\n // pass rather than after a mount.\n const projectDoc = document && isProjectDocument(document) ? document : undefined;\n const resolvedViewport = projectDoc?.viewport ?? options.viewport ?? engine?.viewport ?? FALLBACK_VIEWPORT;\n const fps = projectDoc?.fps ?? options.fps ?? engine?.fps ?? FALLBACK_FPS;\n\n // Held by size: a host that rebuilds its document per edit rebuilds this\n // object with it, and a new one would hand every consumer a new `Player`.\n const [viewport, setViewport] = useState(resolvedViewport);\n if (viewport.width !== resolvedViewport.width || viewport.height !== resolvedViewport.height) {\n setViewport(resolvedViewport);\n }\n const hasDocument = document !== undefined;\n\n const [session, setSession] = useState<Session | null>(null);\n /** Created once; stable for the hook's life, so `usePlayhead` never resubscribes. */\n const [playhead] = useState(createPlayheadStore);\n\n const [isPlaying, setIsPlaying] = useState(false);\n const [speed, setSpeedState] = useState(options.speed ?? 1);\n const [muted, setMutedState] = useState(options.muted ?? false);\n const [loop, setLoopState] = useState(options.loop ?? false);\n const [loading, setLoading] = useState(true);\n const [duration, setDuration] = useState(0);\n const [errors, setErrors] = useState<BuildError[]>(NO_ERRORS);\n const [precomp, setPrecomp] = useState<PrecompProgress>(INITIAL_PRECOMP);\n\n const totalFrames = Math.max(1, Math.round(duration * fps));\n\n // Stable across renders so the action callbacks below never need rebuilding.\n const controllerRef = useRef<PlaybackController | null>(null);\n const sessionRef = useRef<Session | null>(null);\n const documentRef = useRef(document);\n const onFramePaintedRef = useRef(onFramePainted);\n /** Monotonic seek id; only the latest may clear the loading flag. */\n const seekTokenRef = useRef(0);\n /** Read by the tick handler, which is installed once and must see the latest. */\n const loopRef = useRef(loop);\n const fpsRef = useRef(fps);\n\n useEffect(() => {\n onFramePaintedRef.current = onFramePainted;\n loopRef.current = loop;\n fpsRef.current = fps;\n });\n\n // An edit keeps the transport and its errors — the reconcile effect below\n // re-reads both off the session, so an error the edit did not fix never\n // blinks out for a commit. Only losing the document stops it, during render\n // so nothing draws against a timeline that is gone.\n const [prevDocument, setPrevDocument] = useState(document);\n if (document !== prevDocument) {\n setPrevDocument(document);\n if (document === undefined) {\n setIsPlaying(false);\n setDuration(0);\n if (errors.length > 0) setErrors(NO_ERRORS);\n }\n }\n\n /**\n * Pull what the controller now knows: timeline length, errors, progress.\n * Runs on every swap, so it keeps whatever did not change — a fresh array or\n * object here is a new `Player` for every consumer on every edit.\n */\n const sync = useCallback((pc: PlaybackController) => {\n setDuration(pc.totalDuration);\n setErrors(errorsFrom(pc.buildErrors));\n const progress = precompProgressOf(pc.precomp);\n setPrecomp(held => (samePrecomp(held, progress) ? held : progress));\n }, []);\n\n /**\n * Move the playhead and repaint, with the loading flag the UI paces off.\n *\n * Created once so every caller — the transport, the reconcile effect and\n * the first paint after a session opens — shares one seek discipline.\n */\n const [seekTo] = useState(() => (frame: number) => {\n const pc = controllerRef.current;\n if (!pc) return;\n const last = Math.max(0, Math.round(pc.totalDuration * fpsRef.current) - 1);\n const clamped = Math.min(Math.max(0, Math.round(frame)), last);\n playhead.set(clamped);\n // Only the newest seek may report \"settled\". A superseded seek still\n // resolves — its generation guard bailed inside the controller — but it\n // never painted, so clearing the loading flag from it would tell the UI\n // a frame is ready while the newest is still in flight. Consumers pace\n // scrubbing off that edge.\n const token = ++seekTokenRef.current;\n setLoading(true);\n void pc.seek(clamped).then(() => {\n if (token === seekTokenRef.current) setLoading(false);\n });\n });\n\n useEffect(() => {\n if (!engine || !document) return;\n\n // `target` is omitted: the session opens unmounted, and a `MotionStage`\n // binds a canvas to it afterwards. That is what lets this hook own the\n // session without owning any DOM.\n const opened = engine.open({\n document,\n viewport,\n fps,\n // A track landing can surface a build error it threw.\n onPrecompProgress: () => sync(opened.controller),\n });\n\n const pc = opened.controller;\n pc.onRenderError = errs => setErrors(errorsFrom(errs));\n pc.onFramePainted = frame => onFramePaintedRef.current?.(frame);\n\n pc.onTime((t: number) => {\n const frame = Math.trunc(t * fpsRef.current);\n playhead.set(frame);\n\n // A document swap can change the timeline's length, so ask the\n // controller rather than trusting a duration captured when this\n // listener was installed.\n const last = Math.round(pc.totalDuration * fpsRef.current) - 1;\n if (frame < last) return;\n if (loopRef.current) pc.seekWhilePlaying(0);\n else setIsPlaying(false);\n });\n\n controllerRef.current = pc;\n sessionRef.current = opened;\n documentRef.current = document;\n setSession(opened);\n sync(pc);\n setLoading(true);\n\n return () => {\n controllerRef.current = null;\n sessionRef.current = null;\n setSession(null);\n // Disposes the controller and the render context together — the\n // session owns both.\n opened.dispose();\n };\n // `document` is deliberately absent — only whether there is one: a new\n // document is reconciled onto the live session by the effect below\n // rather than rebuilt into a new one. What remains genuinely does need a\n // rebuild — a different engine is a different CanvasKit, and a live\n // surface cannot change its size or rate (`Session.replace` refuses both).\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [engine, hasDocument, viewport.width, viewport.height, fps, sync]);\n\n // Reconcile a changed document onto the session already running. Skipped on\n // the pass that opens it, which was opened with this very document —\n // replacing it there would rediscover a timeline just discovered.\n useEffect(() => {\n if (documentRef.current === document) return;\n documentRef.current = document;\n const live = sessionRef.current;\n if (!live || !document) return;\n const pc = live.controller;\n live.replace(document);\n sync(pc);\n // No seek: `replace` already paints the current frame once its assets\n // load, clamped to the new length, and a running clock keeps ticking.\n const last = Math.max(0, Math.round(pc.totalDuration * fpsRef.current) - 1);\n playhead.set(Math.min(playhead.getSnapshot(), last));\n const token = ++seekTokenRef.current;\n setLoading(true);\n void pc.whenReplaced().then(() => {\n if (token === seekTokenRef.current) setLoading(false);\n });\n }, [document, sync, playhead]);\n\n useEffect(() => {\n const pc = controllerRef.current;\n if (!pc) return;\n if (isPlaying) pc.play(speed);\n else pc.pause();\n }, [session, isPlaying, speed]);\n\n useEffect(() => {\n controllerRef.current?.setMuted(muted);\n }, [session, muted]);\n\n // The one seek that isn't user-driven: park the new session on the frame\n // the playhead already holds, so a rebuild keeps its place.\n useEffect(() => {\n seekTo(playhead.getSnapshot());\n }, [session, playhead, seekTo]);\n\n // Built once and stable for the hook's life: every one reads the controller\n // through a ref, so a re-render never invalidates a handler a child holds —\n // which is what lets a transport subscribe its listeners a single time.\n const [actions] = useState<Omit<Player, keyof PlayerState>>(() => {\n // Scrubbing is a pause: a seek that left the clock running would be\n // overwritten by the next tick.\n const seek = (frame: number) => {\n setIsPlaying(false);\n seekTo(frame);\n };\n\n return {\n playhead,\n play: () => setIsPlaying(true),\n pause: () => setIsPlaying(false),\n toggle: () =>\n setIsPlaying(playing => {\n // Restarting from the end is what a play button means there.\n const pc = controllerRef.current;\n if (!playing && pc) {\n const last = Math.round(pc.totalDuration * fpsRef.current) - 1;\n if (playhead.getSnapshot() >= last) void pc.seek(0);\n }\n return !playing;\n }),\n seek,\n step: (delta: number) => seek(playhead.getSnapshot() + delta),\n seekWhilePlaying: (frame: number) => controllerRef.current?.seekWhilePlaying(frame),\n setSpeed: (next: number) => setSpeedState(next),\n setMuted: (next: boolean) => setMutedState(next),\n setLoop: (next: boolean) => setLoopState(next),\n\n screenshot: async () => controllerRef.current?.screenshot() ?? undefined,\n repaint: () => controllerRef.current?.repaint(),\n\n getTreeState: () => controllerRef.current?.getTreeState() ?? null,\n getNodeProps: (nodeId: string) => controllerRef.current?.getNodeState(nodeId) ?? null,\n getTrackSpans: () => controllerRef.current?.spans ?? [],\n whenReplaced: async () => {\n await controllerRef.current?.whenReplaced();\n },\n\n getNodeBox: (path: string) => controllerRef.current?.getNodeBox(path) ?? null,\n getNodePropsAt: (path: string) => controllerRef.current?.getNodePropsAt(path) ?? null,\n getNodeBoxes: () => controllerRef.current?.getNodeBoxes() ?? [],\n projectNode3D: (path, parentPoints, localPoints) =>\n controllerRef.current?.projectNode3D(path, parentPoints, localPoints) ?? null,\n parentPlanePoint: (path: string, point: Vector2) =>\n controllerRef.current?.parentPlanePoint(path, point) ?? null,\n getTextLayout: (path: string) => controllerRef.current?.getTextLayout(path) ?? null,\n pickNode: (point: Vector2, tolerance?: number) =>\n controllerRef.current?.pickNode(point, tolerance) ?? null,\n setNodeOverride: (path: string, props: NodeOverride) =>\n controllerRef.current?.setNodeOverride(path, props),\n clearNodeOverrides: (path?: string) => controllerRef.current?.clearNodeOverrides(path),\n };\n });\n\n return useMemo(\n () => ({\n ...actions,\n session,\n viewport,\n fps,\n isPlaying,\n speed,\n muted,\n loop,\n loading,\n duration,\n totalFrames,\n errors,\n precomp,\n }),\n [\n actions,\n session,\n viewport,\n fps,\n isPlaying,\n speed,\n muted,\n loop,\n loading,\n duration,\n totalFrames,\n errors,\n precomp,\n ],\n );\n}\n\n/** The fields the memo above supplies; everything else on `Player` is an action. */\ntype PlayerState = Pick<\n Player,\n | \"session\"\n | \"viewport\"\n | \"fps\"\n | \"isPlaying\"\n | \"speed\"\n | \"muted\"\n | \"loop\"\n | \"loading\"\n | \"duration\"\n | \"totalFrames\"\n | \"errors\"\n | \"precomp\"\n>;\n","import { useEffect, useRef, type CSSProperties } from \"react\";\n\nimport type { EngineRenderContext, Size2D } from \"@motionscript/core\";\n\nimport type { Player } from \"./use-motion-player\";\n\n/**\n * The session's surface, with `mount` narrowed to a canvas.\n *\n * `core` compiles without the DOM lib, so `RenderSurface.mount` takes `unknown`\n * there. This is the narrowing that seam always assumed: it happens here, in\n * the one layer that genuinely has `HTMLCanvasElement`.\n */\ntype StageRenderContext = Omit<EngineRenderContext, \"mount\"> & {\n mount(canvas: HTMLCanvasElement): void;\n};\n\n/**\n * What every stage that doesn't ask for a view gets: the whole frame, 1:1.\n *\n * A shared frozen object rather than a literal, so the view effect's dependency\n * on its fields is stable across renders for the overwhelming majority of\n * stages, which never pass `view` at all.\n */\nconst IDENTITY_VIEW = Object.freeze({ zoom: 1, x: 0, y: 0 });\n\nexport interface MotionStageProps {\n /** The player whose session this stage draws. */\n player: Player;\n /**\n * Device pixels to rasterize per viewport unit. Defaults to `1`.\n *\n * **Not a zoom and not a crop.** The viewport stays the scene's logical\n * coordinate space; this only changes how many real pixels that space is\n * drawn into, and CSS scales the canvas back up to fill its box.\n *\n * The lever exists because a preview is usually displayed far smaller than\n * the project's output size: a 1920×1080 project in a 700px-wide panel\n * rasterizes roughly seven times the pixels anyone can see. `0.5` there is a\n * quarter of the fill, the shader work and the effect offscreens.\n *\n * Two things to know before animating it:\n *\n * - **Every change re-creates the Skia surface**, which drops the storage\n * adapter's texture-backed images (decoded video frames, 3D composites;\n * still images survive) and asks for a fresh GL context. Quantize to a few\n * steps and let it settle — never drive it from a resize observer.\n * - **Prefer scales that keep both dimensions whole.** `pixelRatio` is one\n * uniform number, so a scale that rounds differently on each axis shifts\n * the frame's centre by up to half a device pixel.\n *\n * A snapshot from `player.screenshot()` comes back at the surface's size, so\n * it is scaled too — render at `1` if you need full resolution out of it.\n */\n renderScale?: number;\n /**\n * The canvas's own size in CSS pixels, when it is **not** the frame.\n *\n * Omitted — the default — the canvas is the frame: sized `viewport ×\n * renderScale` and stretched over its box by CSS, which is what every\n * export, thumbnail and plain player wants.\n *\n * Supplied, together with {@link view}, the canvas becomes a *window* onto\n * the frame: it takes the size of the area it is shown in, `renderScale`\n * stops meaning \"fraction of the project's resolution\" and starts meaning\n * device pixels per CSS pixel, and only what is on screen is rasterized.\n * That is what lets an editor zoom in without going soft or paying for the\n * whole frame at the zoomed resolution.\n *\n * It re-creates the surface exactly as `renderScale` does, and wants the\n * same treatment: quantize it and let it settle.\n */\n surfaceSize?: Size2D;\n /**\n * A pan and zoom applied to the render itself. Requires {@link surfaceSize}.\n *\n * `zoom` multiplies scene units; `x`/`y` offset the frame's centre in CSS\n * pixels, positive `y` downwards — the same sign convention as a CSS\n * translate, so a preview can hand over the pan it was already computing.\n *\n * Changing it costs a repaint and nothing else: no surface, no textures, no\n * precomp. That is the point of it living here rather than in a CSS\n * transform, which cannot add resolution it was not rasterized with.\n */\n view?: { zoom: number; x: number; y: number };\n /**\n * Whether the frame's edge bounds what draws, rather than only where its own\n * backdrop goes. Requires {@link surfaceSize} and {@link view}. Defaults to\n * `true`.\n *\n * Off lets a node parked past the frame's edge rasterize into the rest of\n * the surface instead of being discarded — for a host that wants to show,\n * faded, what a node placed there while animating in or out.\n */\n clipToFrame?: boolean;\n /**\n * What the frame's backdrop is painted with. Defaults to `\"black\"`.\n *\n * `\"transparent\"` leaves the frame unpainted where nothing was drawn, so\n * whatever the host renders behind the canvas shows through — a\n * checkerboard, for an editor that has to distinguish \"no scene covers this\"\n * from \"a scene covers it in black\". Requires {@link surfaceSize} and\n * {@link view}, the same way {@link clipToFrame} does.\n */\n frameBackground?: \"black\" | \"transparent\";\n className?: string;\n style?: CSSProperties;\n}\n\n/**\n * A canvas bound to a {@link Player}'s session.\n *\n * The stage owns how the frame is rasterized — resolution, view, clipping — and\n * nothing about what is in it or when. A session drives **one** surface, so one\n * stage per player: a second would take the surface from the first.\n */\nexport function MotionStage({\n player,\n renderScale = 1,\n surfaceSize,\n view,\n clipToFrame,\n frameBackground,\n className,\n style,\n}: MotionStageProps) {\n const canvasRef = useRef<HTMLCanvasElement>(null);\n const { session, viewport } = player;\n\n /**\n * The canvas's backing store, in device pixels. `viewport × renderScale` in\n * the default case, where the canvas *is* the frame; with `surfaceSize` it\n * is the viewing area instead.\n *\n * Floored at 1 so a degenerate scale can't ask CanvasKit for a zero-sized\n * surface, which fails the surface creation outright.\n */\n const bufferWidth = Math.max(1, Math.round((surfaceSize?.width ?? viewport.width) * renderScale));\n const bufferHeight = Math.max(1, Math.round((surfaceSize?.height ?? viewport.height) * renderScale));\n\n /**\n * The view transform to draw with, and the frame to clip and anchor against.\n * Only meaningful together, so they resolve in one place: no `surfaceSize`\n * means the canvas is the frame, and both revert to identity.\n */\n const activeView = surfaceSize && view ? view : IDENTITY_VIEW;\n const activeFrame = surfaceSize && view ? viewport : null;\n const activeClipToFrame = surfaceSize && view ? (clipToFrame ?? true) : true;\n const activeFrameBackground = surfaceSize && view ? (frameBackground ?? \"black\") : \"black\";\n\n const viewRef = useRef(activeView);\n const frameRectRef = useRef(activeFrame);\n const clipToFrameRef = useRef(activeClipToFrame);\n const frameBackgroundRef = useRef(activeFrameBackground);\n /**\n * The live render context.\n *\n * Held here rather than reached through `player.session` at each use because\n * it is mutable external state — a Skia context whose knobs are written, not\n * a value derived from a prop — and writing through the prop is what the\n * immutability lint is there to catch everywhere it *isn't* one of these.\n */\n const ctxRef = useRef<StageRenderContext | null>(null);\n useEffect(() => {\n viewRef.current = activeView;\n frameRectRef.current = activeFrame;\n clipToFrameRef.current = activeClipToFrame;\n frameBackgroundRef.current = activeFrameBackground;\n ctxRef.current = session ? (session.surface as StageRenderContext) : null;\n });\n\n /**\n * Bind the surface, and rebuild it when the backing store changes.\n *\n * The order matters and is why this is an effect rather than a callback:\n * React has already written the canvas's new `width`/`height` by the time\n * effects run, and CanvasKit reads exactly those when it builds the surface\n * (`MakeOnScreenGLSurface(ctx, canvas.width, canvas.height)`). Mounting\n * before the resize would produce a surface of the previous size.\n *\n * `repaint()` afterwards because a new surface starts blank and the clock is\n * not necessarily running to fill it — a paused editor would otherwise show\n * black until something else moved.\n */\n useEffect(() => {\n const canvas = canvasRef.current;\n const ctx = ctxRef.current;\n if (!session || !canvas || !ctx) return;\n ctx.pixelRatio = renderScale;\n ctx.view = viewRef.current;\n ctx.frame = frameRectRef.current;\n ctx.clipToFrame = clipToFrameRef.current;\n ctx.frameBackground = frameBackgroundRef.current;\n // `mount` detaches the old surface first, which also drops the storage\n // adapter's texture-backed images — they belong to the surface that made\n // them. Video frames and 3D composites are re-uploaded on demand;\n // decoded still images are raster-backed and survive.\n ctx.mount(canvas);\n session.controller.repaint();\n }, [session, renderScale, bufferWidth, bufferHeight]);\n\n /**\n * Apply {@link MotionStageProps.view} — a repaint and nothing more.\n *\n * Depends on the fields rather than the object so a caller passing a fresh\n * `{zoom, x, y}` each render — which is every caller, since it comes from a\n * gesture — doesn't repaint on renders where the view didn't move.\n */\n useEffect(() => {\n const ctx = ctxRef.current;\n if (!session || !ctx) return;\n ctx.view = { zoom: activeView.zoom, x: activeView.x, y: activeView.y };\n ctx.frame = activeFrame;\n ctx.clipToFrame = activeClipToFrame;\n ctx.frameBackground = activeFrameBackground;\n session.controller.repaint();\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n session,\n activeView.zoom,\n activeView.x,\n activeView.y,\n activeFrame?.width,\n activeFrame?.height,\n activeClipToFrame,\n activeFrameBackground,\n ]);\n\n return (\n <div className={className} style={{ position: \"relative\", width: \"100%\", height: \"100%\", ...style }}>\n <canvas\n ref={canvasRef}\n // The backing store, in device pixels. CSS below stretches it\n // back over the whole box either way, so this is purely how much\n // gets rasterized.\n width={bufferWidth}\n height={bufferHeight}\n style={{\n display: session ? \"block\" : \"none\",\n ...(surfaceSize\n ? {\n // `surfaceSize` is the canvas's CSS size, so it is\n // honoured literally rather than stretched to the\n // box: it is quantized by its caller, which means\n // it is usually a little larger than the box and\n // rarely the same shape. Stretched, that\n // difference would land as a non-uniform scale — a\n // picture squashed on one axis, a far worse\n // artefact than the softness this arrangement\n // exists to remove.\n //\n // Centred, so the surface's midpoint is the box's\n // midpoint: `executePass` puts the scene origin at\n // the centre of the surface, so this is what makes\n // the frame land where the caller's layout says it\n // is. The spare pixels hang off all four edges and\n // are clipped by whatever is holding this.\n position: \"absolute\",\n left: \"50%\",\n top: \"50%\",\n width: `${surfaceSize.width}px`,\n height: `${surfaceSize.height}px`,\n transform: \"translate(-50%, -50%)\",\n }\n : { width: \"100%\", height: \"100%\" }),\n }}\n />\n </div>\n );\n}\n"],"mappings":";;;;AAkBA,SAAgB,sBAAqC;CACjD,IAAI,QAAQ;CACZ,MAAM,4BAAY,IAAI,IAAgB;CACtC,OAAO;EACH,UAAU,UAAU;GAChB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACT,UAAU,OAAO,QAAQ;GAC7B;EACJ;EACA,mBAAmB;EACnB,IAAI,MAAM;GACN,IAAI,SAAS,OAAO;GACpB,QAAQ;GACR,KAAK,MAAM,UAAU,WAAW,OAAO;EAC3C;CACJ;AACJ;;;;;;;AAQA,SAAgB,YAAY,QAA6C;CACrE,MAAM,EAAE,aAAa;CACrB,OAAO,qBAAqB,SAAS,WAAW,SAAS,aAAa,SAAS,WAAW;AAC9F;;;;ACnBA,IAAM,oB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/B,GAAG;EAAC;EAAS;EAAa;EAAa;CAAY,CAAC;;;;;;;;CASpD,gBAAgB;EACZ,MAAM,MAAM,OAAO;EACnB,IAAI,CAAC,WAAW,CAAC,KAAK;EACtB,IAAI,OAAO;GAAE,MAAM,WAAW;GAAM,GAAG,WAAW;GAAG,GAAG,WAAW;EAAE;EACrE,IAAI,QAAQ;EACZ,IAAI,cAAc;EAClB,IAAI,kBAAkB;EACtB,QAAQ,WAAW,QAAQ;CAE/B,GAAG;EACC;EACA,WAAW;EACX,WAAW;EACX,WAAW;EACX,aAAa;EACb,aAAa;EACb;EACA;CACJ,CAAC;CAED,OACI,oBAAC,OAAD;EAAgB;EAAW,OAAO;GAAE,UAAU;GAAY,OAAO;GAAQ,QAAQ;GAAQ,GAAG;EAAM;YAC9F,oBAAC,UAAD;GACI,KAAK;GAIL,OAAO;GACP,QAAQ;GACR,OAAO;IACH,SAAS,UAAU,UAAU;IAC7B,GAAI,cACE;KAiBI,UAAU;KACV,MAAM;KACN,KAAK;KACL,OAAO,GAAG,YAAY,MAAM;KAC5B,QAAQ,GAAG,YAAY,OAAO;KAC9B,WAAW;IACf,IACA;KAAE,OAAO;KAAQ,QAAQ;IAAO;GAC1C;EACH,CAAA;CACA,CAAA;AAEb"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The playhead, held outside React.
|
|
3
|
+
*
|
|
4
|
+
* A frame number written at clock rate through `useState` re-renders whoever
|
|
5
|
+
* owns the player, and on a high-refresh display that is faster than the
|
|
6
|
+
* project's own frame rate. Nothing in a render depends on it: the controller
|
|
7
|
+
* paints straight onto the canvas and React is not involved. So the number
|
|
8
|
+
* lives here, and only the components that actually display it subscribe.
|
|
9
|
+
*/
|
|
10
|
+
export interface PlayheadStore {
|
|
11
|
+
subscribe(onChange: () => void): () => void;
|
|
12
|
+
getSnapshot(): number;
|
|
13
|
+
/** @internal — written by the player that owns this store. */
|
|
14
|
+
set(frame: number): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createPlayheadStore(): PlayheadStore;
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe *this* component to the playhead.
|
|
19
|
+
*
|
|
20
|
+
* Typed structurally rather than against `Player` so the store and the hook
|
|
21
|
+
* that reads it don't import each other.
|
|
22
|
+
*/
|
|
23
|
+
export declare function usePlayhead(player: {
|
|
24
|
+
playhead: PlayheadStore;
|
|
25
|
+
}): number;
|
|
26
|
+
//# sourceMappingURL=playhead.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"playhead.d.ts","sourceRoot":"","sources":["../../src/ui/playhead.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAa;IAC1B,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;IAC5C,WAAW,IAAI,MAAM,CAAC;IACtB,8DAA8D;IAC9D,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED,wBAAgB,mBAAmB,IAAI,aAAa,CAiBnD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE;IAAE,QAAQ,EAAE,aAAa,CAAA;CAAE,GAAG,MAAM,CAGvE"}
|