I am building a website that uses framer-motion for animation
I have this image component
<motion.img
ref={scope}
initial={{ x: -200 }}
alt="pacman"
className="absolute z-50"
src="/pacman.gif"
width={100}
height={50}
/>
and here it is running a series of animation
const [scope, animate] = useAnimate();
const runAnimation = async () => {
await animate(scope.current, { x: 300 }, { duration: 2 });
await animate(scope.current, { scaleX: -1 }, { duration: 0.2 });
await animate(scope.current, { x: -200 }, { duration: 2 });
await animate(scope.current, { scaleX: 1 }, { duration: 0.2 });
};
useEffect(() => {
runAnimation();
}, []);
but I don’t understand how can I restart this series inifinity times in a loop once the series got completed
2
Answers
Here is the answer:
You can call the
runAnimation
function recursively at the end of the animation series. This way, once all the animations are complete, it will start the animation series again.By doing this, the animation series will run indefinitely in a loop.