FaceSWAP / frontend /src /components /ui /CountUp.jsx
aditya-rAj19's picture
Initial commit: DeepFace Studio - AI face swap web application
e7a5a81
Raw
History Blame Contribute Delete
909 Bytes
import { useEffect, useRef, useState } from 'react';
import { useInView } from 'framer-motion';
/* Counts from 0 to `end` when scrolled into view */
export default function CountUp({ end, suffix = '', duration = 1.8, className = '' }) {
const ref = useRef(null);
const inView = useInView(ref, { once: true });
const [val, setVal] = useState(0);
useEffect(() => {
if (!inView) return;
let start = 0;
const steps = 60;
const stepTime = (duration * 1000) / steps;
const timer = setInterval(() => {
start++;
const progress = start / steps;
const eased = 1 - Math.pow(1 - progress, 3);
setVal(Math.round(eased * end));
if (start >= steps) clearInterval(timer);
}, stepTime);
return () => clearInterval(timer);
}, [inView, end, duration]);
return (
<span ref={ref} className={className}>
{val}{suffix}
</span>
);
}