🎥 Make videos programmatically with React
upgrade to [email protected]
Jonny Burgerc94919777bc5
diff --git a/packages/example/src/Rive/RiveExample.tsx b/packages/example/src/Rive/RiveExample.tsxindex ffc20b5849..a5f1dbc9f7 100644--- a/packages/example/src/Rive/RiveExample.tsx+++ b/packages/example/src/Rive/RiveExample.tsx@@ -1,9 +1,14 @@import {AbsoluteFill, useVideoConfig} from 'remotion';+import {RemotionRiveCanvas} from '@remotion/rive';const RiveVehicle = () => {const {height, width} = useVideoConfig();- return <AbsoluteFill style={{height, width}} />;+ return (+ <AbsoluteFill style={{height, width}}>+ <RemotionRiveCanvas src="https://cdn.rive.app/animations/vehicles.riv" />+ </AbsoluteFill>+ );};export default RiveVehicle;diff --git a/packages/rive/.prettierrc.js b/packages/rive/.prettierrc.jsnew file mode 100644index 0000000000..7129f6a51c--- /dev/null+++ b/packages/rive/.prettierrc.js@@ -0,0 +1,14 @@+module.exports = {+ singleQuote: true,+ bracketSpacing: false,+ useTabs: true,+ overrides: [+ {+ files: ["*.yml"],+ options: {+ singleQuote: false,+ },+ },+ ],+ plugins: [require.resolve("prettier-plugin-organize-imports")],+};diff --git a/packages/rive/package.json b/packages/rive/package.jsonindex 9615905d55..a84b2aed1c 100644--- a/packages/rive/package.json+++ b/packages/rive/package.json@@ -21,7 +21,7 @@"author": "Caleb Ojo, Jonny Burger","license": "See LICENSE.md","dependencies": {- "@rive-app/canvas-advanced": "1.0.98",+ "@rive-app/canvas-advanced": "2.3.0","remotion": "workspace:*"},"peerDependencies": {@@ -38,7 +38,9 @@"prettier": "3.0.2","react": "^18.2.0","react-dom": "^18.2.0",- "rollup": "^2.70.1"+ "rollup": "^2.70.1",+ "prettier-plugin-organize-imports": "^3.2.2"+},"publishConfig": {"access": "public"diff --git a/packages/rive/src/RemotionRiveCanvas.tsx b/packages/rive/src/RemotionRiveCanvas.tsxindex 5fa0875f80..518507292b 100644--- a/packages/rive/src/RemotionRiveCanvas.tsx+++ b/packages/rive/src/RemotionRiveCanvas.tsx@@ -1,157 +1,156 @@-import React, { useEffect, useMemo, useRef, useState } from "react";-import type { RiveCanvas } from "@rive-app/canvas-advanced";import type {- Artboard,- CanvasRenderer,- LinearAnimationInstance,-} from "@rive-app/canvas-advanced";-import riveCanvas from "@rive-app/canvas-advanced";+ Artboard,+ CanvasRenderer,+ LinearAnimationInstance,+ RiveCanvas,+} from '@rive-app/canvas-advanced';+import riveCanvas from '@rive-app/canvas-advanced';+import React, {useEffect, useMemo, useRef, useState} from 'react';import {- useVideoConfig,- useCurrentFrame,- delayRender,- continueRender,-} from "remotion";+ continueRender,+ delayRender,+ useCurrentFrame,+ useVideoConfig,+} from 'remotion';import type {- RemotionRiveCanvasAlignment,- RemotionRiveCanvasFit,-} from "./map-enums.js";-import { mapToAlignment } from "./map-enums.js";-import { mapToFit } from "./map-enums.js";+ RemotionRiveCanvasAlignment,+ RemotionRiveCanvasFit,+} from './map-enums.js';+import {mapToAlignment, mapToFit} from './map-enums.js';interface RiveProps {- src: string;- fit?: RemotionRiveCanvasFit;- alignment?: RemotionRiveCanvasAlignment;- artboard?: string | number;- animation?: string | number;+ src: string;+ fit?: RemotionRiveCanvasFit;+ alignment?: RemotionRiveCanvasAlignment;+ artboard?: string | number;+ animation?: string | number;}export const RemotionRiveCanvas: React.FC<RiveProps> = ({- src,- fit = "contain",- alignment = "center",- artboard: artboardName,- animation: animationIndex,+ src,+ fit = 'contain',+ alignment = 'center',+ artboard: artboardName,+ animation: animationIndex,}) => {- 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 [handle] = useState(() => delayRender());- const lastFrame = useRef<number>(0);-- if (err) {- throw err;- }-- const [rive, setRive] = useState<{- animation: LinearAnimationInstance;- renderer: CanvasRenderer;- artboard: Artboard;- } | null>(null);-- useEffect(() => {- riveCanvas({- locateFile: () =>- "https://unpkg.com/@rive-app/[email protected]/rive.wasm",- })- .then((riveInstance) => {- setRiveCanvas(riveInstance);- continueRender(handle);- })- .catch((newErr) => {- setError(newErr);- });- }, [handle]);-- 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 =- typeof artboardName === "string"- ? file.artboardByName(artboardName)- : typeof artboardName === "number"- ? file.artboardByIndex(artboardName)- : file.defaultArtboard();- const animation = new riveCanvasInstance.LinearAnimationInstance(- typeof animationIndex === "number"- ? artboard.animationByIndex(animationIndex)- : typeof animationIndex === "string"- ? artboard.animationByName(animationIndex)- : artboard.animationByIndex(0),- artboard,- );- setRive({- animation,- artboard,- renderer,- });- });- })- .catch((newErr) => {- setError(newErr);- });- }, [animationIndex, artboardName, riveCanvasInstance, src]);-- React.useEffect(() => {- if (!riveCanvasInstance) {- return;- }-- riveCanvasInstance.requestAnimationFrame(() => {- 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, riveCanvasInstance),- 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, fit, alignment]);-- const style: React.CSSProperties = useMemo(- () => ({- height,- width,- }),- [height, width],- );-- return <canvas ref={canvas} width={width} height={height} style={style} />;+ 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 [handle] = useState(() => delayRender());+ const lastFrame = useRef<number>(0);++ if (err) {+ throw err;+ }++ const [rive, setRive] = useState<{+ animation: LinearAnimationInstance;+ renderer: CanvasRenderer;+ artboard: Artboard;+ } | null>(null);++ useEffect(() => {+ riveCanvas({+ locateFile: () =>+ 'https://unpkg.com/@rive-app/[email protected]/rive.wasm',+ })+ .then((riveInstance) => {+ setRiveCanvas(riveInstance);+ continueRender(handle);+ })+ .catch((newErr) => {+ setError(newErr);+ });+ }, [handle]);++ 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 =+ typeof artboardName === 'string'+ ? file.artboardByName(artboardName)+ : typeof artboardName === 'number'+ ? file.artboardByIndex(artboardName)+ : file.defaultArtboard();+ const animation = new riveCanvasInstance.LinearAnimationInstance(+ typeof animationIndex === 'number'+ ? artboard.animationByIndex(animationIndex)+ : typeof animationIndex === 'string'+ ? artboard.animationByName(animationIndex)+ : artboard.animationByIndex(0),+ artboard,+ );+ setRive({+ animation,+ artboard,+ renderer,+ });+ });+ })+ .catch((newErr) => {+ setError(newErr);+ });+ }, [animationIndex, artboardName, riveCanvasInstance, src]);++ React.useEffect(() => {+ if (!riveCanvasInstance) {+ return;+ }++ riveCanvasInstance.requestAnimationFrame(() => {+ 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, riveCanvasInstance),+ 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, fit, alignment]);++ const style: React.CSSProperties = useMemo(+ () => ({+ height,+ width,+ }),+ [height, width],+ );++ return <canvas ref={canvas} width={width} height={height} style={style} />;};diff --git a/packages/rive/src/index.ts b/packages/rive/src/index.tsindex 312d96de5e..2f62401fcf 100644--- a/packages/rive/src/index.ts+++ b/packages/rive/src/index.ts@@ -1 +1 @@-export { RemotionRiveCanvas } from "./RemotionRiveCanvas.js";+export {RemotionRiveCanvas} from './RemotionRiveCanvas.js';diff --git a/packages/rive/src/map-enums.ts b/packages/rive/src/map-enums.tsindex c5fcfa79c0..1e121b9d21 100644--- a/packages/rive/src/map-enums.ts+++ b/packages/rive/src/map-enums.ts@@ -1,98 +1,100 @@-import type { AlignmentFactory } from "@rive-app/canvas-advanced";-import type { Alignment } from "@rive-app/canvas-advanced";-import type { RiveCanvas } from "@rive-app/canvas-advanced";+import type {+ Alignment,+ AlignmentFactory,+ RiveCanvas,+} from '@rive-app/canvas-advanced';export type RemotionRiveCanvasFit =- | "contain"- | "cover"- | "fill"- | "fit-height"- | "none"- | "scale-down"- | "fit-width";+ | 'contain'+ | 'cover'+ | 'fill'+ | 'fit-height'+ | 'none'+ | 'scale-down'+ | 'fit-width';export const mapToFit = (fit: RemotionRiveCanvasFit, canvas: RiveCanvas) => {- if (fit === "contain") {- return canvas.Fit.contain;- }+ if (fit === 'contain') {+ return canvas.Fit.contain;+ }- if (fit === "cover") {- return canvas.Fit.cover;- }+ if (fit === 'cover') {+ return canvas.Fit.cover;+ }- if (fit === "fill") {- return canvas.Fit.fill;- }+ if (fit === 'fill') {+ return canvas.Fit.fill;+ }- if (fit === "fit-height") {- return canvas.Fit.fitHeight;- }+ if (fit === 'fit-height') {+ return canvas.Fit.fitHeight;+ }- if (fit === "fit-width") {- return canvas.Fit.fitWidth;- }+ if (fit === 'fit-width') {+ return canvas.Fit.fitWidth;+ }- if (fit === "none") {- return canvas.Fit.none;- }+ if (fit === 'none') {+ return canvas.Fit.none;+ }- if (fit === "scale-down") {- return canvas.Fit.scaleDown;- }+ if (fit === 'scale-down') {+ return canvas.Fit.scaleDown;+ }- throw new Error("Invalid fit: " + fit);+ 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";+ | '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: RemotionRiveCanvasAlignment,+ factory: AlignmentFactory,): Alignment => {- if (alignment === "center") {- return factory.center;- }+ if (alignment === 'center') {+ return factory.center;+ }- if (alignment === "bottom-center") {- return factory.bottomCenter;- }+ if (alignment === 'bottom-center') {+ return factory.bottomCenter;+ }- if (alignment === "bottom-left") {- return factory.bottomLeft;- }+ if (alignment === 'bottom-left') {+ return factory.bottomLeft;+ }- if (alignment === "bottom-right") {- return factory.bottomRight;- }+ if (alignment === 'bottom-right') {+ return factory.bottomRight;+ }- if (alignment === "center-left") {- return factory.centerLeft;- }+ if (alignment === 'center-left') {+ return factory.centerLeft;+ }- if (alignment === "center-right") {- return factory.centerRight;- }+ if (alignment === 'center-right') {+ return factory.centerRight;+ }- if (alignment === "top-center") {- return factory.topCenter;- }+ if (alignment === 'top-center') {+ return factory.topCenter;+ }- if (alignment === "top-left") {- return factory.topLeft;- }+ if (alignment === 'top-left') {+ return factory.topLeft;+ }- if (alignment === "top-right") {- return factory.topRight;- }+ if (alignment === 'top-right') {+ return factory.topRight;+ }- throw new Error("Invalid alignment: " + alignment);+ throw new Error('Invalid alignment: ' + alignment);};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b2aaa467e4..784302f252 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1532,8 +1532,8 @@ importers:packages/rive:dependencies:'@rive-app/canvas-advanced':- specifier: 1.0.98- version: 1.0.98+ specifier: 2.3.0+ version: 2.3.0remotion:specifier: workspace:*version: link:../core@@ -1559,6 +1559,9 @@ importers:prettier:specifier: 3.0.2version: 3.0.2+ prettier-plugin-organize-imports:+ specifier: ^3.2.2+ version: 3.2.2([email protected])([email protected])react:specifier: ^18.2.0version: 18.2.0@@ -7948,8 +7951,8 @@ packages:three: 0.154.0zustand: 3.7.2([email protected])- /@rive-app/[email protected]:- resolution: {integrity: sha512-Pf2uE1Q9nHbupqlZ/XoaaLGoc3HMkZE7c5heE5qyKVgGeYc08Bf6k5m91vg/pNu8w7VC3xITmdG2Ef5lsH2T6A==}+ /@rive-app/[email protected]:+ resolution: {integrity: sha512-JzgY8aSvWbuigoJ+8x1lHW8or0syQz8hxpRdPijkCCp/GJYRPvBstYdJPbHk2nbv5+BV+8Rv4zRw96wKFVsvKQ==}dev: false