Skip to content

🎥 Make videos programmatically with React

Added a callback function to mimic the property of the High-level API for Rive

Javier Lopez76d4bd4a30b3
diff --git a/packages/docs/docs/rive/remotionrivecanvas.md b/packages/docs/docs/rive/remotionrivecanvas.md
index 82e2a7f384..bcd59a18ca 100644
--- a/packages/docs/docs/rive/remotionrivecanvas.md
+++ b/packages/docs/docs/rive/remotionrivecanvas.md
@@ -31,7 +31,11 @@ Either a `string` specifying the artboard name, a `number` specifying the artboa
Either a `string` specifying the animation name, a `number` specifying the animation index, otherwise the default animation is being used.
-## Example
+### `onLoad?`
+
+A callback function that will be executed when the Rive Runtime is loaded. The argument callback is an object of type `RiveCanvas`
+
+## Basic Example
```tsx twoslash
import { RemotionRiveCanvas } from "@remotion/rive";
@@ -41,6 +45,28 @@ function App() {
}
```
+## Set Text Run at Runtime Example
+
+This example assumes that your Rive animation has a text run named "city". See [here](https://help.rive.app/runtimes/text#low-level-api-usage) for
+more information about Text Runs on Rive.
+
+```tsx twoslash
+import { useCallback } from 'react';
+import { RemotionRiveCanvas } from "@remotion/rive";
+import { RiveCanvas } from "@rive-app/canvas-advanced";
+
+// Make sure to wrap your onLoad handler on `useCallback` to avoid re-rendering this component every single time
+const onLoadHandler = useCallback((rive: RiveCanvas) => {
+ const artboard = rive.defaultArtboard()
+ const textRun = artboard.textRun("city")
+ textRun.text = "Tokyo"
+}, [])
+
+function App() {
+ return <RemotionRiveCanvas src="https://example.com/myAnimation.riv" onLoad={onLoadHandler} />;
+}
+```
+
## See also
- [`@remotion/lottie`](/docs/lottie)
diff --git a/packages/rive/src/RemotionRiveCanvas.tsx b/packages/rive/src/RemotionRiveCanvas.tsx
index 518507292b..1e7a6fce6c 100644
--- a/packages/rive/src/RemotionRiveCanvas.tsx
+++ b/packages/rive/src/RemotionRiveCanvas.tsx
@@ -18,20 +18,26 @@ import type {
} from './map-enums.js';
import {mapToAlignment, mapToFit} from './map-enums.js';
+type onLoadCallback = (rive: RiveCanvas) => void
+
interface RiveProps {
src: string;
fit?: RemotionRiveCanvasFit;
alignment?: RemotionRiveCanvasAlignment;
artboard?: string | number;
animation?: string | number;
+ onLoad?: onLoadCallback | null;
}
+
+
export const RemotionRiveCanvas: React.FC<RiveProps> = ({
src,
fit = 'contain',
alignment = 'center',
artboard: artboardName,
animation: animationIndex,
+ onLoad = null,
}) => {
const {width, fps, height} = useVideoConfig();
const frame = useCurrentFrame();
@@ -96,12 +102,15 @@ export const RemotionRiveCanvas: React.FC<RiveProps> = ({
artboard,
renderer,
});
+ if (onLoad) {
+ onLoad(riveCanvasInstance);
+ }
});
})
.catch((newErr) => {
setError(newErr);
});
- }, [animationIndex, artboardName, riveCanvasInstance, src]);
+ }, [animationIndex, artboardName, riveCanvasInstance, src, onLoad]);
React.useEffect(() => {
if (!riveCanvasInstance) {