| 7 | |
| 8 | // 数字动画Hook - 优化性能 |
| 9 | function 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 | |
| 37 | function HeroSection() { |
| 38 | const t = useTranslations(); |