Skip to Content
HooksuseViewerAnimations

useViewerAnimations

Controls animation playback for GLB/GLTF models that contain embedded animations.

Usage

import { useViewerAnimations } from "@liveroom/react-immersive"; const { clips, currentClip, isPlaying, speed, time, duration, play, pause, stop, setSpeed, seek, handleAnimationsReady, } = useViewerAnimations(); <ModelViewer onAnimationsReady={handleAnimationsReady} ... />

Returns

NameTypeDescription
clipsstring[]Animation clip names found in the GLB/GLTF file
currentClipstring | nullName of the currently playing clip, or null
isPlayingbooleantrue while an animation is playing (not paused or stopped)
speednumberCurrent playback speed (default 1)
timenumberLive playback position of the current clip in seconds (updates while playing)
durationnumberLength of the current clip in seconds
play(clipName: string) => voidStarts a clip by name; resumes if the same clip is paused
pause() => voidPauses the currently playing clip at its current time
stop() => voidStops the current clip and resets it
setSpeed(speed: number) => voidChanges the playback speed
seek(time: number) => voidScrubs the current clip to an absolute time in seconds (works while playing or paused), clamped to the clip length
handleAnimationsReady(controls: AnimationControls) => voidCallback to pass to onAnimationsReady

Playback Controls Example

const { clips, currentClip, isPlaying, play, pause, stop, setSpeed } = useViewerAnimations(); // Play/pause toggle <button onClick={() => isPlaying ? pause() : play(currentClip ?? clips[0])}> {isPlaying ? "Pause" : "Play"} </button> // Stop <button onClick={stop}>Stop</button> // Speed slider <input type="range" min="0.1" max="3" step="0.1" defaultValue="1" onChange={(e) => setSpeed(Number(e.target.value))} /> // Clip selector {clips.map((name) => ( <button key={name} onClick={() => play(name)}> {name} {currentClip === name && isPlaying ? "▶" : ""} </button> ))}

onAnimationsReady is called even when the model has no animations — with an empty clips array and no-op control functions.