MCPcopy
hub / github.com/chinesehuazhou/python-weekly / useAnimatedCounter

Function useAnimatedCounter

landing-page/components/HeroSection.tsx:9–35  ·  view source on GitHub ↗
(end: number, duration: number = 2000, delay: number = 0)

Source from the content-addressed store, hash-verified

7
8// 数字动画Hook - 优化性能
9function useAnimatedCounter(end: number, duration: number = 2000, delay: number = 0) {
10 const [count, setCount] = useState(0);
11
12 const animate = useCallback((currentTime: number, startTime: number) => {
13 const progress = Math.min((currentTime - startTime) / duration, 1);
14
15 // 使用easeOutCubic缓动函数
16 const easeOutCubic = 1 - Math.pow(1 - progress, 3);
17 setCount(Math.floor(end * easeOutCubic));
18
19 if (progress < 1) {
20 requestAnimationFrame((time) => animate(time, startTime));
21 } else {
22 setCount(end);
23 }
24 }, [end, duration]);
25
26 useEffect(() => {
27 const timer = setTimeout(() => {
28 requestAnimationFrame((startTime) => animate(startTime, startTime));
29 }, delay);
30
31 return () => clearTimeout(timer);
32 }, [animate, delay]);
33
34 return count;
35}
36
37function HeroSection() {
38 const t = useTranslations();

Callers 1

HeroSectionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected