Created
June 14, 2024 16:29
-
-
Save malerba118/5d64e9e112659d986961209661ce3788 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 { | |
MotionValue, | |
useAnimationFrame, | |
useMotionValue, | |
} from "framer-motion"; | |
import React from "react"; | |
export interface Clock { | |
value: MotionValue<number>; | |
setRate: (rate: number) => void; | |
} | |
export const useClock = ({ defaultValue = 0, rate = 1 } = {}): Clock => { | |
const value = useMotionValue(defaultValue); | |
const rateRef = React.useRef(rate); | |
useAnimationFrame((_, delta) => { | |
value.set(value.get() + delta * rateRef.current); | |
}); | |
return { | |
value, | |
setRate: (rate: number) => { | |
rateRef.current = rate; | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment