@motionhub/runtime 0.6.0 → 0.7.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/README.md +17 -6
- package/index.d.ts +24 -8
- package/index.js +133 -13
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -15,11 +15,11 @@ 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 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) |
|
|
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 |
|
|
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 |
|
|
22
|
-
| `<Film>`, `<Scene id duration>`, `<Layer id x y w h z>`, `<Line id text
|
|
22
|
+
| `<Film captions>`, `<Scene id duration>`, `<Layer id x y w h z>`, `<Line id text at dur>`, `<Sound src at …>` | A film made of scenes, for an engine that plays a whole film rather than one component: below |
|
|
23
23
|
| `resolveTokens(dtcg)`, `tokenVariables(tokens)` | `tokens.json` (W3C design tokens) as values, and those values as custom properties: `--color-ink`, `--font-display`, `--duration-light`, `--easing-light`, `--caption-size` |
|
|
24
24
|
|
|
25
25
|
A component colours with `var(--color-<name>)` and sets type with `var(--font-<name>)`, reads its time from `useTime()`, never from a clock, a timer or `Math.random`, and takes what changes (words, a progress from 0 to 1, a video's `src`) as props. So the same moment draws the same picture every time.
|
|
@@ -46,16 +46,27 @@ check it scene by scene without running it:
|
|
|
46
46
|
- **`<Layer id x y w h z>`**: a box of the frame in its pixels (the whole frame when left out), stacked by `z`, shown for the whole of its
|
|
47
47
|
scene. How its content moves is the content's own, from `useTime()`. A layer that is a child of `<Film>` is the film's own: drawn across
|
|
48
48
|
every scene, on the film's clock, and no scene's layer may take its id, since a picked element is told by its layer.
|
|
49
|
-
- **`<Line id text>`**: what a scene says, its words as children (`text` where the caption differs). It draws nothing; an engine may
|
|
50
|
-
it or caption it.
|
|
49
|
+
- **`<Line id text at dur>`**: what a scene says, its words as children (`text` where the caption differs). It draws nothing; an engine may
|
|
50
|
+
speak it or caption it. It is said from `at` seconds into its scene (without one, where the line before it ends) for `dur` seconds
|
|
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
|
+
their scene evenly. `<Film captions>` draws the line being said as a `<Caption>`, over everything, fading in and out; `<Caption line="id">`
|
|
53
|
+
draws one line's caption where a component wants it, while that line is said.
|
|
54
|
+
- **`<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
|
+
`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
|
+
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),
|
|
57
|
+
faded in and out over `fadeIn` and `fadeOut` seconds, and `duck` dB lower while a narration speaks. It never plays past the end of its
|
|
58
|
+
scene (or the film). It draws nothing: the engine plays it from the structure, so a `<Sound>` is a child of `<Film>` or a `<Scene>`.
|
|
51
59
|
- Anything else among `<Film>`'s children is drawn across the whole film.
|
|
52
60
|
- `<FilmScene id>` around a film draws its scene `id` alone, on the engine's clock, with the film's own layers on the film's: for an
|
|
53
61
|
engine that places each scene itself. A Remotion project exported from the Open Motion Studio puts each scene in a `Series.Sequence`
|
|
54
62
|
this way, so the scenes are named clips on Remotion Studio's timeline. A scene the film does not have is an error.
|
|
55
63
|
- `filmOutline(children, {width, height})` reads that structure from the elements themselves, without drawing them: `{seconds, scenes:
|
|
56
|
-
[{id, start, duration, layers, lines}], layers}
|
|
64
|
+
[{id, start, duration, layers, lines, sounds}], layers, sounds, captions}`, each line `{id, text, start, end}` and each sound `{src, at, dur, until, trim, loop, volume, fadeIn, fadeOut,
|
|
65
|
+
duck?}` in the film's seconds (`dur` null for the whole file). `<Film>` reports it to the engine's `onFilm` while it is drawn, once for each structure (a new `onFilm` function on every
|
|
57
66
|
render is not called again for the same structure), when its value has one. A scene without an
|
|
58
|
-
id or a positive duration,
|
|
67
|
+
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
|
+
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
|
|
69
|
+
seconds, is an error.
|
|
59
70
|
|
|
60
71
|
## Sound
|
|
61
72
|
|
package/index.d.ts
CHANGED
|
@@ -40,7 +40,8 @@ export declare const Video: FC<VideoProps>;
|
|
|
40
40
|
export declare const MotionRoot: FC<{tokens?: Tokens; style?: CSSProperties; children?: ReactNode}>;
|
|
41
41
|
/** 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
42
|
export declare const MotionScope: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
|
|
43
|
-
|
|
43
|
+
/** 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}>;
|
|
44
45
|
|
|
45
46
|
export type EasingFn = (x: number) => number;
|
|
46
47
|
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};
|
|
@@ -49,19 +50,34 @@ export declare const Sequence: FC<{from?: number; duration?: number; children?:
|
|
|
49
50
|
|
|
50
51
|
/** A box of the frame, in its pixels */
|
|
51
52
|
export type LayerBox = {id: string; x: number; y: number; w: number; h: number; z: number};
|
|
52
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* A <Sound> as the engine plays it, in the film's seconds: from `at`, `dur` seconds of the file from `trim` (null: the whole file, or with
|
|
55
|
+
* `loop` until `until`), never past `until` (the end of its scene, or of the film); `volume` and `duck` in dB, fades in seconds
|
|
56
|
+
*/
|
|
57
|
+
export type FilmSound = {src: string; at: number; dur: number | null; until: number; trim: number; loop: boolean; volume: number; fadeIn: number; fadeOut: number; duck?: number};
|
|
58
|
+
/** A <Film>'s structure: its length, its scenes in order (each from `start` seconds), and its own layers and sounds, across every scene */
|
|
53
59
|
export type FilmOutline = {
|
|
54
60
|
seconds: number;
|
|
55
|
-
scenes: {id: string; start: number; duration: number; layers: LayerBox[]; lines:
|
|
61
|
+
scenes: {id: string; start: number; duration: number; layers: LayerBox[]; lines: FilmLine[]; sounds: FilmSound[]}[];
|
|
56
62
|
layers: LayerBox[];
|
|
63
|
+
sounds: FilmSound[];
|
|
64
|
+
/** Whether the film draws its lines as captions */
|
|
65
|
+
captions: boolean;
|
|
57
66
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
export declare
|
|
67
|
+
/** A line of a scene, and when it is said, in the film's seconds */
|
|
68
|
+
export type FilmLine = {id: string; text: string; start: number; end: number};
|
|
69
|
+
export declare function filmOutline(children: ReactNode, frame?: {width?: number; height?: number; captions?: boolean}): FilmOutline;
|
|
70
|
+
/** 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 captions */
|
|
71
|
+
export declare const Film: FC<{captions?: boolean; children?: ReactNode}>;
|
|
61
72
|
export declare const Scene: FC<{id: string; duration: number; children?: ReactNode}>;
|
|
62
73
|
/** 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 */
|
|
63
74
|
export declare const Layer: FC<{id: string; x?: number; y?: number; w?: number; h?: number; z?: number; style?: CSSProperties; children?: ReactNode}>;
|
|
64
75
|
/** The <Film> under it drawn as its scene `id` alone, on the engine's clock (its own layers on the film's), for an engine that places each scene */
|
|
65
76
|
export declare const FilmScene: FC<{id: string; children?: ReactNode}>;
|
|
66
|
-
/** What a scene says; it draws nothing */
|
|
67
|
-
export declare const Line: FC<{id: string; text?: string; children?: ReactNode}>;
|
|
77
|
+
/** What a scene says, from `at` seconds into it for `dur` seconds (shared evenly when left out); it draws nothing */
|
|
78
|
+
export declare const Line: FC<{id: string; text?: string; at?: number; dur?: number; children?: ReactNode}>;
|
|
79
|
+
/**
|
|
80
|
+
* A sound the film plays, from `at` seconds of its scene (or the film), `dur` seconds of it from `trim` into the file, looped with `loop`, at
|
|
81
|
+
* `volume` dB, faded over `fadeIn`/`fadeOut` seconds, `duck` dB lower under a narration. It draws nothing; the engine plays it
|
|
82
|
+
*/
|
|
83
|
+
export declare const Sound: FC<{src: string; at?: number; dur?: number; trim?: number; loop?: boolean; volume?: number; fadeIn?: number; fadeOut?: number; duck?: number}>;
|
package/index.js
CHANGED
|
@@ -123,13 +123,98 @@ const named = (what, id) => {
|
|
|
123
123
|
return id;
|
|
124
124
|
};
|
|
125
125
|
|
|
126
|
+
/** Seconds as an error says them */
|
|
127
|
+
const round = (value) => Math.round(value * 1000) / 1000;
|
|
128
|
+
|
|
129
|
+
/** A finite number of seconds from `min` (above it when `above`), or `fallback` when left out */
|
|
130
|
+
const seconds = (where, name, value, fallback, {min = 0, above = false} = {}) => {
|
|
131
|
+
if (value === undefined) return fallback;
|
|
132
|
+
if (!(Number.isFinite(value) && (above ? value > min : value >= min))) throw new Error(`${where}: ${name} is a number of seconds ${above ? 'above' : 'from'} ${min}`);
|
|
133
|
+
return value;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A <Sound> as an engine plays it, in the film's time: from `at` (its parent's start, a scene's or the film's, plus its own `at`), for
|
|
138
|
+
* `dur` seconds of its file (null: the whole file, or with `loop` until `until`), never past `until` (the end of its scene, or of the film).
|
|
139
|
+
*/
|
|
140
|
+
const sound = (e, start, until) => {
|
|
141
|
+
const {src, at, dur, trim, loop = false, volume = 0, fadeIn, fadeOut, duck} = e.props;
|
|
142
|
+
if (typeof src !== 'string' || !src) throw new Error('a <Sound> needs a src: the file it plays');
|
|
143
|
+
const where = `<Sound src="${src}">`;
|
|
144
|
+
if (!Number.isFinite(volume)) throw new Error(`${where}: volume is in dB, 0 for the file as it is`);
|
|
145
|
+
if (duck !== undefined && !(Number.isFinite(duck) && duck >= 0)) throw new Error(`${where}: duck is how many dB it drops under a narration, 0 or more`);
|
|
146
|
+
return {
|
|
147
|
+
src,
|
|
148
|
+
at: start + seconds(where, 'at', at, 0),
|
|
149
|
+
dur: seconds(where, 'dur', dur, null, {above: true}),
|
|
150
|
+
until,
|
|
151
|
+
trim: seconds(where, 'trim', trim, 0),
|
|
152
|
+
loop: loop === true,
|
|
153
|
+
volume,
|
|
154
|
+
fadeIn: seconds(where, 'fadeIn', fadeIn, 0),
|
|
155
|
+
fadeOut: seconds(where, 'fadeOut', fadeOut, 0),
|
|
156
|
+
...(duck !== undefined ? {duck} : {}),
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 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
|
+
* 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
|
+
* 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.
|
|
165
|
+
*/
|
|
166
|
+
const timeLines = (lines, start, duration) => {
|
|
167
|
+
const given = lines.map((e) => {
|
|
168
|
+
const where = `<Line id="${e.props.id}">`;
|
|
169
|
+
const at = seconds(where, 'at', e.props.at, undefined);
|
|
170
|
+
if (at !== undefined && at >= duration) throw new Error(`${where}: at is ${at} s, past the end of its scene (${duration} s)`);
|
|
171
|
+
return {where, at, dur: seconds(where, 'dur', e.props.dur, undefined, {above: true})};
|
|
172
|
+
});
|
|
173
|
+
// Lines are said in the order they are written.
|
|
174
|
+
let last = -Infinity;
|
|
175
|
+
for (const g of given) {
|
|
176
|
+
if (g.at === undefined) continue;
|
|
177
|
+
if (g.at < last) throw new Error(`${g.where}: at is ${g.at} s, before the line before it (at ${round(last)} s)`);
|
|
178
|
+
last = g.at;
|
|
179
|
+
}
|
|
180
|
+
const times = [];
|
|
181
|
+
let cursor = 0;
|
|
182
|
+
for (let i = 0; i < given.length;) {
|
|
183
|
+
// The run of lines from here to the next that gives its at: what they do not give, they share.
|
|
184
|
+
let next = i + 1;
|
|
185
|
+
while (next < given.length && given[next].at === undefined) next++;
|
|
186
|
+
const from = given[i].at ?? cursor;
|
|
187
|
+
// Lines are said one after another: one that starts before the one before it ends would be hidden under it.
|
|
188
|
+
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
|
+
const to = next < given.length ? given[next].at : duration;
|
|
190
|
+
const run = given.slice(i, next);
|
|
191
|
+
const fixed = run.reduce((sum, g) => sum + (g.dur ?? 0), 0);
|
|
192
|
+
const free = run.filter((g) => g.dur === undefined);
|
|
193
|
+
const left = to - from - fixed;
|
|
194
|
+
// Lines that give no time would be said in none, and never shown.
|
|
195
|
+
if (free.length && left <= 1e-9) {
|
|
196
|
+
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
|
+
}
|
|
198
|
+
const share = free.length ? left / free.length : 0;
|
|
199
|
+
cursor = from;
|
|
200
|
+
for (const g of run) {
|
|
201
|
+
const end = Math.min(duration, cursor + (g.dur ?? share));
|
|
202
|
+
times.push({start: start + cursor, end: start + end});
|
|
203
|
+
cursor = end;
|
|
204
|
+
}
|
|
205
|
+
i = next;
|
|
206
|
+
}
|
|
207
|
+
return times;
|
|
208
|
+
};
|
|
209
|
+
|
|
126
210
|
/**
|
|
127
211
|
* The structure of a <Film>'s children, read from the elements themselves without drawing them, so it is known before the first frame:
|
|
128
|
-
* `{seconds, scenes: [{id, start, duration, layers, lines}], layers}`, a layer `{id, x, y, w, h, z}` in the frame's
|
|
129
|
-
* fills the frame), a line `{id, text}
|
|
130
|
-
*
|
|
212
|
+
* `{seconds, scenes: [{id, start, duration, layers, lines, sounds}], layers, sounds, captions}`, a layer `{id, x, y, w, h, z}` in the frame's
|
|
213
|
+
* pixels (a box left out fills the frame), a line `{id, text, start, end}` (when it is said, in the film's seconds), a sound `{src, at, dur, until, trim, loop, volume, fadeIn, fadeOut, duck?}` in the
|
|
214
|
+
* 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
|
+
* twice where it must be unique, and a sound with no file or a time that is no number of seconds, are errors.
|
|
131
216
|
*/
|
|
132
|
-
export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
217
|
+
export function filmOutline(children, {width = 1920, height = 1080, captions = false} = {}) {
|
|
133
218
|
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});
|
|
134
219
|
const unique = (what, items) => {
|
|
135
220
|
const seen = new Set();
|
|
@@ -141,6 +226,8 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
141
226
|
};
|
|
142
227
|
const scenes = [];
|
|
143
228
|
const layers = [];
|
|
229
|
+
// The film's own sounds wait for its length, which is known once every scene is read.
|
|
230
|
+
const own = [];
|
|
144
231
|
let start = 0;
|
|
145
232
|
for (const e of elements(children)) {
|
|
146
233
|
if (!isValidElement(e)) continue;
|
|
@@ -149,11 +236,16 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
149
236
|
const {duration} = e.props;
|
|
150
237
|
if (!(Number.isFinite(duration) && duration > 0)) throw new Error(`<Scene id="${id}"> needs its duration in seconds: duration={4}`);
|
|
151
238
|
const inner = elements(e.props.children).filter(isValidElement);
|
|
152
|
-
const
|
|
153
|
-
|
|
239
|
+
const said = inner.filter((c) => c.type === Line);
|
|
240
|
+
const times = timeLines(said, start, duration);
|
|
241
|
+
const lines = said.map((c, i) => ({id: named('Line', c.props.id), text: c.props.text ?? words(c.props.children), ...times[i]}));
|
|
242
|
+
const sounds = inner.filter((c) => c.type === Sound).map((c) => sound(c, start, start + duration));
|
|
243
|
+
scenes.push({id, start, duration, layers: unique(`layers in scene "${id}"`, inner.filter((c) => c.type === Layer).map(box)), lines, sounds});
|
|
154
244
|
start += duration;
|
|
155
245
|
} else if (e.type === Layer) {
|
|
156
246
|
layers.push(box(e));
|
|
247
|
+
} else if (e.type === Sound) {
|
|
248
|
+
own.push(e);
|
|
157
249
|
}
|
|
158
250
|
}
|
|
159
251
|
if (!scenes.length) throw new Error('a <Film> needs at least one <Scene>');
|
|
@@ -165,7 +257,7 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
165
257
|
const taken = scene.layers.find((layer) => layers.some((own) => own.id === layer.id));
|
|
166
258
|
if (taken) throw new Error(`scene "${scene.id}" has a layer called "${taken.id}", the name of one of the film's own layers`);
|
|
167
259
|
}
|
|
168
|
-
return {seconds: start, scenes, layers};
|
|
260
|
+
return {seconds: start, scenes, layers, sounds: own.map((e) => sound(e, 0, start)), captions: captions === true};
|
|
169
261
|
}
|
|
170
262
|
|
|
171
263
|
/**
|
|
@@ -173,12 +265,12 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
173
265
|
* at the film's end), and anything else among its children (a <Layer>, a background) drawn across them all. It reports its structure
|
|
174
266
|
* (filmOutline) to the engine's `onFilm`, if the engine has one, while it is drawn: the engine learns the length from the scenes.
|
|
175
267
|
*/
|
|
176
|
-
export const Film = ({children}) => {
|
|
268
|
+
export const Film = ({captions = false, children}) => {
|
|
177
269
|
const r = runtime();
|
|
178
270
|
// Read on every frame (it is cheap: a film's children are new elements each frame), and reported while drawing, once for each
|
|
179
271
|
// structure, whether or not the engine's onFilm is the same function each time: an engine that renders the film once, off the
|
|
180
272
|
// page, learns it before the first frame is shown.
|
|
181
|
-
const outline = filmOutline(children, r);
|
|
273
|
+
const outline = filmOutline(children, {...r, captions});
|
|
182
274
|
const reported = JSON.stringify(outline);
|
|
183
275
|
const onFilm = r.onFilm;
|
|
184
276
|
useMemo(() => onFilm?.(JSON.parse(reported)), [reported]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
@@ -190,8 +282,12 @@ export const Film = ({children}) => {
|
|
|
190
282
|
// Drawn one scene at a time (an engine that places each scene itself, as Remotion does in a <Series>), the scene keeps the engine's clock
|
|
191
283
|
// and the rest of the film, its own layers, is drawn on the film's.
|
|
192
284
|
const engine = only === null ? r : {...r, useTime: r.useCompositionTime};
|
|
285
|
+
// 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
|
+
const now = only === null ? t : (outline.scenes.find((scene) => scene.id === only)?.start ?? 0) + t;
|
|
287
|
+
const lines = outline.scenes.flatMap((scene) => scene.lines);
|
|
288
|
+
const spoken = lines.find((line) => now >= line.start && now < line.end);
|
|
193
289
|
let index = 0;
|
|
194
|
-
return createElement(Runtime.Provider, {value: engine}, createElement(InFilm.Provider, {value: true}, elements(children).map((e) => {
|
|
290
|
+
return createElement(Runtime.Provider, {value: engine}, createElement(InFilm.Provider, {value: true}, createElement(Said.Provider, {value: {lines, now}}, elements(children).map((e) => {
|
|
195
291
|
if (!isValidElement(e) || e.type !== Scene) return e;
|
|
196
292
|
const scene = outline.scenes[index++];
|
|
197
293
|
const key = e.key ?? scene.id;
|
|
@@ -200,9 +296,15 @@ export const Film = ({children}) => {
|
|
|
200
296
|
if (t < scene.start || (t >= scene.start + scene.duration && !(last && t <= outline.seconds))) return null;
|
|
201
297
|
const value = {...r, useTime: () => r.useTime() - scene.start};
|
|
202
298
|
return createElement(Runtime.Provider, {key, value}, e);
|
|
203
|
-
})
|
|
299
|
+
}), captions && spoken ? createElement('div', {key: 'captions', style: {position: 'absolute', inset: 0, zIndex: 2147483647, pointerEvents: 'none'}},
|
|
300
|
+
createElement(Caption, {line: spoken.id})) : null)));
|
|
204
301
|
};
|
|
205
302
|
|
|
303
|
+
/** The film's lines and its time, for a <Caption line> anywhere in it */
|
|
304
|
+
const Said = createContext(null);
|
|
305
|
+
/** How long a line's caption takes to come and go, in seconds */
|
|
306
|
+
const CAPTION_FADE = 0.15;
|
|
307
|
+
|
|
206
308
|
const OnlyScene = createContext(null);
|
|
207
309
|
/**
|
|
208
310
|
* The <Film> under it drawn as its scene `id` alone, on the engine's clock, with the film's own layers on the film's: for an engine that places
|
|
@@ -232,6 +334,14 @@ export const Layer = ({id, x = 0, y = 0, w, h, z = 0, style, children}) => {
|
|
|
232
334
|
/** What a scene says: its words (the children, or `text` where the caption differs). It draws nothing; an engine may speak or caption it */
|
|
233
335
|
export const Line = () => null;
|
|
234
336
|
|
|
337
|
+
/**
|
|
338
|
+
* A sound the film plays: the file `src`, from `at` seconds of its scene (of the film, for one of the film's own), `dur` seconds of it from
|
|
339
|
+
* `trim` seconds into the file (the whole file without `dur`; with `loop`, again and again to the end of its scene or the film), at `volume`
|
|
340
|
+
* dB (0: as the file is), faded in and out over `fadeIn` and `fadeOut` seconds, and `duck` dB lower while a narration speaks. It draws
|
|
341
|
+
* nothing: the engine plays it, from the film's structure.
|
|
342
|
+
*/
|
|
343
|
+
export const Sound = () => null;
|
|
344
|
+
|
|
235
345
|
/** The ground a motion's components expect: the tokens as custom properties, filling the frame */
|
|
236
346
|
export const MotionRoot = ({tokens, style, children}) => createElement('div', {
|
|
237
347
|
style: {position: 'absolute', inset: 0, overflow: 'hidden', ...tokenVariables(tokens), ...style},
|
|
@@ -257,10 +367,20 @@ export const MotionScope = ({tokens, root = true, children}) => {
|
|
|
257
367
|
/**
|
|
258
368
|
* The motion's caption: one spoken line on the frame, drawn from the caption group of its tokens: font, size, weight, color, background, radius,
|
|
259
369
|
* 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).
|
|
260
|
-
* `opacity` fades it; it stays a function of what you pass.
|
|
370
|
+
* `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
|
+
* it is said, fading in and out, and nothing at other times.
|
|
261
372
|
*/
|
|
262
|
-
export const Caption = ({children, opacity = 1, style}) => {
|
|
373
|
+
export const Caption = ({line, children, opacity = 1, style}) => {
|
|
374
|
+
// Both read before anything branches, so a caption calls the same hooks with a line or without.
|
|
375
|
+
const said = useContext(Said);
|
|
263
376
|
const c = useTokens().caption ?? {};
|
|
377
|
+
if (line !== undefined) {
|
|
378
|
+
const spoken = said?.lines.find((l) => l.id === line);
|
|
379
|
+
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
|
+
const fade = Math.min(1, (said.now - spoken.start) / CAPTION_FADE, (spoken.end - said.now) / CAPTION_FADE);
|
|
381
|
+
if (fade <= 0) return null;
|
|
382
|
+
return createElement(Caption, {opacity: opacity * fade, style}, children ?? spoken.text);
|
|
383
|
+
}
|
|
264
384
|
const py = c['padding-y'] ?? 10, pad = `${py}px ${c['padding-x'] ?? 26}px`;
|
|
265
385
|
const rule = c['rule-left']
|
|
266
386
|
? createElement('div', {style: {position: 'absolute', left: 0, top: py * 0.6, bottom: py * 0.6, width: 4, borderRadius: 2, background: c['rule-left']}})
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionhub/runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), in any engine that provides it.",
|
|
3
|
+
"version": "0.7.0",
|
|
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/motionhub-dev/
|
|
8
|
+
"url": "git+https://github.com/motionhub-dev/motionhub.git",
|
|
9
9
|
"directory": "packages/runtime"
|
|
10
10
|
},
|
|
11
11
|
"homepage": "https://motionhub.dev",
|