Skip to content

🎥 Make videos programmatically with React

enums

Jonny Burger7d2972acf916
diff --git a/packages/docs/docs/rive/remotionrivecanvas.md b/packages/docs/docs/rive/remotionrivecanvas.md
index bf97129b92..fa680f7ba4 100644
--- a/packages/docs/docs/rive/remotionrivecanvas.md
+++ b/packages/docs/docs/rive/remotionrivecanvas.md
@@ -1,6 +1,6 @@
---
sidebar_label: Overview
-title: "<RiveCanvas>"
+title: "<RemotionRiveCanvas>"
---
This component can render a [Rive](https://rive.app/) animation so it synchronizes with Remotion's time.
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index c73174eb85..0769a96496 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -19,7 +19,7 @@ import {OffthreadRemoteVideo} from './OffthreadRemoteVideo/OffthreadRemoteVideo'
import {OrbScene} from './Orb';
import InfinityVideo from './ReallyLongVideo';
import RemoteVideo from './RemoteVideo';
-import RiveVehicle from './Rive/vehicle';
+import RiveVehicle from './Rive/RiveExample';
import {ScalePath} from './ScalePath';
import {Scripts} from './Scripts';
import CircleTest from './Shapes/CircleTest';
diff --git a/packages/rive/src/RemotionRiveCanvas.tsx b/packages/rive/src/RemotionRiveCanvas.tsx
index a4ded79e33..923dc688a1 100644
--- a/packages/rive/src/RemotionRiveCanvas.tsx
+++ b/packages/rive/src/RemotionRiveCanvas.tsx
@@ -7,16 +7,30 @@ import type {
} from "@rive-app/canvas-advanced";
import riveCanvas from "@rive-app/canvas-advanced";
import { useVideoConfig, useCurrentFrame } from "remotion";
+import type {
+ RemotionRiveCanvasAlignment,
+ RemotionRiveCanvasFit,
+} from "./map-enums.js";
+import { mapToAlignment } from "./map-enums.js";
+import { mapToFit } from "./map-enums.js";
interface RiveProps {
src: string;
+ fit?: RemotionRiveCanvasFit;
+ alignment?: RemotionRiveCanvasAlignment;
}
-export const RemotionRiveCanvas: React.FC<RiveProps> = ({ src }) => {
+
+export const RemotionRiveCanvas: React.FC<RiveProps> = ({
+ src,
+ fit = "contain",
+ alignment = "center",
+}) => {
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);
+ const lastFrame = useRef<number>(0);
if (err) {
throw err;
@@ -71,39 +85,46 @@ export const RemotionRiveCanvas: React.FC<RiveProps> = ({ src }) => {
}, [riveCanvasInstance, src]);
React.useEffect(() => {
- if (!rive || !canvas.current || !riveCanvasInstance) {
+ if (!riveCanvasInstance) {
return;
}
+ console.log("hi");
riveCanvasInstance.requestAnimationFrame(() => {
- if (rive && canvas.current) {
- rive.renderer.clear();
-
- if (rive.animation) {
- rive.animation.advance(1 / fps);
- rive.animation.apply(1);
- }
-
- rive.artboard.advance(1 / fps);
-
- rive.renderer.save();
- rive.renderer.align(
- riveCanvasInstance.Fit.contain,
- riveCanvasInstance.Alignment.center,
- {
- minX: 0,
- minY: 0,
- maxX: canvas.current.width,
- maxY: canvas.current.height,
- },
- rive.artboard.bounds
- );
-
- rive.artboard.draw(rive.renderer);
- rive.renderer.restore();
+ if (!rive || !canvas.current) {
+ return;
}
+
+ const diff = frame - lastFrame.current;
+
+ rive.renderer.clear();
+
+ if (rive.animation) {
+ rive.animation.advance(diff / fps);
+ rive.animation.apply(1);
+ }
+
+ rive.artboard.advance(diff / fps);
+
+ rive.renderer.save();
+ rive.renderer.align(
+ mapToFit(fit),
+ mapToAlignment(alignment, riveCanvasInstance.Alignment),
+ {
+ minX: 0,
+ minY: 0,
+ maxX: canvas.current.width,
+ maxY: canvas.current.height,
+ },
+ rive.artboard.bounds
+ );
+
+ rive.artboard.draw(rive.renderer);
+ rive.renderer.restore();
+
+ lastFrame.current = frame;
});
- }, [frame, fps, rive, riveCanvasInstance]);
+ }, [frame, fps, rive, riveCanvasInstance, fit, alignment]);
const style: React.CSSProperties = useMemo(
() => ({
diff --git a/packages/rive/src/map-enums.ts b/packages/rive/src/map-enums.ts
new file mode 100644
index 0000000000..705f214627
--- /dev/null
+++ b/packages/rive/src/map-enums.ts
@@ -0,0 +1,98 @@
+import type { AlignmentFactory } from "@rive-app/canvas-advanced";
+import type { Alignment } from "@rive-app/canvas-advanced";
+import { Fit } from "@rive-app/canvas-advanced";
+
+export type RemotionRiveCanvasFit =
+ | "contain"
+ | "cover"
+ | "fill"
+ | "fit-height"
+ | "none"
+ | "scale-down"
+ | "fit-width";
+
+export const mapToFit = (fit: RemotionRiveCanvasFit): Fit => {
+ if (fit === "contain") {
+ return Fit.contain;
+ }
+
+ if (fit === "cover") {
+ return Fit.cover;
+ }
+
+ if (fit === "fill") {
+ return Fit.fill;
+ }
+
+ if (fit === "fit-height") {
+ return Fit.fitHeight;
+ }
+
+ if (fit === "fit-width") {
+ return Fit.fitWidth;
+ }
+
+ if (fit === "none") {
+ return Fit.none;
+ }
+
+ if (fit === "scale-down") {
+ return Fit.scaleDown;
+ }
+
+ throw new Error("Invalid fit: " + fit);
+};
+
+export type RemotionRiveCanvasAlignment =
+ | "center"
+ | "bottom-center"
+ | "bottom-left"
+ | "bottom-right"
+ | "center-left"
+ | "center-right"
+ | "top-center"
+ | "top-left"
+ | "top-right";
+
+export const mapToAlignment = (
+ alignment: RemotionRiveCanvasAlignment,
+ factory: AlignmentFactory
+): Alignment => {
+ if (alignment === "center") {
+ return factory.center;
+ }
+
+ if (alignment === "bottom-center") {
+ return factory.bottomCenter;
+ }
+
+ if (alignment === "bottom-left") {
+ return factory.bottomLeft;
+ }
+
+ if (alignment === "bottom-right") {
+ return factory.bottomRight;
+ }
+
+ if (alignment === "center-left") {
+ return factory.centerLeft;
+ }
+
+ if (alignment === "center-right") {
+ return factory.centerRight;
+ }
+
+ if (alignment === "top-center") {
+ return factory.topCenter;
+ }
+
+ if (alignment === "top-left") {
+ return factory.topLeft;
+ }
+
+ if (alignment === "top-right") {
+ return factory.topRight;
+ }
+
+ throw new Error("Invalid alignment: " + alignment);
+};