mirror of
https://github.com/nagaoo0/HabbitGrid.git
synced 2026-04-19 15:23:16 +00:00
Add counter animations
This commit is contained in:
34
src/components/AnimatedCounter.jsx
Normal file
34
src/components/AnimatedCounter.jsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
|
||||
/**
|
||||
* AnimatedCounter
|
||||
* Animates a number from 0 (or start) to the target value progressively.
|
||||
* Usage: <AnimatedCounter value={targetNumber} duration={1000} />
|
||||
*/
|
||||
function AnimatedCounter({ value, duration = 1000, start = 0, format = v => v }) {
|
||||
const [displayValue, setDisplayValue] = useState(start);
|
||||
const rafRef = useRef();
|
||||
const startRef = useRef(start);
|
||||
const valueRef = useRef(value);
|
||||
|
||||
useEffect(() => {
|
||||
startRef.current = displayValue;
|
||||
valueRef.current = value;
|
||||
let startTime;
|
||||
function animate(ts) {
|
||||
if (!startTime) startTime = ts;
|
||||
const progress = Math.min((ts - startTime) / duration, 1);
|
||||
const current = Math.round(startRef.current + (valueRef.current - startRef.current) * progress);
|
||||
setDisplayValue(current);
|
||||
if (progress < 1) {
|
||||
rafRef.current = requestAnimationFrame(animate);
|
||||
}
|
||||
}
|
||||
rafRef.current = requestAnimationFrame(animate);
|
||||
return () => cancelAnimationFrame(rafRef.current);
|
||||
}, [value, duration]);
|
||||
|
||||
return <span>{format(displayValue)}</span>;
|
||||
}
|
||||
|
||||
export default AnimatedCounter;
|
||||
Reference in New Issue
Block a user