|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useRef } from 'react'; |
| 4 | + |
| 5 | +import { cn } from '@/lib/utils'; |
| 6 | + |
| 7 | +interface QaLoaderProps { |
| 8 | + className?: string; |
| 9 | + size?: number; |
| 10 | +} |
| 11 | + |
| 12 | +interface ParticleState { |
| 13 | + x: number; |
| 14 | + y: number; |
| 15 | + angle: number; |
| 16 | + speed: number; |
| 17 | + accel: number; |
| 18 | + radius: number; |
| 19 | + decay: number; |
| 20 | + life: number; |
| 21 | +} |
| 22 | + |
| 23 | +const TWO_PI = Math.PI * 2; |
| 24 | + |
| 25 | +export function QaLoader({ className, size = 240 }: QaLoaderProps) { |
| 26 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 27 | + const particlesRef = useRef<ParticleState[]>([]); |
| 28 | + const animationRef = useRef<number | null>(null); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const canvas = canvasRef.current; |
| 32 | + if (!canvas) return; |
| 33 | + |
| 34 | + const ctx = canvas.getContext('2d'); |
| 35 | + if (!ctx) return; |
| 36 | + |
| 37 | + const dpr = window.devicePixelRatio || 1; |
| 38 | + const width = size; |
| 39 | + const height = size; |
| 40 | + |
| 41 | + canvas.width = width * dpr; |
| 42 | + canvas.height = height * dpr; |
| 43 | + canvas.style.width = `${width}px`; |
| 44 | + canvas.style.height = `${height}px`; |
| 45 | + ctx.setTransform(dpr, 0, 0, dpr, 0, 0); |
| 46 | + ctx.globalCompositeOperation = 'lighter'; |
| 47 | + |
| 48 | + const particles = particlesRef.current; |
| 49 | + const min = width * 0.5; |
| 50 | + let globalAngle = 0; |
| 51 | + let tick = 0; |
| 52 | + let lastFrame = 0; |
| 53 | + |
| 54 | + const spawnParticle = () => { |
| 55 | + particles.push({ |
| 56 | + x: width / 2 + Math.cos(tick / 20) * (min / 2), |
| 57 | + y: height / 2 + Math.sin(tick / 20) * (min / 2), |
| 58 | + angle: globalAngle, |
| 59 | + speed: 0, |
| 60 | + accel: 0.012, |
| 61 | + radius: 7, |
| 62 | + decay: 0.012, |
| 63 | + life: 1, |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + const step = () => { |
| 68 | + spawnParticle(); |
| 69 | + spawnParticle(); |
| 70 | + |
| 71 | + for (let i = particles.length - 1; i >= 0; i -= 1) { |
| 72 | + const p = particles[i]; |
| 73 | + p.speed += p.accel; |
| 74 | + p.x += Math.cos(p.angle) * p.speed; |
| 75 | + p.y += Math.sin(p.angle) * p.speed; |
| 76 | + p.angle += Math.PI / 64; |
| 77 | + p.accel *= 1.01; |
| 78 | + p.life -= p.decay; |
| 79 | + |
| 80 | + if (p.life <= 0) { |
| 81 | + particles.splice(i, 1); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + globalAngle += Math.PI / 3.2; |
| 86 | + }; |
| 87 | + |
| 88 | + const draw = () => { |
| 89 | + ctx.clearRect(0, 0, width, height); |
| 90 | + |
| 91 | + particles.forEach((p, i) => { |
| 92 | + const hue = 300 - (1 - p.life) * 120; |
| 93 | + const alpha = Math.max(0, p.life); |
| 94 | + ctx.fillStyle = `hsla(${hue}, 100%, 60%, ${alpha})`; |
| 95 | + ctx.strokeStyle = `hsla(${hue}, 100%, 60%, ${alpha})`; |
| 96 | + |
| 97 | + if (particles[i - 1]) { |
| 98 | + ctx.beginPath(); |
| 99 | + ctx.moveTo(p.x, p.y); |
| 100 | + ctx.lineTo(particles[i - 1].x, particles[i - 1].y); |
| 101 | + ctx.stroke(); |
| 102 | + } |
| 103 | + |
| 104 | + ctx.beginPath(); |
| 105 | + ctx.arc(p.x, p.y, Math.max(0.001, p.life * p.radius), 0, TWO_PI); |
| 106 | + ctx.fill(); |
| 107 | + |
| 108 | + const sparkle = Math.random() * 1.2; |
| 109 | + ctx.fillRect( |
| 110 | + Math.round(p.x + (Math.random() - 0.5) * 35 * p.life), |
| 111 | + Math.round(p.y + (Math.random() - 0.5) * 35 * p.life), |
| 112 | + sparkle, |
| 113 | + sparkle |
| 114 | + ); |
| 115 | + }); |
| 116 | + }; |
| 117 | + |
| 118 | + const loop = (timestamp: number) => { |
| 119 | + animationRef.current = requestAnimationFrame(loop); |
| 120 | + if (!lastFrame) lastFrame = timestamp; |
| 121 | + const frameDiff = timestamp - lastFrame; |
| 122 | + if (frameDiff < 1000 / 60) return; |
| 123 | + lastFrame = timestamp; |
| 124 | + step(); |
| 125 | + draw(); |
| 126 | + tick += 1; |
| 127 | + }; |
| 128 | + |
| 129 | + animationRef.current = requestAnimationFrame(loop); |
| 130 | + |
| 131 | + return () => { |
| 132 | + if (animationRef.current) cancelAnimationFrame(animationRef.current); |
| 133 | + }; |
| 134 | + }, [size]); |
| 135 | + |
| 136 | + return ( |
| 137 | + <div |
| 138 | + className={cn('relative flex items-center justify-center', className)} |
| 139 | + style={{ width: size, height: size }} |
| 140 | + aria-hidden="true" |
| 141 | + > |
| 142 | + <canvas ref={canvasRef} className="block" /> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +} |
0 commit comments