Skip to content

🎥 Make videos programmatically with React

13.7 KB
Raw Blame
import type {
	Artboard,
	CanvasRenderer,
	File,
	FileAsset,
	LinearAnimationInstance,
	RiveCanvas,
} from '@rive-app/canvas-advanced';
import riveCanvas from '@rive-app/canvas-advanced';
import React, {
	forwardRef,
	useEffect,
	useImperativeHandle,
	useLayoutEffect,
	useMemo,
	useRef,
	useState,
} from 'react';
import type {
	EffectsProp,
	EffectsOutputSize,
	InteractiveBaseProps,
	InteractivePremountProps,
	InteractiveCropProps,
	SequenceControls,
	InteractivitySchema,
} from 'remotion';
import {
	Freeze,
	Sequence,
	Internals,
	Interactive,
	useCurrentFrame,
	useDelayRender,
	useVideoConfig,
} from 'remotion';
import type {
	RemotionRiveCanvasAlignment,
	RemotionRiveCanvasFit,
} from './map-enums.js';
import {mapToAlignment, mapToFit} from './map-enums.js';

const {
	runEffectChain,
	useEffectChainState,
	useMemoizedEffectDefinitions,
	useMemoizedEffects,
} = Internals;

type assetLoadCallback = (asset: FileAsset, bytes: Uint8Array) => boolean;
type onLoadCallback = (file: File) => void;

type RemotionRiveCanvasOwnProps = {
	readonly src: string;
	readonly fit?: RemotionRiveCanvasFit;
	readonly alignment?: RemotionRiveCanvasAlignment;
	readonly artboard?: string | number;
	readonly animation?: string | number;
	readonly onLoad?: onLoadCallback | null;
	readonly enableRiveAssetCdn?: boolean;
	readonly assetLoader?: assetLoadCallback;
	readonly className?: string;
	readonly style?: React.CSSProperties;
	readonly effects?: EffectsProp;
	readonly effectsOutputSize?: EffectsOutputSize;
};

export type RemotionRiveCanvasProps = RemotionRiveCanvasOwnProps &
	InteractiveBaseProps &
	InteractivePremountProps &
	InteractiveCropProps;

export type RiveCanvasRef = {
	getAnimationInstance: () => LinearAnimationInstance | null;
	getArtboard: () => Artboard | null;
	getRenderer: () => CanvasRenderer | null;
	getCanvas: () => RiveCanvas | null;
};

const riveFitVariants: Record<RemotionRiveCanvasFit, InteractivitySchema> = {
	contain: {},
	cover: {},
	fill: {},
	'fit-height': {},
	'fit-width': {},
	none: {},
	'scale-down': {},
};

const riveAlignmentVariants: Record<
	RemotionRiveCanvasAlignment,
	InteractivitySchema
> = {
	center: {},
	'bottom-center': {},
	'bottom-left': {},
	'bottom-right': {},
	'center-left': {},
	'center-right': {},
	'top-center': {},
	'top-left': {},
	'top-right': {},
};

export const riveCanvasSchema: InteractivitySchema = {
	...Internals.baseSchema,
	...Internals.premountSchema,
	fit: {
		type: 'enum',
		default: 'contain',
		description: 'Fit',
		variants: riveFitVariants,
	},
	alignment: {
		type: 'enum',
		default: 'center',
		description: 'Alignment',
		variants: riveAlignmentVariants,
	},
	...Internals.transformSchema,
	...Interactive.backgroundSchema,
	...Interactive.borderSchema,
	...Interactive.borderRadiusSchema,
	...Interactive.cropSchema,
};

type RemotionRiveCanvasContentProps = Omit<
	RemotionRiveCanvasOwnProps,
	'effects' | 'effectsOutputSize'
> & {
	readonly fit: RemotionRiveCanvasFit;
	readonly alignment: RemotionRiveCanvasAlignment;
	readonly enableRiveAssetCdn: boolean;
	readonly effects: EffectsProp;
	readonly effectsOutputSize: EffectsOutputSize | null;
	readonly controls: SequenceControls | undefined;
	readonly canvasRef: React.RefObject<HTMLCanvasElement | null>;
};

const RemotionRiveCanvasContentForwardRefFunction: React.ForwardRefRenderFunction<
	RiveCanvasRef,
	RemotionRiveCanvasContentProps
> = (
	{
		src,
		fit,
		alignment,
		artboard: artboardName,
		animation: animationIndex,
		onLoad = null,
		assetLoader,
		enableRiveAssetCdn,
		className,
		style,
		effects,
		effectsOutputSize,
		controls,
		canvasRef,
	},
	ref,
) => {
	const {width, fps, height} = useVideoConfig();
	const frame = useCurrentFrame();
	const [loadedRuntime, setLoadedRuntime] = useState<{
		instance: RiveCanvas;
		handle: number;
	} | null>(null);
	const riveCanvasInstance = loadedRuntime?.instance ?? null;
	const [err, setError] = useState<Error | null>(null);
	const {delayRender, continueRender} = useDelayRender();
	const lastFrame = useRef<number>(0);

	// Rive draws to this offscreen-style canvas; the effect chain then
	// composites from here onto the visible output canvas.
	const sourceCanvas = useMemo(() => {
		if (typeof document === 'undefined') {
			return null;
		}

		return document.createElement('canvas');
	}, []);

	const chainState = useEffectChainState(effectsOutputSize);

	const memoizedEffects = useMemoizedEffects({
		effects,
		overrideId: controls?.overrideId ?? null,
		videoConfigValues: controls?.videoConfigValues ?? null,
	});

	if (err) {
		throw err;
	}

	const [rive, setRive] = useState<{
		animation: LinearAnimationInstance;
		renderer: CanvasRenderer;
		artboard: Artboard;
		file: File;
		handle: number;
	} | null>(null);

	useImperativeHandle(ref, () => {
		return {
			getAnimationInstance() {
				return rive?.animation ?? null;
			},
			getArtboard() {
				return rive?.artboard ?? null;
			},
			getRenderer() {
				return rive?.renderer ?? null;
			},
			getCanvas() {
				return riveCanvasInstance ?? null;
			},
		};
	}, [rive, riveCanvasInstance]);

	useLayoutEffect(() => {
		const handle = delayRender('Loading Rive runtime');
		let cancelled = false;
		riveCanvas({
			locateFile: () =>
				'https://unpkg.com/@rive-app/[email protected]/rive.wasm',
		})
			.then((riveInstance) => {
				if (!cancelled) {
					setLoadedRuntime({instance: riveInstance, handle});
				}
			})
			.catch((newErr) => {
				if (!cancelled) {
					setError(newErr);
				}
			});
		return () => {
			cancelled = true;
			continueRender(handle);
		};
	}, [delayRender, continueRender]);

	useLayoutEffect(() => {
		if (!riveCanvasInstance || !sourceCanvas || !loadedRuntime) {
			return;
		}

		const handle = delayRender(`Loading <RemotionRiveCanvas src="${src}"/>`);
		continueRender(loadedRuntime.handle);
		let cancelled = false;
		const controller = new AbortController();
		sourceCanvas.width = width;
		sourceCanvas.height = height;

		const renderer = riveCanvasInstance.makeRenderer(sourceCanvas);

		fetch(new Request(src), {signal: controller.signal})
			.then((f) => f.arrayBuffer())
			.then((b) => {
				return riveCanvasInstance
					.load(
						new Uint8Array(b),
						assetLoader
							? new riveCanvasInstance.CustomFileAssetLoader({
									loadContents: assetLoader,
								})
							: undefined,
						enableRiveAssetCdn,
					)
					.then((file) => {
						if (cancelled) {
							return;
						}

						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,
							file,
							handle,
						});
					});
			})
			.catch((newErr) => {
				if (!cancelled) {
					setError(newErr);
				}
			});
		return () => {
			cancelled = true;
			controller.abort();
			continueRender(handle);
		};
	}, [
		animationIndex,
		artboardName,
		riveCanvasInstance,
		src,
		onLoad,
		assetLoader,
		enableRiveAssetCdn,
		sourceCanvas,
		width,
		height,
		loadedRuntime,
		delayRender,
		continueRender,
	]);

	useEffect(() => {
		if (onLoad && rive) {
			onLoad(rive.file);
		}
	}, [onLoad, rive]);

	useLayoutEffect(() => {
		if (!riveCanvasInstance || !rive) {
			return;
		}

		const outputCanvas = canvasRef.current;
		if (!outputCanvas || !sourceCanvas) {
			return;
		}

		// Keep source/output dimensions in sync with the video config.
		if (sourceCanvas.width !== width || sourceCanvas.height !== height) {
			sourceCanvas.width = width;
			sourceCanvas.height = height;
		}

		if (outputCanvas.width !== width || outputCanvas.height !== height) {
			outputCanvas.width = width;
			outputCanvas.height = height;
		}

		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: sourceCanvas.width,
				maxY: sourceCanvas.height,
			},
			rive.artboard.bounds,
		);

		rive.artboard.draw(rive.renderer);
		rive.renderer.restore();

		// Flush the renderer's queued draw calls so `sourceCanvas` actually
		// contains the pixels we just drew before `runEffectChain` reads from
		// it. We don't use `riveCanvasInstance.requestAnimationFrame` (which
		// would flush implicitly at the end of its own callback) because the
		// effect chain needs the flushed pixels synchronously, and waiting
		// for a Rive-scheduled frame would mean the very first paint after
		// mount (and after every prop change) would composite an empty /
		// stale source canvas to the output.
		riveCanvasInstance.resolveAnimationFrame();

		lastFrame.current = frame;

		const state = chainState.get(width, height);
		if (!state) {
			return;
		}

		const effectChainHandle = delayRender(
			`Rendering frame at ${frame} of <RemotionRiveCanvas src="${src}"/>`,
		);
		continueRender(rive.handle);

		let cancelled = false;

		runEffectChain({
			state,
			source: sourceCanvas,
			effects: memoizedEffects,
			output: outputCanvas,
			width,
			height,
		})
			.then(() => {
				if (!cancelled) {
					continueRender(effectChainHandle);
				}
			})
			.catch((newErr) => {
				setError(newErr);
			});

		return () => {
			cancelled = true;
			continueRender(effectChainHandle);
		};
	}, [
		frame,
		fps,
		rive,
		riveCanvasInstance,
		fit,
		alignment,
		width,
		height,
		sourceCanvas,
		memoizedEffects,
		chainState,
		delayRender,
		continueRender,
		src,
		canvasRef,
	]);

	const canvasStyle: React.CSSProperties = useMemo(
		() => ({
			height,
			width,
			...style,
		}),
		[height, style, width],
	);

	return (
		<canvas
			ref={canvasRef}
			width={width}
			height={height}
			className={className}
			style={canvasStyle}
		/>
	);
};

const RemotionRiveCanvasContent = forwardRef(
	RemotionRiveCanvasContentForwardRefFunction,
);

const RemotionRiveCanvasInnerForwardRefFunction: React.ForwardRefRenderFunction<
	RiveCanvasRef,
	RemotionRiveCanvasProps & {
		readonly controls?: SequenceControls | undefined;
	}
> = (
	{
		src,
		fit = 'contain',
		alignment = 'center',
		artboard,
		animation,
		onLoad = null,
		assetLoader,
		enableRiveAssetCdn = true,
		className,
		style,
		effects = [],
		effectsOutputSize,
		controls,
		durationInFrames,
		name,
		from,
		premountFor,
		postmountFor,
		styleWhilePremounted,
		styleWhilePostmounted,
		trimBefore,
		playbackRate,
		loop,
		freeze,
		showInTimeline,
		hidden,
		cropLeft,
		cropRight,
		cropTop,
		cropBottom,
		...props
	},
	ref,
) => {
	props satisfies Record<string, never>;

	const memoizedEffectDefinitions = useMemoizedEffectDefinitions(effects);
	const canvasRef = useRef<HTMLCanvasElement | null>(null);
	const {
		effectivePremountFor,
		effectivePostmountFor,
		freezeFrame,
		isPremountingOrPostmounting,
		premountingActive,
		postmountingActive,
		premountingStyle,
	} = Internals.usePremounting({
		from: from ?? 0,
		durationInFrames: Internals.resolveSequenceDuration({
			durationInFrames,
			playbackRate,
			loop,
		}),
		premountFor: premountFor ?? null,
		postmountFor: postmountFor ?? null,
		style: style ?? null,
		styleWhilePremounted: styleWhilePremounted ?? null,
		styleWhilePostmounted: styleWhilePostmounted ?? null,
		hideWhilePremounted: 'opacity',
	});
	const croppedStyle = Internals.useCropStyle({
		cropLeft,
		cropRight,
		cropTop,
		cropBottom,
		style: premountingStyle,
		componentName: '<RemotionRiveCanvas />',
	});

	return (
		<Freeze frame={freezeFrame} active={isPremountingOrPostmounting}>
			<Sequence
				layout="none"
				from={from}
				trimBefore={trimBefore}
				playbackRate={playbackRate}
				loop={loop}
				freeze={freeze}
				hidden={hidden}
				showInTimeline={showInTimeline}
				name={name ?? '<RemotionRiveCanvas>'}
				_remotionInternalDocumentationLink="https://www.remotion.dev/docs/rive/remotionrivecanvas"
				durationInFrames={durationInFrames}
				controls={controls}
				_remotionInternalEffects={memoizedEffectDefinitions}
				{...props}
				_remotionInternalPremountDisplay={effectivePremountFor || null}
				_remotionInternalPostmountDisplay={effectivePostmountFor || null}
				_remotionInternalIsPremounting={premountingActive}
				_remotionInternalIsPostmounting={postmountingActive}
			>
				<RemotionRiveCanvasContent
					ref={ref}
					src={src}
					fit={fit}
					alignment={alignment}
					artboard={artboard}
					animation={animation}
					onLoad={onLoad}
					assetLoader={assetLoader}
					enableRiveAssetCdn={enableRiveAssetCdn}
					className={className}
					style={croppedStyle ?? undefined}
					effects={effects}
					effectsOutputSize={effectsOutputSize ?? null}
					controls={controls}
					canvasRef={canvasRef}
				/>
			</Sequence>
		</Freeze>
	);
};

const RemotionRiveCanvasInner = forwardRef(
	RemotionRiveCanvasInnerForwardRefFunction,
);

export const RemotionRiveCanvas = Interactive.withSchema({
	Component: RemotionRiveCanvasInner as unknown as React.ComponentType<
		RemotionRiveCanvasProps & {
			readonly controls: SequenceControls | undefined;
		}
	>,
	componentName: '<RemotionRiveCanvas>',
	componentIdentity: 'dev.remotion.rive.RemotionRiveCanvas',
	schema: riveCanvasSchema,
	supportsEffects: true,
}) as React.ForwardRefExoticComponent<
	RemotionRiveCanvasProps & React.RefAttributes<RiveCanvasRef>
>;

(RemotionRiveCanvas as unknown as {displayName: string}).displayName =
	'RemotionRiveCanvas';