|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | + |
| 5 | +interface Particle { |
| 6 | + x: number; |
| 7 | + y: number; |
| 8 | + vx: number; |
| 9 | + vy: number; |
| 10 | + radius: number; |
| 11 | + baseOpacity: number; |
| 12 | + // despawn lifecycle |
| 13 | + dying: boolean; |
| 14 | + dieStartTs: number; // timestamp when despawn began |
| 15 | + spawnProgress: number; // 0→1 fade-in on spawn |
| 16 | +} |
| 17 | + |
| 18 | +const ACCENT = { r: 6, g: 182, b: 212 }; |
| 19 | +const PARTICLE_COUNT = 80; |
| 20 | +const CONNECTION_DIST = 130; |
| 21 | +const GRAVITY_STRENGTH = 1800; |
| 22 | +const GRAVITY_RADIUS = 350; // gravity only pulls within this px radius |
| 23 | +const MAX_SPEED = 2.5; |
| 24 | +const CURSOR_RING_RADIUS = 18; // the drawn follower ring |
| 25 | +const DESPAWN_DURATION = 5000; // ms to fade out after touch |
| 26 | +const SPAWN_DURATION = 800; // ms to fade in on respawn |
| 27 | +const GRID_COLS = 28; |
| 28 | +const GRID_ROWS = 18; |
| 29 | +const WAVE_SPEED = 0.0012; |
| 30 | +const WAVE_AMPLITUDE = 6; |
| 31 | + |
| 32 | +const SPAWN_SPEED_MIN = 0.6; |
| 33 | +const SPAWN_SPEED_MAX = 2.2; |
| 34 | + |
| 35 | +function makeParticle(W: number, H: number): Particle { |
| 36 | + const angle = Math.random() * Math.PI * 2; |
| 37 | + const speed = SPAWN_SPEED_MIN + Math.random() * (SPAWN_SPEED_MAX - SPAWN_SPEED_MIN); |
| 38 | + return { |
| 39 | + x: Math.random() * W, |
| 40 | + y: Math.random() * H, |
| 41 | + vx: Math.cos(angle) * speed, |
| 42 | + vy: Math.sin(angle) * speed, |
| 43 | + radius: Math.random() * 1.5 + 0.5, |
| 44 | + baseOpacity: Math.random() * 0.4 + 0.1, |
| 45 | + dying: false, |
| 46 | + dieStartTs: 0, |
| 47 | + spawnProgress: 0, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +export default function CursorGlow() { |
| 52 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 53 | + const cursorRef = useRef({ x: -1000, y: -1000 }); |
| 54 | + const followerRef = useRef({ x: -1000, y: -1000 }); |
| 55 | + const frameRef = useRef<number>(0); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + const canvas = canvasRef.current; |
| 59 | + if (!canvas) return; |
| 60 | + const ctx = canvas.getContext("2d")!; |
| 61 | + |
| 62 | + function resize() { |
| 63 | + canvas!.width = window.innerWidth; |
| 64 | + canvas!.height = window.innerHeight; |
| 65 | + } |
| 66 | + resize(); |
| 67 | + window.addEventListener("resize", resize); |
| 68 | + |
| 69 | + function onMouseMove(e: MouseEvent) { |
| 70 | + cursorRef.current = { x: e.clientX, y: e.clientY }; |
| 71 | + } |
| 72 | + window.addEventListener("mousemove", onMouseMove, { passive: true }); |
| 73 | + |
| 74 | + const particles: Particle[] = Array.from({ length: PARTICLE_COUNT }, () => |
| 75 | + makeParticle(window.innerWidth, window.innerHeight) |
| 76 | + ); |
| 77 | + // stagger spawn-in so they don't all pop in at once |
| 78 | + particles.forEach((p, i) => { p.spawnProgress = i / PARTICLE_COUNT; }); |
| 79 | + |
| 80 | + function draw(ts: number) { |
| 81 | + const W = canvas!.width; |
| 82 | + const H = canvas!.height; |
| 83 | + ctx.clearRect(0, 0, W, H); |
| 84 | + |
| 85 | + // ── Wave grid ── |
| 86 | + const cellW = W / GRID_COLS; |
| 87 | + const cellH = H / GRID_ROWS; |
| 88 | + ctx.lineWidth = 0.5; |
| 89 | + |
| 90 | + for (let row = 0; row <= GRID_ROWS; row++) { |
| 91 | + ctx.beginPath(); |
| 92 | + for (let col = 0; col <= GRID_COLS; col++) { |
| 93 | + const bx = col * cellW; |
| 94 | + const by = row * cellH; |
| 95 | + const distToCursor = Math.hypot(bx - cursorRef.current.x, by - cursorRef.current.y); |
| 96 | + const waveY = |
| 97 | + Math.sin(col * 0.3 + ts * WAVE_SPEED) * WAVE_AMPLITUDE + |
| 98 | + Math.sin(row * 0.4 + ts * WAVE_SPEED * 0.7) * WAVE_AMPLITUDE * 0.5; |
| 99 | + const cursorPush = Math.max(0, 1 - distToCursor / 220) * 18; |
| 100 | + const angle = Math.atan2(by - cursorRef.current.y, bx - cursorRef.current.x); |
| 101 | + const px = bx + Math.cos(angle) * cursorPush; |
| 102 | + const py = by + waveY + Math.sin(angle) * cursorPush; |
| 103 | + ctx.strokeStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},0.06)`; |
| 104 | + col === 0 ? ctx.moveTo(px, py) : ctx.lineTo(px, py); |
| 105 | + } |
| 106 | + ctx.stroke(); |
| 107 | + } |
| 108 | + |
| 109 | + for (let col = 0; col <= GRID_COLS; col++) { |
| 110 | + ctx.beginPath(); |
| 111 | + for (let row = 0; row <= GRID_ROWS; row++) { |
| 112 | + const bx = col * cellW; |
| 113 | + const by = row * cellH; |
| 114 | + const distToCursor = Math.hypot(bx - cursorRef.current.x, by - cursorRef.current.y); |
| 115 | + const waveX = |
| 116 | + Math.sin(row * 0.3 + ts * WAVE_SPEED * 0.8) * WAVE_AMPLITUDE + |
| 117 | + Math.sin(col * 0.4 + ts * WAVE_SPEED * 0.5) * WAVE_AMPLITUDE * 0.5; |
| 118 | + const cursorPush = Math.max(0, 1 - distToCursor / 220) * 18; |
| 119 | + const angle = Math.atan2(by - cursorRef.current.y, bx - cursorRef.current.x); |
| 120 | + const px = bx + waveX + Math.cos(angle) * cursorPush; |
| 121 | + const py = by + Math.sin(angle) * cursorPush; |
| 122 | + ctx.strokeStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},0.06)`; |
| 123 | + row === 0 ? ctx.moveTo(px, py) : ctx.lineTo(px, py); |
| 124 | + } |
| 125 | + ctx.stroke(); |
| 126 | + } |
| 127 | + |
| 128 | + // ── Update follower first so ring position is ready for touch-detection ── |
| 129 | + const cx = cursorRef.current.x; |
| 130 | + const cy = cursorRef.current.y; |
| 131 | + followerRef.current.x += (cx - followerRef.current.x) * 0.1; |
| 132 | + followerRef.current.y += (cy - followerRef.current.y) * 0.1; |
| 133 | + const fx = followerRef.current.x; |
| 134 | + const fy = followerRef.current.y; |
| 135 | + |
| 136 | + // ── Particles ── |
| 137 | + for (let i = 0; i < particles.length; i++) { |
| 138 | + const p = particles[i]; |
| 139 | + |
| 140 | + // Fade-in on spawn |
| 141 | + if (p.spawnProgress < 1) { |
| 142 | + p.spawnProgress = Math.min(1, p.spawnProgress + 1 / (SPAWN_DURATION / 16.67)); |
| 143 | + } |
| 144 | + |
| 145 | + // Check if touching the follower ring (not yet dying) |
| 146 | + if (!p.dying) { |
| 147 | + const distToFollower = Math.hypot(p.x - fx, p.y - fy); |
| 148 | + if (distToFollower <= CURSOR_RING_RADIUS + p.radius) { |
| 149 | + p.dying = true; |
| 150 | + p.dieStartTs = ts; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // If dying, check if fully faded — respawn |
| 155 | + if (p.dying) { |
| 156 | + const elapsed = ts - p.dieStartTs; |
| 157 | + if (elapsed >= DESPAWN_DURATION) { |
| 158 | + // Respawn at a random edge so it drifts in naturally |
| 159 | + const newP = makeParticle(W, H); |
| 160 | + newP.spawnProgress = 0; |
| 161 | + particles[i] = newP; |
| 162 | + continue; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Gravity — only within GRAVITY_RADIUS |
| 167 | + const dx = cx - p.x; |
| 168 | + const dy = cy - p.y; |
| 169 | + const distToCursor = Math.sqrt(dx * dx + dy * dy); |
| 170 | + |
| 171 | + if (distToCursor < GRAVITY_RADIUS) { |
| 172 | + const distSq = dx * dx + dy * dy + 200; |
| 173 | + const dist = Math.sqrt(distSq); |
| 174 | + // Taper force to 0 at the radius edge so there's no hard cutoff snap |
| 175 | + const falloff = 1 - distToCursor / GRAVITY_RADIUS; |
| 176 | + const force = GRAVITY_STRENGTH / distSq * falloff; |
| 177 | + p.vx += (dx / dist) * force; |
| 178 | + p.vy += (dy / dist) * force; |
| 179 | + } |
| 180 | + |
| 181 | + // Cap speed |
| 182 | + const speed = Math.sqrt(p.vx * p.vx + p.vy * p.vy); |
| 183 | + if (speed > MAX_SPEED) { |
| 184 | + p.vx = (p.vx / speed) * MAX_SPEED; |
| 185 | + p.vy = (p.vy / speed) * MAX_SPEED; |
| 186 | + } |
| 187 | + |
| 188 | + p.vx *= 0.985; |
| 189 | + p.vy *= 0.985; |
| 190 | + p.x += p.vx; |
| 191 | + p.y += p.vy; |
| 192 | + |
| 193 | + // Wrap edges |
| 194 | + if (p.x < 0) p.x = W; |
| 195 | + if (p.x > W) p.x = 0; |
| 196 | + if (p.y < 0) p.y = H; |
| 197 | + if (p.y > H) p.y = 0; |
| 198 | + |
| 199 | + // Compute final opacity |
| 200 | + let alpha = p.baseOpacity; |
| 201 | + // Fade in |
| 202 | + alpha *= Math.min(1, p.spawnProgress); |
| 203 | + // Fade out if dying |
| 204 | + if (p.dying) { |
| 205 | + const t = (ts - p.dieStartTs) / DESPAWN_DURATION; |
| 206 | + alpha *= Math.max(0, 1 - t); |
| 207 | + } |
| 208 | + |
| 209 | + // Draw dot |
| 210 | + ctx.beginPath(); |
| 211 | + ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2); |
| 212 | + ctx.fillStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},${alpha})`; |
| 213 | + ctx.fill(); |
| 214 | + } |
| 215 | + |
| 216 | + // ── Connections ── |
| 217 | + for (let i = 0; i < particles.length; i++) { |
| 218 | + for (let j = i + 1; j < particles.length; j++) { |
| 219 | + const pi = particles[i]; |
| 220 | + const pj = particles[j]; |
| 221 | + const dx = pi.x - pj.x; |
| 222 | + const dy = pi.y - pj.y; |
| 223 | + const d = Math.sqrt(dx * dx + dy * dy); |
| 224 | + if (d < CONNECTION_DIST) { |
| 225 | + // Inherit opacity of the dimmer of the two |
| 226 | + const aI = pi.dying ? Math.max(0, 1 - (ts - pi.dieStartTs) / DESPAWN_DURATION) : pi.spawnProgress; |
| 227 | + const aJ = pj.dying ? Math.max(0, 1 - (ts - pj.dieStartTs) / DESPAWN_DURATION) : pj.spawnProgress; |
| 228 | + const alpha = (1 - d / CONNECTION_DIST) * 0.18 * Math.min(aI, aJ); |
| 229 | + ctx.beginPath(); |
| 230 | + ctx.moveTo(pi.x, pi.y); |
| 231 | + ctx.lineTo(pj.x, pj.y); |
| 232 | + ctx.strokeStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},${alpha})`; |
| 233 | + ctx.lineWidth = 0.6; |
| 234 | + ctx.stroke(); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + // ── Cursor-to-particle lines ── |
| 240 | + for (const p of particles) { |
| 241 | + const dx = p.x - cx; |
| 242 | + const dy = p.y - cy; |
| 243 | + const d = Math.sqrt(dx * dx + dy * dy); |
| 244 | + if (d < 200) { |
| 245 | + const fade = p.dying ? Math.max(0, 1 - (ts - p.dieStartTs) / DESPAWN_DURATION) : p.spawnProgress; |
| 246 | + const alpha = (1 - d / 200) * 0.35 * fade; |
| 247 | + ctx.beginPath(); |
| 248 | + ctx.moveTo(cx, cy); |
| 249 | + ctx.lineTo(p.x, p.y); |
| 250 | + ctx.strokeStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},${alpha})`; |
| 251 | + ctx.lineWidth = 0.6; |
| 252 | + ctx.stroke(); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + // ── Cursor follower ring ── |
| 257 | + ctx.beginPath(); |
| 258 | + ctx.arc(fx, fy, CURSOR_RING_RADIUS, 0, Math.PI * 2); |
| 259 | + ctx.strokeStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},0.5)`; |
| 260 | + ctx.lineWidth = 1; |
| 261 | + ctx.stroke(); |
| 262 | + |
| 263 | + // Inner dot |
| 264 | + ctx.beginPath(); |
| 265 | + ctx.arc(cx, cy, 3, 0, Math.PI * 2); |
| 266 | + ctx.fillStyle = `rgba(${ACCENT.r},${ACCENT.g},${ACCENT.b},0.9)`; |
| 267 | + ctx.fill(); |
| 268 | + |
| 269 | + frameRef.current = requestAnimationFrame(draw); |
| 270 | + } |
| 271 | + |
| 272 | + frameRef.current = requestAnimationFrame(draw); |
| 273 | + |
| 274 | + return () => { |
| 275 | + cancelAnimationFrame(frameRef.current); |
| 276 | + window.removeEventListener("resize", resize); |
| 277 | + window.removeEventListener("mousemove", onMouseMove); |
| 278 | + }; |
| 279 | + }, []); |
| 280 | + |
| 281 | + return ( |
| 282 | + <canvas |
| 283 | + ref={canvasRef} |
| 284 | + aria-hidden="true" |
| 285 | + style={{ |
| 286 | + position: "fixed", |
| 287 | + inset: 0, |
| 288 | + pointerEvents: "none", |
| 289 | + zIndex: 0, |
| 290 | + }} |
| 291 | + /> |
| 292 | + ); |
| 293 | +} |
0 commit comments