Get StartedComponentsStylingThemingMath & Science
MCP

Science

Player

A small animation clock for scenes. usePlayer ticks t (seconds) with requestAnimationFrame and gives you play, pause, seek, speed and looping. PlayerBar is a ready-made control bar for it.

Plot2D, Plot3D and ModelLab use these internally. Use them directly when you animate something else, such as a CSS demo, a canvas or a custom chart.

Markdown

Import

tsx
import { usePlayer, PlayerBar } from "@indoku/ui"

Anatomy

const player = usePlayer({ duration, loop?, autoPlay?, speed? })// player.t, player.playing, player.speed, player.duration// player.play(), pause(), toggle(), seek(t), setSpeed(s)​<PlayerBar player={player} />

Examples

Clock and control bar

Drive anything from player.t. Here a dot moves along a track.

0.00 / 4.00 s

Your own controls

autoPlay: false starts paused and loop: false stops at the end. Build any controls from the returned methods.

t = 0.00 s · paused · 1x

Props

usePlayer(options)

PropTypeDefaultDescription
duration*number–Length in seconds. 0 or less means static: the clock never ticks.
loopbooleantrueWrap around at the end. When false, playback stops at duration.
autoPlaybooleantrue (false with reduced motion)Start playing right away.
speednumber1Initial playback speed multiplier.

PlayerBar

PropTypeDefaultDescription
player*PlayerState–The object returned by usePlayer.

* required. Style props and HTML attributes are accepted as well.

Accessibility

  • The bar uses a native button and input[type=range], both labeled and keyboard accessible.
  • prefers-reduced-motion turns autoplay off by default.

Notes

  • t is React state updated every frame, so keep components that read it small, or compute from it with useMemo.
  • Changing duration resets the clock to 0.

Created by @indokudev