🪛 In Progress: Performance on Rive hero for React website
I’m having some issues improving the performance for an interactive animating product diagram for my react website on https://macroscope.com/ - the performance is hovering between 10-20% CPU usage on chrome performance monitor, and our goal is to get it under 5%.
I’ve tried dialing in some runtime parameters in the component that renders the asset, but it doesn’t seem like they’re getting through / taking effect.
Would love some help with adjusting the react component to improve performance on the browser, and if that’s not really going to help, what I should change about the asset. Currently using a lot of svg’s, text, and nested artboards with state machines… really don’t want to have to go through and convert everything to raster assets unless there’s some automated way of doing that.
Here are some code snippets:
import type { RiveFile } from '@rive-app/react-canvas'
/** Performance-related optional props to fine-tune Rive rendering */
export interface RiveAnimationPerfProps {
/** Reuse a parsed .riv instead of refetch + reparsing 20× */
riveFile?: RiveFile
/** Pick the renderer package you installed */
renderer?: 'canvas' | 'webgl'
/** Share a global off-screen WebGL ctx (true by default) */
useOffscreenRenderer?: boolean
/** Obey devicePixelRatio = crisp but heavier; false locks to 1 */
useDevicePixelRatio?: boolean
/** Auto-fit canvas to artboard height; avoids overscan pixels */
fitCanvasToArtboardHeight?: boolean
/** Kill Rive hit-test listeners you’re not using */
shouldDisableRiveListeners?: boolean
/** Optional: throttle render loop */
maxFPS?: number
/** Optional: pause when canvas leaves viewport */
pauseWhenOffscreen?: boolean
}
export interface RiveAnimationStyleOverrides {
container?: React.CSSProperties
}
export interface RiveAnimationProps extends RiveAnimationPerfProps {
/** Path or URL to the .riv file */
riveAnimationSrc: string
/** Artboard within the Rive file to render */
artboard?: string
/** State machine to run (ignored if `timeline` is provided) */
stateMachine?: string
/** Specific animation(s) to run instead of a state machine */
timeline?: string | string[]
/** Values to assign to state machine inputs once the animation is ready */
stateInputs?: Record<string, number | boolean>
/** Explicit width for the rendered animation */
width?: string | number
/** Explicit height for the rendered animation */
height?: string | number
/** Optional style overrides for the container */
sx?: RiveAnimationStyleOverrides
/** Called when the Rive file has finished loading */
onLoad?: () => void
} 'use client';
import React from 'react'
import { useRive } from '@rive-app/react-canvas'
import type { RiveAnimationProps } from './RiveAnimation.types'
function RiveAnimation({
riveAnimationSrc,
artboard,
stateMachine = 'Default',
timeline,
stateInputs,
width = '100%',
height = '100%',
sx,
onLoad,
riveFile,
renderer: _renderer = 'canvas',
useOffscreenRenderer = true,
useDevicePixelRatio = true,
fitCanvasToArtboardHeight = true,
shouldDisableRiveListeners = false,
maxFPS,
pauseWhenOffscreen = true,
}: RiveAnimationProps) {
const containerRef = React.useRef<HTMLDivElement>(null)
// Use offscreen renderer to share GL/canvas context for better performance
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { rive, RiveComponent } = useRive({
src: riveAnimationSrc,
...(riveFile && { riveFile }),
...(artboard && { artboard }),
...(timeline ? { animations: timeline } : { stateMachines: stateMachine }),
autoplay: true,
useOffscreenRenderer,
...(useDevicePixelRatio !== undefined && { useDevicePixelRatio }),
...(fitCanvasToArtboardHeight !== undefined && { fitCanvasToArtboardHeight }),
...(shouldDisableRiveListeners !== undefined && { shouldDisableRiveListeners }),
// Cast to unknown to bypass stricter type checking for experimental props
} as unknown as Record<string, unknown>)
// Apply state inputs once the Rive instance is available
React.useEffect(() => {
if (!rive || !stateInputs) return
try {
const inputs = rive.stateMachineInputs(stateMachine)
if (!inputs?.length) return
Object.entries(stateInputs).forEach(([name, value]) => {
if (value === undefined) return
const input = inputs.find((input) => input.name === name)
if (input) {
input.value = value
}
})
} catch {
// Intentionally swallow errors – animation should still play without inputs
}
}, [rive, stateInputs, stateMachine])
// Setup Intersection Observer to pause when out of view
// and resize drawing surface to optimize for retina displays
React.useEffect(() => {
if (!rive || !containerRef.current) return
// Apply optional FPS throttling if supported
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (maxFPS && typeof (rive as unknown as { setFPS?: (fps: number) => void }).setFPS === 'function') {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(rive as unknown as { setFPS: (fps: number) => void }).setFPS(maxFPS)
} catch {
// Silently ignore if method is unavailable
}
}
// Fire onLoad callback as early as possible
if (onLoad) onLoad()
// Optionally pause when off-screen using IntersectionObserver
if (!pauseWhenOffscreen) return
const observer = new IntersectionObserver(
(entries) => {
const entry = entries[0]
if (entry?.isIntersecting) {
rive.play()
} else {
rive.pause()
}
},
{ threshold: 0.1 }
)
observer.observe(containerRef.current)
return () => {
observer.disconnect()
}
}, [rive, onLoad, maxFPS, pauseWhenOffscreen])
Thanks so much!