"use client"; import { useEffect, useRef } from "react"; /** Ambient particle field — same feel as 旅居社群 homepage. */ export default function ParticleCanvas() { const canvasRef = useRef(null); useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext("2d", { alpha: true }); if (!ctx) return; if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) { canvas.style.display = "none"; return; } let animId = 0; let running = false; let started = false; let resizeTimer = 0; let particles: { x: number; y: number; size: number; speedX: number; speedY: number; opacity: number; hue: number; }[] = []; const resize = () => { const dpr = Math.min(window.devicePixelRatio || 1, 1.5); const w = window.innerWidth; const h = window.innerHeight; canvas.width = Math.floor(w * dpr); canvas.height = Math.floor(h * dpr); canvas.style.width = `${w}px`; canvas.style.height = `${h}px`; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); }; const create = () => { const w = window.innerWidth; const dense = document.querySelector(".social-fx:not(.social-home)") != null; const count = Math.min( Math.floor(w / (dense ? 14 : 40)), dense ? 90 : 28, ); particles = Array.from({ length: count }, () => ({ x: Math.random() * w, y: Math.random() * window.innerHeight, size: Math.random() * (dense ? 3.2 : 2) + (dense ? 1 : 0.5), speedX: (Math.random() - 0.5) * (dense ? 0.45 : 0.22), speedY: (Math.random() - 0.5) * (dense ? 0.45 : 0.22), opacity: Math.random() * (dense ? 0.55 : 0.35) + (dense ? 0.18 : 0.08), hue: [0, 45, 170][Math.floor(Math.random() * 3)], })); }; const draw = () => { if (!running) return; const w = window.innerWidth; const h = window.innerHeight; ctx.clearRect(0, 0, w, h); for (const p of particles) { p.x += p.speedX; p.y += p.speedY; if (p.x < 0) p.x = w; if (p.x > w) p.x = 0; if (p.y < 0) p.y = h; if (p.y > h) p.y = 0; ctx.beginPath(); ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2); ctx.fillStyle = `hsla(${p.hue}, 70%, 60%, ${p.opacity})`; ctx.fill(); } animId = requestAnimationFrame(draw); }; const start = () => { if (started) return; started = true; resize(); create(); running = document.visibilityState === "visible"; if (running) draw(); }; const onResize = () => { clearTimeout(resizeTimer); resizeTimer = window.setTimeout(() => { resize(); create(); }, 200); }; const onVis = () => { running = document.visibilityState === "visible" && started; if (running) draw(); else cancelAnimationFrame(animId); }; const ric = window.requestIdleCallback ? window.requestIdleCallback(() => start(), { timeout: 600 }) : 0; const fallback = window.setTimeout(start, 120); window.addEventListener("resize", onResize, { passive: true }); document.addEventListener("visibilitychange", onVis); return () => { running = false; cancelAnimationFrame(animId); clearTimeout(fallback); clearTimeout(resizeTimer); if (ric && window.cancelIdleCallback) window.cancelIdleCallback(ric); window.removeEventListener("resize", onResize); document.removeEventListener("visibilitychange", onVis); }; }, []); return