@motionhub/remotion 0.0.0-stage → 0.1.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.
Files changed (4) hide show
  1. package/README.md +38 -2
  2. package/index.d.ts +13 -0
  3. package/index.js +82 -0
  4. package/package.json +31 -4
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @motionhub/remotion
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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.
4
+
5
+ ```sh
6
+ npx @motionhub/cli get stage-light # into ./motion
7
+ npm i @motionhub/remotion
8
+ ```
9
+
10
+ Wrap the composition once, outside every `<Sequence>`, with the motion's tokens, and use the components with props. Inside a `<Sequence>`, a component's `useTime()` starts with it.
11
+
12
+ ```tsx
13
+ import {Sequence, interpolate, useCurrentFrame} from 'remotion';
14
+ import {Caption} from '@motionhub/runtime';
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';
19
+
20
+ export const Film = () => {
21
+ const frame = useCurrentFrame();
22
+ const value = interpolate(frame, [30, 90], [0, 3], {extrapolateLeft: 'clamp', extrapolateRight: 'clamp'});
23
+ return (
24
+ <MotionProvider tokens={tokens}>
25
+ <MotionFonts />
26
+ <div style={{position: 'absolute', left: 200, top: 170, width: 680, height: 200}}><Plate title="Heat pump" sub="1 kWh in" /></div>
27
+ <div style={{position: 'absolute', left: 200, top: 420, width: 680, height: 360}}><Numeral value={value} unit="kWh" accent /></div>
28
+ <Sequence from={30} durationInFrames={90}><Caption>A heat pump moves three units of heat for each one it uses.</Caption></Sequence>
29
+ </MotionProvider>
30
+ );
31
+ };
32
+ ```
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`.
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
+ - `MotionFonts` loads the faces the tokens name from Google Fonts before the first frame (`han` adds the large Chinese, Japanese and Korean ones). A face it cannot find is named in the console: bundle it with `@remotion/fonts`.
37
+ - `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.
38
+ - A component that draws in 3D imports `three`: `npm i three`.
39
+ - 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`.
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type {FC, ReactNode} from 'react';
2
+ import type {Tokens} from '@motionhub/runtime';
3
+
4
+ export declare const MotionProvider: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
5
+ export declare const MotionFonts: FC<{han?: boolean}>;
6
+ export declare function families(tokens: Tokens | undefined): string[];
7
+ export declare function useMotion(): {
8
+ tokens: Tokens;
9
+ fps: number;
10
+ frames(duration: string): number;
11
+ ease(easing: string): (t: number) => number;
12
+ progress(frame: number, start: number, duration: string, easing?: string): number;
13
+ };
package/index.js ADDED
@@ -0,0 +1,82 @@
1
+ // @motionhub/remotion: a motion in Remotion. <MotionProvider> gives a motion's React components the runtime they draw with
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.
4
+ import {createElement, useEffect, useMemo, useState} from 'react';
5
+ import {Easing, OffthreadVideo, Sequence, continueRender, delayRender, interpolate, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
6
+ import {MotionRoot, MotionRuntimeProvider, resolveTokens, useTokens} from '@motionhub/runtime';
7
+
8
+ const sequenceSeconds = () => useCurrentFrame() / useVideoConfig().fps;
9
+
10
+ /** The runtime's <Video> on Remotion's: `src` (a URL, or a path in public/), shown from `startAt` s, its source from `trimBefore` s */
11
+ const RemotionVideo = ({src, startAt = 0, trimBefore = 0, playbackRate = 1, objectFit = 'cover', style}) => {
12
+ const {fps} = useVideoConfig();
13
+ const url = /^(https?:|\/|data:|blob:)/.test(src) ? src : staticFile(src);
14
+ return createElement(Sequence, {from: Math.round(startAt * fps), layout: 'none'},
15
+ createElement(OffthreadVideo, {src: url, trimBefore: Math.round(trimBefore * fps), playbackRate, style: {width: '100%', height: '100%', objectFit, ...style}}));
16
+ };
17
+
18
+ /**
19
+ * Wrap the composition in it once, outside every <Sequence>, with the motion's tokens.json: `import tokens from '../motion/tokens.json'`. Its components
20
+ * then draw by Remotion's frame: useTime() inside a <Sequence> counts from its start. `root={false}` leaves out the full-frame root that sets the
21
+ * tokens' custom properties (--color-*, --font-*).
22
+ */
23
+ export const MotionProvider = ({tokens, root = true, children}) => {
24
+ const frame = useCurrentFrame();
25
+ const {fps, width, height} = useVideoConfig();
26
+ const resolved = useMemo(() => resolveTokens(tokens), [tokens]);
27
+ const value = {useTime: sequenceSeconds, useCompositionTime: () => frame / fps, fps, width, height, tokens: resolved, Video: RemotionVideo};
28
+ return createElement(MotionRuntimeProvider, {value}, root ? createElement(MotionRoot, {tokens: resolved}, children) : children);
29
+ };
30
+
31
+ // ---- the motion's faces, from Google Fonts, before the first frame ----
32
+ const GENERIC = /^(serif|sans-serif|monospace|cursive|fantasy|system-ui|math|emoji|ui-[a-z-]+)$/;
33
+ const HAN = /^(Noto (Sans|Serif) (SC|TC|JP|KR)|LXGW WenKai|Fusion Pixel.*)$/;
34
+ /** The family names in the tokens' font stacks */
35
+ export const families = (resolved) => [...new Set(Object.values(resolved?.font ?? {}).flatMap((s) => String(s).split(',').map((f) => f.trim().replace(/^["']|["']$/g, ''))))].filter((f) => f && !GENERIC.test(f));
36
+ const css = async (family) => {
37
+ const name = encodeURIComponent(family).replace(/%20/g, '+');
38
+ // Google Fonts refuses a weight range a face does not span, so ask for the common ones in turn, then its default weight
39
+ for (const axes of [':wght@100..900', ':wght@200..900', ':wght@100..800', ':wght@200..800', ':wght@300..800', ':wght@300..700', ':wght@400..700', ':wght@400;700', '']) {
40
+ const res = await fetch(`https://fonts.googleapis.com/css2?family=${name}${axes}&display=block`).catch(() => undefined);
41
+ if (res?.ok) return res.text();
42
+ }
43
+ return undefined;
44
+ };
45
+ /**
46
+ * Loads the faces the tokens name, inside <MotionProvider>. Faces for Chinese, Japanese and Korean are large and load only with `han`; one Google Fonts
47
+ * does not have is named in the console and left to the stack's next face (bundle it with @remotion/fonts).
48
+ */
49
+ export const MotionFonts = ({han = false}) => {
50
+ const tokens = useTokens();
51
+ const [handle] = useState(() => delayRender('the motion\'s faces'));
52
+ useEffect(() => {
53
+ const wanted = families(tokens).filter((f) => han || !HAN.test(f));
54
+ Promise.all(wanted.map(async (family) => {
55
+ const text = await css(family);
56
+ if (!text) { console.warn(`@motionhub/remotion: Google Fonts has no "${family}"; load it with @remotion/fonts`); return; }
57
+ const el = document.createElement('style');
58
+ el.textContent = text;
59
+ document.head.appendChild(el);
60
+ await Promise.all(['400', '700'].map((w) => document.fonts.load(`${w} 32px "${family}"`).catch(() => undefined)));
61
+ })).finally(() => continueRender(handle));
62
+ }, [handle]);
63
+ return null;
64
+ };
65
+
66
+ /**
67
+ * The tokens as Remotion values, inside <MotionProvider>: `tokens`, `frames('light')` (a duration token in frames), `ease('light')` (an easing token as
68
+ * Easing.bezier), and `progress(frame, start, 'light', 'light')`: 0 to 1 over that duration with that curve, clamped.
69
+ */
70
+ export function useMotion() {
71
+ const tokens = useTokens();
72
+ const {fps} = useVideoConfig();
73
+ const need = (group, name) => {
74
+ const v = tokens[group]?.[name];
75
+ if (v === undefined) throw new Error(`the motion has no ${group} token "${name}": it has ${Object.keys(tokens[group] ?? {}).join(', ') || 'none'}`);
76
+ return v;
77
+ };
78
+ const frames = (name) => Math.max(1, Math.round(need('duration', name) * fps));
79
+ const ease = (name) => Easing.bezier(...need('easing', name));
80
+ const progress = (frame, start, duration, easing) => interpolate(frame, [start, start + frames(duration)], [0, 1], {easing: easing ? ease(easing) : undefined, extrapolateLeft: 'clamp', extrapolateRight: 'clamp'});
81
+ return {tokens, fps, frames, ease, progress};
82
+ }
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@motionhub/remotion",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Use a motion from motionhub.dev in Remotion: its React components as they are, and its tokens as Remotion values.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/motionhub-dev/motions.git",
9
+ "directory": "packages/remotion"
10
+ },
11
+ "homepage": "https://motionhub.dev",
12
+ "type": "module",
13
+ "main": "index.js",
14
+ "types": "index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "index.js",
23
+ "index.d.ts",
24
+ "README.md"
25
+ ],
26
+ "dependencies": {
27
+ "@motionhub/runtime": "0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=18",
31
+ "remotion": ">=4"
32
+ }
33
+ }