Skip to Content
ohooks

useAnimationFrame

Runs a callback on every requestAnimationFrame tick with delta time tracking, FPS limiting, and playback controls.

Loading preview...

The useAnimationFrame hook runs a callback on every requestAnimationFrame tick, providing frame timing metadata (timestamp, deltaTime, elapsedTime), optional FPS throttling, and imperative playback controls.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useAnimationFrame.json

Usage

Examples

FPS Limiting

Use fpsLimit to cap how often the callback runs per second while keeping smooth frame pacing.

Manual Start

Set immediate: false to keep the loop paused on mount and start it on user interaction.

API Reference

AnimationFrameInfo

Timing metadata passed to the callback on each animation frame.

Property
Specification
timestamp
number
High-resolution timestamp passed by requestAnimationFrame
deltaTime
number
Time elapsed in milliseconds since the previous frame
elapsedTime
number
Total time elapsed in milliseconds since the loop started

UseAnimationFrameOptions

Configuration options for the animation loop.

Property
Specification
immediate?
uniondefault:true
Start the animation loop immediately on mount
fpsLimit?
number
Optional maximum frames per second limit

UseAnimationFrameControls

Playback controls and state returned by the hook.

Property
Specification
start
function
Starts the animation frame loop
stop
function
Stops the animation frame loop and resets timing counters
toggle
function
Toggles the animation frame loop between active and stopped states
isActive
union
Whether the animation frame loop is currently running