@motionhub/runtime 0.7.0 → 0.8.1
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/README.md +14 -4
- package/index.d.ts +27 -8
- package/index.js +105 -23
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ You do not use this package on its own: an engine binding provides it.
|
|
|
15
15
|
| `useTokens()` | The motion's `tokens.json` resolved: `color.ink` (a CSS colour), `font.display` (a CSS stack), `duration.light` (seconds), `easing.light` (`[x1, y1, x2, y2]`), `caption.*` |
|
|
16
16
|
| `seededRandom(seed)` | A stream of numbers in [0, 1) for a string or number seed: the same seed draws the same picture in every engine |
|
|
17
17
|
| `<Video src startAt trimBefore playbackRate objectFit>` | The engine's video: shown from `startAt` s of the sequence, its source from `trimBefore` s |
|
|
18
|
-
| `<Caption line opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top); with `line`, that line of the film's while it is said |
|
|
18
|
+
| `<Caption line opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top; lengths in pixels, as `36` or `'36px'`; with neither color nor background, white on a dark ground; without max-width, at most 1500 px and 64 px from the frame's sides); with `line`, that line of the film's while it is said |
|
|
19
19
|
| `<Sequence from duration>`, `interpolate(t, input, output, {easing})`, `Easing` | As Remotion's, in seconds: a span whose children count time from its start, a value mapped through a curve, and the curves (`Easing.bezier` takes a cubicBezier token). For composing components, as a reel does |
|
|
20
20
|
| `<MotionRoot tokens>` | A full-frame box carrying the tokens as CSS custom properties |
|
|
21
21
|
| `<MotionScope tokens root>` | Its children drawn in another motion, on the engine's clock: `useTokens()` under it gives that motion's tokens (a `tokens.json`, or tokens already resolved), set as custom properties on a full-frame root (`root={false}` leaves it out). A film in several motions wraps each scene, or each part of a frame, in one |
|
|
@@ -51,6 +51,15 @@ check it scene by scene without running it:
|
|
|
51
51
|
(without one, an equal share of the time to the next line that gives its `at`, or to the scene's end), so lines that give no time share
|
|
52
52
|
their scene evenly. `<Film captions>` draws the line being said as a `<Caption>`, over everything, fading in and out; `<Caption line="id">`
|
|
53
53
|
draws one line's caption where a component wants it, while that line is said.
|
|
54
|
+
- **A narration**: `<Film voice="provider:name">` names the voice a film's lines are spoken in, and the engine that speaks them gives each
|
|
55
|
+
voiced line's speech in its runtime (`voice: {[line id]: {seconds, words: [{text, start, end}]}}`, word times into the speech). A voiced
|
|
56
|
+
line lasts as long as its speech, after a lead (0.4 s, before a scene's first line) or a hold (0.4 s, between lines) when it gives no
|
|
57
|
+
`at` (`NARRATION`); its `dur` gives way to the speech. Speech that runs past its scene's end, or into the next line that gives its `at`,
|
|
58
|
+
is cut there, and the line says by how much (`over`), for the engine to tell; lines it leaves no time are said where it ends, in none.
|
|
59
|
+
Speech never makes the outline fail, since its length is the engine's; times the author wrote that cannot all hold still do. A voiced line in the outline has `voiced` and its `words`, in the film's seconds. `useLine(id)` gives a
|
|
60
|
+
line's `{start, end, words}` in its scene's time, so a picture can move with what is said; `<Film captions="words">` draws a voiced line
|
|
61
|
+
word by word, each full once it is said. Its words are set apart by a blank, but where either side is Chinese, Japanese, Korean or Thai,
|
|
62
|
+
which an engine gives one character (or syllable) a word: `热向前走` stays together.
|
|
54
63
|
- **`<Sound src at dur trim loop volume fadeIn fadeOut duck>`**: a sound the film plays, the file `src` (a path the engine serves), from
|
|
55
64
|
`at` seconds of its scene, or of the film when it is one of `<Film>`'s own; `dur` seconds of it, from `trim` seconds into the file (the
|
|
56
65
|
whole file when left out); with `loop`, again and again to the end of its scene or of the film; at `volume` dB (0 is the file as it is),
|
|
@@ -61,8 +70,9 @@ check it scene by scene without running it:
|
|
|
61
70
|
engine that places each scene itself. A Remotion project exported from the Open Motion Studio puts each scene in a `Series.Sequence`
|
|
62
71
|
this way, so the scenes are named clips on Remotion Studio's timeline. A scene the film does not have is an error.
|
|
63
72
|
- `filmOutline(children, {width, height})` reads that structure from the elements themselves, without drawing them: `{seconds, scenes:
|
|
64
|
-
[{id, start, duration, layers, lines, sounds}], layers, sounds, captions}`, each line `{id, text, start, end}` and
|
|
65
|
-
|
|
73
|
+
[{id, start, duration, layers, lines, sounds}], layers, sounds, captions, voice}`, each line `{id, text, start, end}` (and, voiced,
|
|
74
|
+
`voiced`, `words` and `over`) and each sound `{src, at, dur, until, trim, loop, volume, fadeIn, fadeOut,
|
|
75
|
+
duck?}` in the film's seconds (`dur` null for the whole file). A line's and a word's times are in whole milliseconds. `<Film>` reports it to the engine's `onFilm` while it is drawn, once for each structure (a new `onFilm` function on every
|
|
66
76
|
render is not called again for the same structure), when its value has one. A scene without an
|
|
67
77
|
id or a positive duration, an id used twice (scenes, lines, layers in one scene, the film's own layers), a sound without a `src`, a line
|
|
68
78
|
that starts past its scene's end or before the line before it ends, lines that would be left no time, and a time that is no number of
|
|
@@ -72,4 +82,4 @@ check it scene by scene without running it:
|
|
|
72
82
|
|
|
73
83
|
`@motionhub/runtime/sound` synthesises a motion's sound, with no React and no audio API: `renderSound({seconds, cues, sound, rate})` returns mono samples from the motion's `react/sound.json` (a bed at a tempo, and a voice for each role: `land`, `move`, `draw`, `count`, `cut`, `out`) and a list of cues (`[{at, role, dur}]`, a reel's `export const cues`). The same input gives the same samples, so a player plays them and a renderer can write them to a WAV for Remotion's `<Audio>` or a HyperFrames track.
|
|
74
84
|
|
|
75
|
-
An engine binding passes `<MotionRuntimeProvider value>` an object with `useTime` and `useCompositionTime` (hooks returning seconds), `fps`, `width`, `height`, `tokens` (resolved), if it plays video `Video`,
|
|
85
|
+
An engine binding passes `<MotionRuntimeProvider value>` an object with `useTime` and `useCompositionTime` (hooks returning seconds), `fps`, `width`, `height`, `tokens` (resolved), if it plays video `Video`, if it wants a `<Film>`'s structure, `onFilm(outline)`, and, if it speaks the film's lines, `voice`.
|
package/index.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ export type MotionRuntime = {
|
|
|
28
28
|
Video?: FC<VideoProps>;
|
|
29
29
|
/** called with a <Film>'s structure when it is drawn */
|
|
30
30
|
onFilm?(outline: FilmOutline): void;
|
|
31
|
+
/** Each voiced line's speech, by line id: how long it is and its words, each from and to seconds into it */
|
|
32
|
+
voice?: Record<string, {seconds: number; words?: {text: string; start: number; end: number}[]}>;
|
|
31
33
|
};
|
|
32
34
|
export declare const MotionRuntimeProvider: FC<{value: MotionRuntime; children?: ReactNode}>;
|
|
33
35
|
|
|
@@ -41,7 +43,7 @@ export declare const MotionRoot: FC<{tokens?: Tokens; style?: CSSProperties; chi
|
|
|
41
43
|
/** Its children drawn in another motion: its tokens (a tokens.json, or resolved), keeping the engine's time; one per scene or part of a frame */
|
|
42
44
|
export declare const MotionScope: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
|
|
43
45
|
/** One spoken line, styled by the caption tokens; with `line`, that line of the film's, while it is said */
|
|
44
|
-
export declare const Caption: FC<{line?: string; children?: ReactNode; opacity?: number; style?: CSSProperties}>;
|
|
46
|
+
export declare const Caption: FC<{line?: string; words?: boolean; children?: ReactNode; opacity?: number; style?: CSSProperties}>;
|
|
45
47
|
|
|
46
48
|
export type EasingFn = (x: number) => number;
|
|
47
49
|
export declare const Easing: {linear: EasingFn; bezier(x1: number, y1: number, x2: number, y2: number): EasingFn; inOutSine: EasingFn; outCubic: EasingFn; outQuart: EasingFn; inOutCubic: EasingFn; outBack: EasingFn};
|
|
@@ -61,14 +63,31 @@ export type FilmOutline = {
|
|
|
61
63
|
scenes: {id: string; start: number; duration: number; layers: LayerBox[]; lines: FilmLine[]; sounds: FilmSound[]}[];
|
|
62
64
|
layers: LayerBox[];
|
|
63
65
|
sounds: FilmSound[];
|
|
64
|
-
/** Whether the film draws its lines as captions */
|
|
65
|
-
captions: boolean;
|
|
66
|
+
/** Whether the film draws its lines as captions, whole or word by word */
|
|
67
|
+
captions: boolean | 'words';
|
|
68
|
+
/** The film's voice, `provider:name`, or null */
|
|
69
|
+
voice: string | null;
|
|
66
70
|
};
|
|
67
|
-
/** A
|
|
68
|
-
export type
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
/** A word of a voiced line, and when it is said */
|
|
72
|
+
export type FilmWord = {text: string; start: number; end: number};
|
|
73
|
+
/**
|
|
74
|
+
* A line of a scene, and when it is said, in the film's seconds; a voiced one with its words, and `over`, the seconds of its speech
|
|
75
|
+
* past its scene's end
|
|
76
|
+
*/
|
|
77
|
+
export type FilmLine = {id: string; text: string; start: number; end: number; voiced?: true; words?: FilmWord[]; over?: number};
|
|
78
|
+
/** How long a scene waits before its first spoken line, and between spoken lines, in seconds */
|
|
79
|
+
export declare const NARRATION: {lead: number; hold: number};
|
|
80
|
+
/** When a line is said, in its scene's time (as useTime() counts there), with its words where it is voiced */
|
|
81
|
+
export declare function useLine(id: string): {start: number; end: number; words: FilmWord[]};
|
|
82
|
+
export declare function filmOutline(
|
|
83
|
+
children: ReactNode,
|
|
84
|
+
frame?: {width?: number; height?: number; captions?: boolean | 'words'; voice?: MotionRuntime['voice']; speaker?: string},
|
|
85
|
+
): FilmOutline;
|
|
86
|
+
/**
|
|
87
|
+
* Its <Scene>s laid end to end, each drawn in its span on its own clock; anything else drawn across them all; with `captions`, its lines as
|
|
88
|
+
* captions (`"words"`: word by word, where they are voiced); `voice`, the voice its lines are spoken in (`provider:name`)
|
|
89
|
+
*/
|
|
90
|
+
export declare const Film: FC<{captions?: boolean | 'words'; voice?: string; children?: ReactNode}>;
|
|
72
91
|
export declare const Scene: FC<{id: string; duration: number; children?: ReactNode}>;
|
|
73
92
|
/** A box of the frame (`x`, `y`, `w`, `h` in pixels, the whole frame when left out), stacked by `z`, shown for its whole scene */
|
|
74
93
|
export declare const Layer: FC<{id: string; x?: number; y?: number; w?: number; h?: number; z?: number; style?: CSSProperties; children?: ReactNode}>;
|
package/index.js
CHANGED
|
@@ -157,18 +157,38 @@ const sound = (e, start, until) => {
|
|
|
157
157
|
};
|
|
158
158
|
};
|
|
159
159
|
|
|
160
|
+
/** How long a scene waits before its first spoken line, and between spoken lines, in seconds */
|
|
161
|
+
export const NARRATION = {lead: 0.4, hold: 0.4};
|
|
162
|
+
|
|
163
|
+
/** A line's clip as the engine gives it (`voice[id]`): its length in seconds and its words, each from and to seconds into it */
|
|
164
|
+
const clipOf = (where, clip) => {
|
|
165
|
+
if (clip === undefined) return undefined;
|
|
166
|
+
if (!clip || !(Number.isFinite(clip.seconds) && clip.seconds > 0)) throw new Error(`${where}: its voice is no length of speech`);
|
|
167
|
+
// Within the speech, and each ending after it starts: an engine's word times can be a little off.
|
|
168
|
+
const within = (t) => Math.min(clip.seconds, Math.max(0, t));
|
|
169
|
+
const words = Array.isArray(clip.words)
|
|
170
|
+
? clip.words
|
|
171
|
+
.filter((w) => w && typeof w.text === 'string' && Number.isFinite(w.start) && Number.isFinite(w.end) && w.end >= w.start)
|
|
172
|
+
.map((w) => ({text: w.text, start: within(w.start), end: within(w.end)}))
|
|
173
|
+
: [];
|
|
174
|
+
return {seconds: clip.seconds, words};
|
|
175
|
+
};
|
|
176
|
+
|
|
160
177
|
/**
|
|
161
178
|
* When each of a scene's lines is said, `start` and `end` in the film's seconds. A line's `at` is seconds into its scene,
|
|
162
179
|
* and without one it starts where the line before it ends (the first, at the scene's start); a line without `dur` takes an
|
|
163
180
|
* equal share of the time to the next line that gives its `at`, or to the scene's end. So lines that give no time share
|
|
164
|
-
* their scene evenly.
|
|
181
|
+
* their scene evenly. A line the engine has a voice for (`voice[id]`) lasts as long as its speech, after a lead (the first)
|
|
182
|
+
* or a hold (the rest) when it gives no `at`; speech that runs past the scene's end is cut there, and `over` says by how much.
|
|
165
183
|
*/
|
|
166
|
-
const timeLines = (lines, start, duration) => {
|
|
167
|
-
const given = lines.map((e) => {
|
|
184
|
+
const timeLines = (lines, start, duration, voice = {}) => {
|
|
185
|
+
const given = lines.map((e, i) => {
|
|
168
186
|
const where = `<Line id="${e.props.id}">`;
|
|
169
187
|
const at = seconds(where, 'at', e.props.at, undefined);
|
|
170
188
|
if (at !== undefined && at >= duration) throw new Error(`${where}: at is ${at} s, past the end of its scene (${duration} s)`);
|
|
171
|
-
|
|
189
|
+
const clip = clipOf(where, voice[e.props.id]);
|
|
190
|
+
if (clip) return {where, at, dur: clip.seconds, gap: at === undefined ? (i === 0 ? NARRATION.lead : NARRATION.hold) : 0, clip};
|
|
191
|
+
return {where, at, dur: seconds(where, 'dur', e.props.dur, undefined, {above: true}), gap: 0};
|
|
172
192
|
});
|
|
173
193
|
// Lines are said in the order they are written.
|
|
174
194
|
let last = -Infinity;
|
|
@@ -188,18 +208,38 @@ const timeLines = (lines, start, duration) => {
|
|
|
188
208
|
if (from < cursor - 1e-9) throw new Error(`${given[i].where}: at is ${from} s, before the line before it ends (${round(cursor)} s into the scene)`);
|
|
189
209
|
const to = next < given.length ? given[next].at : duration;
|
|
190
210
|
const run = given.slice(i, next);
|
|
191
|
-
const fixed = run.reduce((sum, g) => sum + (g.dur ?? 0), 0);
|
|
211
|
+
const fixed = run.reduce((sum, g) => sum + (g.dur ?? 0) + g.gap, 0);
|
|
192
212
|
const free = run.filter((g) => g.dur === undefined);
|
|
193
213
|
const left = to - from - fixed;
|
|
194
|
-
// Lines that give no time would be said in none, and never shown
|
|
195
|
-
|
|
214
|
+
// Lines that give no time would be said in none, and never shown: the author's to fix, unless it is speech that takes the time,
|
|
215
|
+
// whose length the author does not write. Then they are said where it ends, in what is left, and none if nothing is.
|
|
216
|
+
if (free.length && left <= 1e-9 && !run.some((g) => g.clip)) {
|
|
196
217
|
throw new Error(`${free[0].where}: no time is left for it before ${next < given.length ? `${given[next].where} at ${to} s` : `the end of its scene (${to} s)`}; give it an at, or shorten the lines before it`);
|
|
197
218
|
}
|
|
198
|
-
const share = free.length ? left / free.length : 0;
|
|
219
|
+
const share = free.length ? Math.max(0, left) / free.length : 0;
|
|
199
220
|
cursor = from;
|
|
200
221
|
for (const g of run) {
|
|
201
|
-
|
|
202
|
-
|
|
222
|
+
// Speech is cut where the next line that gives its at begins, or the scene ends, and says by how much (over); a dur the
|
|
223
|
+
// author wrote runs on, and one that runs into the next line is an error there.
|
|
224
|
+
const limit = g.clip ? to : duration;
|
|
225
|
+
const begin = Math.min(limit, cursor + g.gap);
|
|
226
|
+
const wanted = begin + (g.dur ?? share);
|
|
227
|
+
const end = Math.min(limit, wanted);
|
|
228
|
+
const at = start + begin;
|
|
229
|
+
// In whole milliseconds, so the times read as written and add up as they should.
|
|
230
|
+
times.push({
|
|
231
|
+
start: round(at),
|
|
232
|
+
end: round(start + end),
|
|
233
|
+
...(g.clip
|
|
234
|
+
? {
|
|
235
|
+
voiced: true,
|
|
236
|
+
words: g.clip.words
|
|
237
|
+
.filter((w) => begin + w.start < end)
|
|
238
|
+
.map((w) => ({text: w.text, start: round(at + w.start), end: round(at + Math.min(w.end, end - begin))})),
|
|
239
|
+
...(wanted - limit > 1e-9 ? {over: round(wanted - limit)} : {}),
|
|
240
|
+
}
|
|
241
|
+
: {}),
|
|
242
|
+
});
|
|
203
243
|
cursor = end;
|
|
204
244
|
}
|
|
205
245
|
i = next;
|
|
@@ -214,7 +254,11 @@ const timeLines = (lines, start, duration) => {
|
|
|
214
254
|
* film's seconds. `layers` and `sounds` are the film's own, across every scene. A scene without an id or a positive duration, an id used
|
|
215
255
|
* twice where it must be unique, and a sound with no file or a time that is no number of seconds, are errors.
|
|
216
256
|
*/
|
|
217
|
-
export function filmOutline(children, {width = 1920, height = 1080, captions = false} = {}) {
|
|
257
|
+
export function filmOutline(children, {width = 1920, height = 1080, captions = false, voice = {}, speaker} = {}) {
|
|
258
|
+
// A provider's own name for a voice may hold blanks and brackets, as MiniMax's "Chinese (Mandarin)_News_Anchor" does.
|
|
259
|
+
if (speaker !== undefined && !(typeof speaker === 'string' && /^[\w.-]+:\S(?:.*\S)?$/.test(speaker))) {
|
|
260
|
+
throw new Error(`<Film voice="${speaker}">: a voice is its provider and its name, as voice="minimax:Chinese (Mandarin)_News_Anchor"`);
|
|
261
|
+
}
|
|
218
262
|
const box = (e) => ({id: named('Layer', e.props.id), x: e.props.x ?? 0, y: e.props.y ?? 0, w: e.props.w ?? width, h: e.props.h ?? height, z: e.props.z ?? 0});
|
|
219
263
|
const unique = (what, items) => {
|
|
220
264
|
const seen = new Set();
|
|
@@ -237,7 +281,7 @@ export function filmOutline(children, {width = 1920, height = 1080, captions = f
|
|
|
237
281
|
if (!(Number.isFinite(duration) && duration > 0)) throw new Error(`<Scene id="${id}"> needs its duration in seconds: duration={4}`);
|
|
238
282
|
const inner = elements(e.props.children).filter(isValidElement);
|
|
239
283
|
const said = inner.filter((c) => c.type === Line);
|
|
240
|
-
const times = timeLines(said, start, duration);
|
|
284
|
+
const times = timeLines(said, start, duration, voice ?? {});
|
|
241
285
|
const lines = said.map((c, i) => ({id: named('Line', c.props.id), text: c.props.text ?? words(c.props.children), ...times[i]}));
|
|
242
286
|
const sounds = inner.filter((c) => c.type === Sound).map((c) => sound(c, start, start + duration));
|
|
243
287
|
scenes.push({id, start, duration, layers: unique(`layers in scene "${id}"`, inner.filter((c) => c.type === Layer).map(box)), lines, sounds});
|
|
@@ -257,7 +301,14 @@ export function filmOutline(children, {width = 1920, height = 1080, captions = f
|
|
|
257
301
|
const taken = scene.layers.find((layer) => layers.some((own) => own.id === layer.id));
|
|
258
302
|
if (taken) throw new Error(`scene "${scene.id}" has a layer called "${taken.id}", the name of one of the film's own layers`);
|
|
259
303
|
}
|
|
260
|
-
return {
|
|
304
|
+
return {
|
|
305
|
+
seconds: start,
|
|
306
|
+
scenes,
|
|
307
|
+
layers,
|
|
308
|
+
sounds: own.map((e) => sound(e, 0, start)),
|
|
309
|
+
captions: captions === true || captions === 'words' ? captions : false,
|
|
310
|
+
voice: speaker ?? null,
|
|
311
|
+
};
|
|
261
312
|
}
|
|
262
313
|
|
|
263
314
|
/**
|
|
@@ -265,12 +316,12 @@ export function filmOutline(children, {width = 1920, height = 1080, captions = f
|
|
|
265
316
|
* at the film's end), and anything else among its children (a <Layer>, a background) drawn across them all. It reports its structure
|
|
266
317
|
* (filmOutline) to the engine's `onFilm`, if the engine has one, while it is drawn: the engine learns the length from the scenes.
|
|
267
318
|
*/
|
|
268
|
-
export const Film = ({captions = false, children}) => {
|
|
319
|
+
export const Film = ({captions = false, voice, children}) => {
|
|
269
320
|
const r = runtime();
|
|
270
321
|
// Read on every frame (it is cheap: a film's children are new elements each frame), and reported while drawing, once for each
|
|
271
322
|
// structure, whether or not the engine's onFilm is the same function each time: an engine that renders the film once, off the
|
|
272
323
|
// page, learns it before the first frame is shown.
|
|
273
|
-
const outline = filmOutline(children, {...r, captions});
|
|
324
|
+
const outline = filmOutline(children, {...r, captions, voice: r.voice, speaker: voice});
|
|
274
325
|
const reported = JSON.stringify(outline);
|
|
275
326
|
const onFilm = r.onFilm;
|
|
276
327
|
useMemo(() => onFilm?.(JSON.parse(reported)), [reported]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
@@ -284,7 +335,7 @@ export const Film = ({captions = false, children}) => {
|
|
|
284
335
|
const engine = only === null ? r : {...r, useTime: r.useCompositionTime};
|
|
285
336
|
// The film's time, for its lines: the engine's, or, drawn as one scene, that scene's start on from the engine's.
|
|
286
337
|
const now = only === null ? t : (outline.scenes.find((scene) => scene.id === only)?.start ?? 0) + t;
|
|
287
|
-
const lines = outline.scenes.flatMap((scene) => scene.lines);
|
|
338
|
+
const lines = outline.scenes.flatMap((scene) => scene.lines.map((line) => ({...line, scene: scene.start})));
|
|
288
339
|
const spoken = lines.find((line) => now >= line.start && now < line.end);
|
|
289
340
|
let index = 0;
|
|
290
341
|
return createElement(Runtime.Provider, {value: engine}, createElement(InFilm.Provider, {value: true}, createElement(Said.Provider, {value: {lines, now}}, elements(children).map((e) => {
|
|
@@ -297,11 +348,23 @@ export const Film = ({captions = false, children}) => {
|
|
|
297
348
|
const value = {...r, useTime: () => r.useTime() - scene.start};
|
|
298
349
|
return createElement(Runtime.Provider, {key, value}, e);
|
|
299
350
|
}), captions && spoken ? createElement('div', {key: 'captions', style: {position: 'absolute', inset: 0, zIndex: 2147483647, pointerEvents: 'none'}},
|
|
300
|
-
createElement(Caption, {line: spoken.id})) : null)));
|
|
351
|
+
createElement(Caption, {line: spoken.id, words: outline.captions === 'words'})) : null)));
|
|
301
352
|
};
|
|
302
353
|
|
|
303
354
|
/** The film's lines and its time, for a <Caption line> anywhere in it */
|
|
304
355
|
const Said = createContext(null);
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* When a line of the film is said, in the time of its scene (as useTime() counts there): `{start, end, words}`, each word `{text, start,
|
|
359
|
+
* end}` where the engine has its voice (none otherwise). So a picture can move with what is said: `const {start} = useLine('intro-1')`.
|
|
360
|
+
*/
|
|
361
|
+
export const useLine = (id) => {
|
|
362
|
+
const said = useContext(Said);
|
|
363
|
+
const line = said?.lines.find((l) => l.id === id);
|
|
364
|
+
if (!line) throw new Error(`useLine("${id}"): ${said ? 'the film has no line by that id' : 'a line is read inside a <Film>'}`);
|
|
365
|
+
const shift = (t) => round(t - line.scene);
|
|
366
|
+
return {start: shift(line.start), end: shift(line.end), words: (line.words ?? []).map((w) => ({text: w.text, start: shift(w.start), end: shift(w.end)}))};
|
|
367
|
+
};
|
|
305
368
|
/** How long a line's caption takes to come and go, in seconds */
|
|
306
369
|
const CAPTION_FADE = 0.15;
|
|
307
370
|
|
|
@@ -367,27 +430,46 @@ export const MotionScope = ({tokens, root = true, children}) => {
|
|
|
367
430
|
/**
|
|
368
431
|
* The motion's caption: one spoken line on the frame, drawn from the caption group of its tokens: font, size, weight, color, background, radius,
|
|
369
432
|
* padding-y, padding-x, bottom, max-width, shadow (a text shadow), and a rule: rule-left (a bar down its left side) or rule-top (a short bar above it).
|
|
433
|
+
* Lengths are pixels, as numbers (36) or as '36px'. With neither a color nor a background it is drawn white on a dark ground.
|
|
434
|
+
* Without a max-width, its text is at most 1500 px wide and keeps 64 px from the frame's sides.
|
|
370
435
|
* `opacity` fades it; it stays a function of what you pass. With `line`, the id of one of its <Film>'s lines, it draws that line's words while
|
|
371
436
|
* it is said, fading in and out, and nothing at other times.
|
|
372
437
|
*/
|
|
373
|
-
|
|
438
|
+
// Scripts written without blanks between words: an engine gives their words one character (or syllable) each, and they
|
|
439
|
+
// are shown together again, with a blank only where neither side is one of them, as HyperFrames joins its words.
|
|
440
|
+
const UNSPACED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Thai}]/u;
|
|
441
|
+
const blankBetween = (left, right) => (UNSPACED.test(left.at(-1) ?? '') || UNSPACED.test(right[0] ?? '') ? '' : ' ');
|
|
442
|
+
|
|
443
|
+
export const Caption = ({line, words = false, children, opacity = 1, style}) => {
|
|
374
444
|
// Both read before anything branches, so a caption calls the same hooks with a line or without.
|
|
375
445
|
const said = useContext(Said);
|
|
376
446
|
const c = useTokens().caption ?? {};
|
|
447
|
+
const frame = useVideoConfig().width;
|
|
377
448
|
if (line !== undefined) {
|
|
378
449
|
const spoken = said?.lines.find((l) => l.id === line);
|
|
379
450
|
if (!spoken) throw new Error(`<Caption line="${line}">: ${said ? 'the film has no line by that id' : 'a caption of a line is drawn inside a <Film>'}`);
|
|
380
451
|
const fade = Math.min(1, (said.now - spoken.start) / CAPTION_FADE, (spoken.end - said.now) / CAPTION_FADE);
|
|
381
452
|
if (fade <= 0) return null;
|
|
382
|
-
|
|
453
|
+
// Word by word, where the line is voiced: each word full once it is said, the rest waiting faint.
|
|
454
|
+
const shown = words && spoken.words?.length
|
|
455
|
+
? spoken.words.map((w, i) => createElement('span', {key: i, style: {opacity: said.now >= w.start ? 1 : 0.35}}, `${i ? blankBetween(spoken.words[i - 1].text, w.text) : ''}${w.text}`))
|
|
456
|
+
: spoken.text;
|
|
457
|
+
return createElement(Caption, {opacity: opacity * fade, style}, children ?? shown);
|
|
383
458
|
}
|
|
384
|
-
|
|
459
|
+
// A length in pixels as resolved tokens give it (36), or as a film's own tokens may write it ('36px'); anything else is not one.
|
|
460
|
+
const px = (v, fallback) => (typeof v === 'number' ? v : typeof v === 'string' && /^\s*-?[\d.]+(px)?\s*$/.test(v) ? parseFloat(v) : fallback);
|
|
461
|
+
const py = px(c['padding-y'], 10), padX = px(c['padding-x'], 26), pad = `${py}px ${padX}px`;
|
|
462
|
+
// At most 1500 px of text, and never closer than 64 px to the frame's sides: a portrait frame is narrower than that.
|
|
463
|
+
const widest = Number.isFinite(frame) && frame > 0 ? Math.min(1500, frame - 128 - 2 * padX) : 1500;
|
|
464
|
+
// A caption given neither a colour nor a ground is drawn light on a dark ground, legible over any picture.
|
|
465
|
+
const plain = c.color === undefined && c.background === undefined;
|
|
385
466
|
const rule = c['rule-left']
|
|
386
467
|
? createElement('div', {style: {position: 'absolute', left: 0, top: py * 0.6, bottom: py * 0.6, width: 4, borderRadius: 2, background: c['rule-left']}})
|
|
387
468
|
: c['rule-top'] ? createElement('div', {style: {width: 56, height: 3, background: c['rule-top'], margin: '0 auto 8px'}}) : null;
|
|
388
|
-
return createElement('div', {style: {position: 'absolute', left: 0, right: 0, bottom: c.bottom
|
|
469
|
+
return createElement('div', {style: {position: 'absolute', left: 0, right: 0, bottom: px(c.bottom, 54), display: 'flex', justifyContent: 'center', opacity, pointerEvents: 'none'}},
|
|
389
470
|
createElement('div', {style: {
|
|
390
|
-
position: 'relative', maxWidth: c['max-width']
|
|
391
|
-
color: c.color, background: c.background ?? '
|
|
471
|
+
position: 'relative', maxWidth: px(c['max-width'], widest), textAlign: 'center', fontFamily: c.font, fontSize: px(c.size, 34), fontWeight: c.weight ?? 400, lineHeight: 1.35,
|
|
472
|
+
color: c.color ?? (plain ? '#ffffff' : undefined), background: c.background ?? (plain ? 'rgba(12, 14, 18, 0.72)' : 'transparent'), padding: pad,
|
|
473
|
+
borderRadius: px(c.radius, 10), textShadow: c.shadow, ...style,
|
|
392
474
|
}}, rule, children));
|
|
393
475
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionhub/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), and a film of scenes and sounds, in any engine that provides it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/
|
|
8
|
+
"url": "git+https://github.com/motionhubdev/motionhub.git",
|
|
9
9
|
"directory": "packages/runtime"
|
|
10
10
|
},
|
|
11
11
|
"homepage": "https://motionhub.dev",
|