Skip to content
العربية
Motion

ScenePlayer

Play/pause and step dots for a looping scene. Required for large motion that plays with reduced motion.

import { ScenePlayer } from "@percentsa/motion";

Examples

Design

Code
export function ScenePlayerExample() {
  const t = useT();
  const steps = [t("Design", "التصميم"), t("Build", "البناء"), t("Run", "التشغيل")];
  const [playing, setPlaying] = useState(true);
  const [active, setActive] = useState(0);
  const [progress, setProgress] = useState(0);
  useEffect(() => {
    if (!playing) return;
    let last = performance.now();
    let raf = requestAnimationFrame(function tick(now) {
      setProgress((p) => {
        const next = p + (now - last) / STEP_MS;
        if (next >= 1) setActive((a) => (a + 1) % 3);
        return next >= 1 ? 0 : next;
      });
      last = now;
      raf = requestAnimationFrame(tick);
    });
    return () => cancelAnimationFrame(raf);
  }, [playing]);
  return (
    <Stack gap={4} align="center">
      <p className="display text-4xl">{steps[active]}</p>
      <ScenePlayer
        label={t("Services animation", "حركة الخدمات")}
        steps={steps}
        active={active}
        progress={progress}
        playing={playing}
        onToggle={() => setPlaying(!playing)}
        onJump={(i) => (setActive(i), setProgress(0))}
        labels={{ play: t("Play", "تشغيل"), pause: t("Pause", "إيقاف مؤقت"), show: (s) => t(`Show ${s}`, `اعرض ${s}`) }}
      />
    </Stack>
  );
}