UNPKG

@theguild/components

Version:
28 lines (27 loc) 993 B
import { useRef } from "react"; function useTweenPlaybackRate() { const slowingHandle = useRef(null); const resumingHandle = useRef(null); const time = useRef(null); return function tweenPlaybackRate(animation, step) { const currentHandle = step > 0 ? resumingHandle : slowingHandle; const oppositeHandle = step > 0 ? slowingHandle : resumingHandle; if (oppositeHandle.current) { cancelAnimationFrame(oppositeHandle.current); oppositeHandle.current = time.current = null; } const now = performance.now(); if (time.current) { const deltaTime = now - time.current; const { playbackRate } = animation; const newValue = Math.min(Math.max(playbackRate + step * deltaTime, 0), 1); if (newValue === playbackRate) return; animation.updatePlaybackRate(newValue); } time.current = now; currentHandle.current = requestAnimationFrame(() => tweenPlaybackRate(animation, step)); }; } export { useTweenPlaybackRate };