Skip to content

🎥 Make videos programmatically with React

clean up a bit

Jonny Burger74453c3d479f
diff --git a/packages/docs/components/TableOfContents/rive.tsx b/packages/docs/components/TableOfContents/rive.tsx
new file mode 100644
index 0000000000..da995911c8
--- /dev/null
+++ b/packages/docs/components/TableOfContents/rive.tsx
@@ -0,0 +1,16 @@
+import React from "react";
+import { Grid } from "./Grid";
+import { TOCItem } from "./TOCItem";
+
+export const TableOfContents: React.FC = () => {
+ return (
+ <div>
+ <Grid>
+ <TOCItem link="/docs/rive/remotionrivecanvas">
+ <strong>{"<RemotionRiveCanvas>"}</strong>
+ <div>Render a Rive animation</div>
+ </TOCItem>
+ </Grid>
+ </div>
+ );
+};
diff --git a/packages/docs/docs/rive/index.md b/packages/docs/docs/rive/index.md
index 887bcd6020..09ecf4fe13 100644
--- a/packages/docs/docs/rive/index.md
+++ b/packages/docs/docs/rive/index.md
@@ -1,5 +1,4 @@
---
-id: rive
sidebar_label: Overview
title: "@remotion/rive"
---
@@ -37,21 +36,3 @@ pnpm i @remotion/rive
```
</TabItem>
</Tabs>
-
-## Props
-
-- `src`: a valid URL of the rive file to load. When it is left empty, the default animation is rendered.
-
-## Usage
-
-```jsx
-import { Rive } from "@remotion/rive";
-
-function App() {
- return (
- <div>
- <Rive src="https://example.com/myAnimation.riv" />
- </div>
- );
-}
-```
diff --git a/packages/docs/docs/rive/remotionrivecanvas.md b/packages/docs/docs/rive/remotionrivecanvas.md
new file mode 100644
index 0000000000..bf97129b92
--- /dev/null
+++ b/packages/docs/docs/rive/remotionrivecanvas.md
@@ -0,0 +1,24 @@
+---
+sidebar_label: Overview
+title: "<RiveCanvas>"
+---
+
+This component can render a [Rive](https://rive.app/) animation so it synchronizes with Remotion's time.
+
+## Props
+
+- `src`: a valid URL of the rive file to load. When it is left empty, the default animation is rendered.
+
+## Usage
+
+```tsx twoslash
+import { RemotionRiveCanvas } from "@remotion/rive";
+
+function App() {
+ return (
+ <div>
+ <RemotionRiveCanvas src="https://example.com/myAnimation.riv" />
+ </div>
+ );
+}
+```
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 25c314486f..b117c0c85d 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -40,6 +40,7 @@
"@remotion/player": "3.3.65",
"@remotion/preload": "3.3.65",
"@remotion/renderer": "3.3.65",
+ "@remotion/rive": "3.3.65",
"@remotion/shapes": "3.3.65",
"@remotion/skia": "3.3.65",
"@remotion/three": "3.3.65",
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 437eabc6cf..a83d48fe57 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -318,6 +318,15 @@ module.exports = {
"shapes/make-pie",
],
},
+ {
+ type: "category",
+ label: "@remotion/rive",
+ link: {
+ type: "doc",
+ id: "rive/index",
+ },
+ items: ["rive/index", "rive/remotionrivecanvas"],
+ },
],
mainSidebar: [
{
diff --git a/packages/example/src/Rive/vehicle.tsx b/packages/example/src/Rive/RiveExample.tsx
similarity index 64%
rename from packages/example/src/Rive/vehicle.tsx
rename to packages/example/src/Rive/RiveExample.tsx
index 7939770492..09473e6915 100644
--- a/packages/example/src/Rive/vehicle.tsx
+++ b/packages/example/src/Rive/RiveExample.tsx
@@ -1,4 +1,4 @@
-import {Rive} from '@remotion/rive';
+import {RemotionRiveCanvas} from '@remotion/rive';
import {AbsoluteFill, useVideoConfig} from 'remotion';
const RiveVehicle = () => {
@@ -6,7 +6,7 @@ const RiveVehicle = () => {
return (
<AbsoluteFill style={{height, width}}>
- <Rive src="https://cdn.rive.app/animations/vehicles.riv" />
+ <RemotionRiveCanvas src="https://cdn.rive.app/animations/vehicles.riv" />
</AbsoluteFill>
);
};
diff --git a/packages/rive/src/Rive.tsx b/packages/rive/src/RemotionRiveCanvas.tsx
similarity index 52%
rename from packages/rive/src/Rive.tsx
rename to packages/rive/src/RemotionRiveCanvas.tsx
index d21ed0ed69..a4ded79e33 100644
--- a/packages/rive/src/Rive.tsx
+++ b/packages/rive/src/RemotionRiveCanvas.tsx
@@ -1,9 +1,9 @@
import React, { useEffect, useMemo, useRef, useState } from "react";
+import type { RiveCanvas } from "@rive-app/canvas-advanced";
import type {
Artboard,
CanvasRenderer,
LinearAnimationInstance,
- RiveCanvas,
} from "@rive-app/canvas-advanced";
import riveCanvas from "@rive-app/canvas-advanced";
import { useVideoConfig, useCurrentFrame } from "remotion";
@@ -11,48 +11,71 @@ import { useVideoConfig, useCurrentFrame } from "remotion";
interface RiveProps {
src: string;
}
-export const Rive: React.FC<RiveProps> = ({ src }) => {
+export const RemotionRiveCanvas: React.FC<RiveProps> = ({ src }) => {
const { width, fps, height } = useVideoConfig();
const frame = useCurrentFrame();
const canvas = useRef<HTMLCanvasElement>(null);
+ const [riveCanvasInstance, setRiveCanvas] = useState<RiveCanvas | null>(null);
+ const [err, setError] = useState<Error | null>(null);
+
+ if (err) {
+ throw err;
+ }
const [rive, setRive] = useState<{
animation: LinearAnimationInstance;
renderer: CanvasRenderer;
artboard: Artboard;
- riveInstance: RiveCanvas;
} | null>(null);
useEffect(() => {
riveCanvas({
locateFile: () =>
"https://unpkg.com/@rive-app/[email protected]/rive.wasm",
- }).then((riveInstance) => {
- const renderer = riveInstance.makeRenderer(
- canvas.current as HTMLCanvasElement
- );
- fetch(new Request(src))
- .then((f) => f.arrayBuffer())
- .then((b) => {
- riveInstance.load(new Uint8Array(b)).then((file) => {
- const artboard = file.defaultArtboard();
- const animation = new riveInstance.LinearAnimationInstance(
- artboard.animationByIndex(0),
- artboard
- );
- setRive({
- animation,
- artboard,
- renderer,
- riveInstance,
- });
+ })
+ .then((riveInstance) => {
+ setRiveCanvas(riveInstance);
+ })
+ .catch((newErr) => {
+ setError(newErr);
+ });
+ }, []);
+
+ useEffect(() => {
+ if (!riveCanvasInstance) {
+ return;
+ }
+
+ const renderer = riveCanvasInstance.makeRenderer(
+ canvas.current as HTMLCanvasElement
+ );
+ fetch(new Request(src))
+ .then((f) => f.arrayBuffer())
+ .then((b) => {
+ riveCanvasInstance.load(new Uint8Array(b)).then((file) => {
+ const artboard = file.defaultArtboard();
+ const animation = new riveCanvasInstance.LinearAnimationInstance(
+ artboard.animationByIndex(0),
+ artboard
+ );
+ setRive({
+ animation,
+ artboard,
+ renderer,
});
});
- });
- }, [src]);
+ })
+ .catch((newErr) => {
+ setError(newErr);
+ });
+ }, [riveCanvasInstance, src]);
React.useEffect(() => {
- rive?.riveInstance.requestAnimationFrame(() => {
+ if (!rive || !canvas.current || !riveCanvasInstance) {
+ return;
+ }
+
+ riveCanvasInstance.requestAnimationFrame(() => {
if (rive && canvas.current) {
rive.renderer.clear();
@@ -65,8 +88,8 @@ export const Rive: React.FC<RiveProps> = ({ src }) => {
rive.renderer.save();
rive.renderer.align(
- rive.riveInstance.Fit.contain,
- rive.riveInstance.Alignment.center,
+ riveCanvasInstance.Fit.contain,
+ riveCanvasInstance.Alignment.center,
{
minX: 0,
minY: 0,
@@ -80,7 +103,7 @@ export const Rive: React.FC<RiveProps> = ({ src }) => {
rive.renderer.restore();
}
});
- }, [frame, fps, rive]);
+ }, [frame, fps, rive, riveCanvasInstance]);
const style: React.CSSProperties = useMemo(
() => ({
diff --git a/packages/rive/src/index.ts b/packages/rive/src/index.ts
index 96379ca2b5..312d96de5e 100644
--- a/packages/rive/src/index.ts
+++ b/packages/rive/src/index.ts
@@ -1 +1 @@
-export { Rive } from "./Rive.js";
+export { RemotionRiveCanvas } from "./RemotionRiveCanvas.js";
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 57e57480e7..9674601e49 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -337,6 +337,7 @@ importers:
'@remotion/player': 3.3.65
'@remotion/preload': 3.3.65
'@remotion/renderer': 3.3.65
+ '@remotion/rive': 3.3.65
'@remotion/shapes': 3.3.65
'@remotion/skia': 3.3.65
'@remotion/three': 3.3.65
@@ -399,6 +400,7 @@ importers:
'@remotion/player': link:../player
'@remotion/preload': link:../preload
'@remotion/renderer': link:../renderer
+ '@remotion/rive': link:../rive
'@remotion/shapes': link:../shapes
'@remotion/skia': link:../skia
'@remotion/three': link:../three
@@ -7468,7 +7470,7 @@ packages:
dependencies:
'@types/istanbul-lib-coverage': 2.0.3
'@types/istanbul-reports': 3.0.1
- '@types/node': 18.11.2
+ '@types/node': 18.14.6
'@types/yargs': 16.0.4
chalk: 4.1.2