Skip to content

Instantly share code, notes, and snippets.

@malerba118
Created June 14, 2024 16:29
Show Gist options
  • Save malerba118/5d64e9e112659d986961209661ce3788 to your computer and use it in GitHub Desktop.
Save malerba118/5d64e9e112659d986961209661ce3788 to your computer and use it in GitHub Desktop.
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