@motionhub/remotion 0.2.0 → 0.4.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 CHANGED
@@ -1,9 +1,9 @@
1
1
  # @motionhub/remotion
2
2
 
3
- Use a motion from [motionhub.dev](https://motionhub.dev) in Remotion. Its React components (`motion/react/parts/`) run as they are, drawn by Remotion's frame; its tokens become Remotion values for anything you draw yourself.
3
+ Use a motion from [motionhub.dev](https://motionhub.dev) in Remotion. Its React components (`react/parts/`) run as they are, drawn by Remotion's frame; its tokens become Remotion values for anything you draw yourself.
4
4
 
5
5
  ```sh
6
- npx @motionhub/cli get @iamsuosi/stage-light # into ./motion
6
+ npx @motionhub/cli get @iamsuosi/stage-light # into ./motions/@iamsuosi/stage-light/
7
7
  npm i @motionhub/remotion @motionhub/runtime
8
8
  ```
9
9
 
@@ -13,9 +13,9 @@ Wrap the composition once, outside every `<Sequence>`, with the motion's tokens,
13
13
  import {Sequence, interpolate, useCurrentFrame} from 'remotion';
14
14
  import {Caption} from '@motionhub/runtime';
15
15
  import {MotionFonts, MotionProvider} from '@motionhub/remotion';
16
- import tokens from '../motion/tokens.json';
17
- import {Plate} from '../motion/react/parts/plate.tsx';
18
- import {Numeral} from '../motion/react/parts/numeral.tsx';
16
+ import tokens from '../motions/@iamsuosi/stage-light/tokens.json';
17
+ import {Plate} from '../motions/@iamsuosi/stage-light/react/parts/plate.tsx';
18
+ import {Numeral} from '../motions/@iamsuosi/stage-light/react/parts/numeral.tsx';
19
19
 
20
20
  export const Film = () => {
21
21
  const frame = useCurrentFrame();
@@ -31,10 +31,96 @@ export const Film = () => {
31
31
  };
32
32
  ```
33
33
 
34
- - Each component's props are in its file's `meta` (what it is for, what fits it, each parameter) and signature, and `motion/react/previews/<file>.jpg` shows it. A parameter of type `progress` runs from 0 to 1: animate it with `interpolate`.
34
+ - Each component's props are in its file's `meta` (what it is for, what fits it, each parameter) and signature, and the motion's `react/previews/<file>.jpg` shows it. A parameter of type `progress` runs from 0 to 1: animate it with `interpolate`.
35
35
  - `MotionProvider` gives the components their runtime (`@motionhub/runtime`) from Remotion's frame, and sets the tokens as `--color-*`, `--font-*` and `--caption-*` on a full-frame root (`root={false}` leaves it out). A component's `<Video src>` is an `<OffthreadVideo>`: `src` is a URL or a path in `public/`.
36
- - A face the motion carries in `motion/react/fonts/` (`fonts.json` names each) is loaded with `@remotion/fonts`' `loadFont`, from a copy in `public/`.
36
+ - A face the motion carries in its `react/fonts/` (`fonts.json` names each) is loaded with `@remotion/fonts`' `loadFont`, from a copy in `public/`.
37
37
  - `MotionFonts` loads the faces the tokens name before the first frame, from Google Fonts or, for one it lacks (LXGW WenKai), from where it is. `han` adds the Chinese, Japanese and Korean faces, every part of them (several MB each), since a part fetched only when its text is first drawn arrives too late for that frame. A face no source has is named in the console: bundle it with `@remotion/fonts`.
38
38
  - `useMotion()` turns the tokens into Remotion values: `tokens`, `frames('light')` (a duration in frames), `ease('light')` (an `Easing.bezier`), `progress(frame, start, 'light', 'light')`, for a motion with `duration` and `easing` tokens.
39
39
  - A component that draws in 3D imports `three`: `npm i three`.
40
40
  - The components are imported with their `.tsx` extension; set `"allowImportingTsExtensions": true` and `"noEmit": true` in `tsconfig.json` if you type-check them, and `"resolveJsonModule": true` for `tokens.json`.
41
+
42
+ ## A film of scenes
43
+
44
+ A film drawn as `@motionhub/runtime`'s `<Film>` of `<Scene>`s (0.6 or later; the Open Motion Studio's films are, and its Remotion export
45
+ writes this for one) plays in Remotion with each scene in a `Series.Sequence` of its own, so the scenes are named clips on the Studio's
46
+ timeline. `<FilmScene id>` draws one scene of the film on the sequence's clock; `sceneFrames` gives each its length in frames, and
47
+ `filmMetadata` the composition's, checking that the film's scenes are the ones placed:
48
+
49
+ ```tsx
50
+ import {Composition, Series} from 'remotion';
51
+ import {FilmScene} from '@motionhub/runtime';
52
+ import {MotionProvider, filmMetadata, sceneFrames} from '@motionhub/remotion';
53
+ import Main, {film} from '../film';
54
+
55
+ const frames = sceneFrames(Main, film);
56
+ const scene = (id: string) => <FilmScene id={id}><Main /></FilmScene>;
57
+
58
+ const Film = () => (
59
+ <MotionProvider tokens={{}} root={false}>
60
+ <Series>
61
+ <Series.Sequence name="intro" durationInFrames={frames.intro}>{scene('intro')}</Series.Sequence>
62
+ <Series.Sequence name="how" durationInFrames={frames.how}>{scene('how')}</Series.Sequence>
63
+ </Series>
64
+ </MotionProvider>
65
+ );
66
+
67
+ export const Root = () => (
68
+ <Composition id="film" component={Film} durationInFrames={1} fps={film.fps ?? 30} width={film.width ?? 1920} height={film.height ?? 1080}
69
+ calculateMetadata={() => filmMetadata(Main, film, ['intro', 'how'])} />
70
+ );
71
+ ```
72
+
73
+ Each `Series.Sequence` is written on a line of its own because Remotion Studio's timeline tells sequences apart by where they are written:
74
+ made in a loop, they would show as one row. A scene's seconds are put on whole frames that meet end to end. A scene's length changed in
75
+ the film follows by itself; a scene added, taken out or moved is an error until the `Series` places it. The film's own motions are its
76
+ `<MotionScope>`s, so the provider's tokens can be empty.
77
+
78
+ ## A component's parameters in Remotion Studio
79
+
80
+ `paramsSchema(meta, z)` turns a component's `meta.params` into a composition's schema, so Remotion Studio's props editor edits
81
+ them; `paramDefaults(meta)` gives its `defaultProps`. Pass your project's `zod` (Remotion wants its own version):
82
+
83
+ ```tsx
84
+ import {z} from 'zod';
85
+ import {Composition} from 'remotion';
86
+ import {MotionProvider, paramDefaults, paramsSchema} from '@motionhub/remotion';
87
+ import tokens from '../motions/@iamsuosi/lab-notebook/tokens.json';
88
+ import {CurveArrow, meta} from '../motions/@iamsuosi/lab-notebook/react/parts/arrow.tsx';
89
+
90
+ const Arrow = (props) => <MotionProvider tokens={tokens}><CurveArrow {...props} /></MotionProvider>;
91
+
92
+ // paramDefaults gives what the meta has defaults for; a param with none (here from and to) is yours to give
93
+ <Composition id="arrow" component={Arrow} schema={paramsSchema(meta, z)} defaultProps={{...paramDefaults(meta), from: [400, 700], to: [1500, 380]}}
94
+ durationInFrames={90} fps={30} width={1920} height={1080} />
95
+ ```
96
+
97
+ A param whose `type` is in the vocabulary of SPEC.md §12 (`text`, `string`, `color`, `number`, `px`, `degrees`, `progress`, `boolean`,
98
+ `[x, y]`, `text[]`, or choices written `a | b | c`) is a field described by its `doc`: `progress` from 0 to 1, choices as a list.
99
+ One with no default is optional, one whose default is `null` is nullable. A param of any other type is not in the schema and passes
100
+ through untouched, for code to give. Remotion Studio edits the props live either way; to save them back into the source it needs
101
+ `defaultProps` written as an object literal, so write out what `paramDefaults(meta)` gives once you have it.
102
+
103
+ ## Several motions in one film
104
+
105
+ A film can change motion from scene to scene, or hold two in one frame ([a project's motions](../../SPEC.md#13-a-projects-motions)). Keep one `<MotionProvider>` outside every `<Sequence>`, and wrap each scene in `<MotionScope tokens>` from `@motionhub/runtime` (0.4 or later): the components under it read that motion's tokens and custom properties, and keep Remotion's frame and the film's time. Load every motion's faces before the first frame, each `<MotionFonts>` in its own scope with `root={false}`:
106
+
107
+ ```tsx
108
+ import {Sequence} from 'remotion';
109
+ import {MotionScope} from '@motionhub/runtime';
110
+ import {MotionFonts, MotionProvider} from '@motionhub/remotion';
111
+ import notebook from '../motions/@iamsuosi/lab-notebook/tokens.json';
112
+ import pencil from '../motions/@iamsuosi/red-pencil/tokens.json';
113
+ import {Opening} from '../motions/@iamsuosi/lab-notebook/react/parts/opening.tsx';
114
+ import {Headline} from '../motions/@iamsuosi/red-pencil/react/parts/headline.tsx';
115
+
116
+ export const Film = () => (
117
+ <MotionProvider tokens={notebook} root={false}>
118
+ <MotionScope tokens={notebook} root={false}><MotionFonts /></MotionScope>
119
+ <MotionScope tokens={pencil} root={false}><MotionFonts /></MotionScope>
120
+ <Sequence durationInFrames={150}><MotionScope tokens={notebook}><Opening lines={["Heat pumps"]} /></MotionScope></Sequence>
121
+ <Sequence from={150} durationInFrames={210}><MotionScope tokens={pencil}><Headline title="Where they came from" /></MotionScope></Sequence>
122
+ </MotionProvider>
123
+ );
124
+ ```
125
+
126
+ Do not nest a second `<MotionProvider>` inside a `<Sequence>` for this: it would count the film's time from that sequence's start.
package/film.js ADDED
@@ -0,0 +1,50 @@
1
+ // A film of scenes (@motionhub/runtime's <Film>) in Remotion's frames: each scene's length and the composition's, learned by drawing the
2
+ // film once, off the page, at its start. No Remotion here, so it is tested on its own (test.mjs).
3
+ import {createElement} from 'react';
4
+ import {renderToStaticMarkup} from 'react-dom/server';
5
+ import {MotionRuntimeProvider} from '@motionhub/runtime';
6
+
7
+ /** The structure of the <Film> a component draws */
8
+ function outlineOf(Component, {fps = 30, width = 1920, height = 1080, props = {}}) {
9
+ let outline = null;
10
+ const value = {useTime: () => 0, useCompositionTime: () => 0, fps, width, height, tokens: {}, Video: () => null, onFilm: (o) => { outline = o; }};
11
+ try {
12
+ renderToStaticMarkup(createElement(MotionRuntimeProvider, {value}, createElement(Component, props)));
13
+ } catch (error) {
14
+ throw new Error(`drawing the film once, off the page, at its start, to learn its scenes: ${error.message}. Its motions' tokens are its own <MotionScope>s: the provider here has none`, {cause: error});
15
+ }
16
+ if (!outline) throw new Error('the component draws no <Film> (@motionhub/runtime 0.6)');
17
+ return outline;
18
+ }
19
+
20
+ /** Each scene's frames at `fps`, rounded where it starts and ends so the scenes meet end to end and add up to the film */
21
+ function framesOf(outline, fps) {
22
+ return outline.scenes.map(({id, start, duration}) => {
23
+ const frames = Math.round((start + duration) * fps) - Math.round(start * fps);
24
+ if (frames < 1) throw new Error(`scene "${id}" lasts ${duration} s, less than a frame at ${fps} fps`);
25
+ return [id, frames];
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Each scene of the <Film> a component draws, by id, in whole frames at `fps` that meet end to end and add up to the film: the
31
+ * `durationInFrames` of a Series.Sequence that holds `<FilmScene id>` around the film. A scene shorter than a frame is an error.
32
+ */
33
+ export function sceneFrames(Component, film = {}) {
34
+ return Object.fromEntries(framesOf(outlineOf(Component, film), film.fps ?? 30));
35
+ }
36
+
37
+ /**
38
+ * A composition's metadata for a component that draws a <Film> (`calculateMetadata: () => filmMetadata(Main, film, ['intro', 'how'])`): its
39
+ * length in frames from the film's scenes, so it follows them as they change. With `ids`, the scenes the composition places one by one,
40
+ * the film's scenes must be those, in that order: a scene added, taken out or moved is named in the error.
41
+ */
42
+ export function filmMetadata(Component, film = {}, ids) {
43
+ const {fps = 30, width = 1920, height = 1080} = film;
44
+ const frames = framesOf(outlineOf(Component, film), fps);
45
+ const has = frames.map(([id]) => id);
46
+ if (ids && has.join('\n') !== ids.join('\n')) {
47
+ throw new Error(`the film's scenes are ${has.map((id) => `"${id}"`).join(', ')}, the composition places ${ids.map((id) => `"${id}"`).join(', ')}: give each scene its Series.Sequence, in order`);
48
+ }
49
+ return {durationInFrames: frames.reduce((sum, [, n]) => sum + n, 0), fps, width, height};
50
+ }
package/index.d.ts CHANGED
@@ -2,6 +2,21 @@ import type {FC, ReactNode} from 'react';
2
2
  import type {Tokens} from '@motionhub/runtime';
3
3
 
4
4
  export declare const MotionProvider: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
5
+ /** A component's meta, as SPEC.md §12 has it */
6
+ type MetaOf = {params?: Record<string, {type: string; doc: string; default?: unknown}>};
7
+ /** The types of the vocabulary (SPEC.md §12, Parameters) a schema field is made for */
8
+ export declare const PARAM_TYPES: string[];
9
+ /** The choices of a type written `a | b | c`, or null */
10
+ export declare function paramChoices(type: string): string[] | null;
11
+ /** A composition's schema for a component, from its meta, built with the project's zod (`paramsSchema(meta, z)`) */
12
+ export declare function paramsSchema<Z>(meta: MetaOf, z: Z): any;
13
+ /** A composition's defaultProps for a component: every param's default, where it has one */
14
+ export declare function paramDefaults(meta: MetaOf): Record<string, unknown>;
15
+ type FilmOf<P> = {fps?: number; width?: number; height?: number; props?: P};
16
+ /** Each scene of the <Film> a component draws, by id, in whole frames that meet end to end */
17
+ export declare function sceneFrames<P extends object>(Component: FC<P>, film?: FilmOf<P>): Record<string, number>;
18
+ /** A composition's metadata for a component that draws a <Film>: its length in frames from the scenes; with `ids`, the scenes must be those, in order */
19
+ export declare function filmMetadata<P extends object>(Component: FC<P>, film?: FilmOf<P>, ids?: string[]): {durationInFrames: number; fps: number; width: number; height: number};
5
20
  export declare const MotionFonts: FC<{han?: boolean}>;
6
21
  export declare function families(tokens: Tokens | undefined): string[];
7
22
  export declare function useMotion(): {
package/index.js CHANGED
@@ -1,10 +1,14 @@
1
1
  // @motionhub/remotion: a motion in Remotion. <MotionProvider> gives a motion's React components the runtime they draw with
2
2
  // (@motionhub/runtime) from Remotion's own frame, so they run unchanged; <MotionFonts> loads its faces; useMotion() turns its
3
- // tokens into Remotion values (frames, Easing) for anything you draw yourself.
3
+ // tokens into Remotion values (frames, Easing) for anything you draw yourself. For a film of scenes (@motionhub/runtime's <Film>),
4
+ // sceneFrames() and filmMetadata() give each scene and the composition their length in frames, from the scenes.
4
5
  import {createElement, useEffect, useMemo, useState} from 'react';
5
6
  import {Easing, OffthreadVideo, Sequence, continueRender, delayRender, interpolate, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
6
7
  import {MotionRoot, MotionRuntimeProvider, resolveTokens, useTokens} from '@motionhub/runtime';
7
8
 
9
+ export {filmMetadata, sceneFrames} from './film.js';
10
+ export {PARAM_TYPES, paramChoices, paramDefaults, paramsSchema} from './params.js';
11
+
8
12
  const sequenceSeconds = () => useCurrentFrame() / useVideoConfig().fps;
9
13
 
10
14
  /** The runtime's <Video> on Remotion's: `src` (a URL, or a path in public/), shown from `startAt` s, its source from `trimBefore` s */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@motionhub/remotion",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Use a motion from motionhub.dev in Remotion: its React components as they are, and its tokens as Remotion values.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,12 +20,24 @@
20
20
  },
21
21
  "files": [
22
22
  "index.js",
23
+ "film.js",
24
+ "params.js",
23
25
  "index.d.ts",
24
26
  "README.md"
25
27
  ],
26
28
  "peerDependencies": {
27
- "@motionhub/runtime": "^0.3.0",
29
+ "@motionhub/runtime": "^0.6.0",
30
+ "react-dom": ">=18",
28
31
  "react": ">=18",
29
32
  "remotion": ">=4"
33
+ },
34
+ "scripts": {
35
+ "test": "node --test test.mjs"
36
+ },
37
+ "devDependencies": {
38
+ "@motionhub/runtime": "file:../runtime",
39
+ "react": "19.1.1",
40
+ "react-dom": "19.1.1",
41
+ "zod": "4.6.7"
30
42
  }
31
43
  }
package/params.js ADDED
@@ -0,0 +1,55 @@
1
+ // A component's params (SPEC.md §12, Parameters) as a Remotion composition's schema: each param whose type is in the vocabulary
2
+ // becomes a zod field, described by its doc, so Remotion Studio edits it; any other is left to code and kept as it is. zod is the
3
+ // project's own (pass `z`), since Remotion wants its version. No Remotion here, so it is tested on its own (test.mjs).
4
+
5
+ const TYPES = {
6
+ text: (z) => z.string(),
7
+ string: (z) => z.string(),
8
+ color: (z) => z.string(),
9
+ number: (z) => z.number(),
10
+ px: (z) => z.number(),
11
+ degrees: (z) => z.number(),
12
+ progress: (z) => z.number().min(0).max(1),
13
+ boolean: (z) => z.boolean(),
14
+ '[x, y]': (z) => z.tuple([z.number(), z.number()]),
15
+ 'text[]': (z) => z.array(z.string()),
16
+ };
17
+
18
+ /** The types of the vocabulary a field is made for */
19
+ export const PARAM_TYPES = Object.keys(TYPES);
20
+
21
+ /** The choices of a type written `a | b | c` (words or numbers, none of them a type of the vocabulary or null); null for any other type */
22
+ export function paramChoices(type) {
23
+ const words = type.split(' | ');
24
+ if (words.length < 2 || !words.every((w) => /^(-?\d+(\.\d+)?|[a-z0-9-]+)$/.test(w))) return null;
25
+ return words.some((w) => w in TYPES || w === 'null') ? null : words;
26
+ }
27
+
28
+ /** The zod field for one param, or null when its type is outside the vocabulary */
29
+ function field(param, z) {
30
+ let f = TYPES[param.type]?.(z) ?? null;
31
+ const choices = f ? null : paramChoices(param.type);
32
+ if (choices) f = choices.every((c) => /^-?\d+(\.\d+)?$/.test(c)) ? z.union(choices.map((c) => z.literal(Number(c)))) : z.enum(choices);
33
+ if (!f) return null;
34
+ if (param.default === null) f = f.nullable();
35
+ else if (param.default === undefined) f = f.optional();
36
+ return f.describe(param.doc);
37
+ }
38
+
39
+ /**
40
+ * A composition's `schema` for a component, from its `meta`: `schema={paramsSchema(meta, z)}`. A param of a type outside the
41
+ * vocabulary is not in it, and passes through untouched; one with no default is optional, one whose default is null is nullable.
42
+ */
43
+ export function paramsSchema(meta, z) {
44
+ const shape = {};
45
+ for (const [name, param] of Object.entries(meta?.params ?? {})) {
46
+ const f = field(param, z);
47
+ if (f) shape[name] = f;
48
+ }
49
+ return z.looseObject ? z.looseObject(shape) : z.object(shape).passthrough();
50
+ }
51
+
52
+ /** A composition's `defaultProps` for a component, from its `meta`: every param's default, where it has one */
53
+ export function paramDefaults(meta) {
54
+ return Object.fromEntries(Object.entries(meta?.params ?? {}).filter(([, p]) => p.default !== undefined).map(([name, p]) => [name, p.default]));
55
+ }