Created
September 18, 2025 15:40
-
-
Save Wigny/721934b31a4fcdd0a5bd8c7c4037f7d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useLayoutEffect } from 'react'; | |
| import { duration } from 'dayjs'; | |
| import { type Duration } from 'dayjs/plugin/duration'; | |
| export function useAnimationFrame(callback: (deltaTime: Duration) => void): void { | |
| useLayoutEffect(() => { | |
| let requestID: number; | |
| let previousTime = 0; | |
| const tick = (time: number) => { | |
| const deltaTime = duration(time - previousTime, 'milliseconds'); | |
| callback(deltaTime); | |
| previousTime = time; | |
| requestID = requestAnimationFrame(tick); | |
| }; | |
| requestID = requestAnimationFrame(tick); | |
| return () => cancelAnimationFrame(requestID); | |
| }, [callback]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment