import { useEffect, useRef } from 'react'; import { useTheme } from '../context/ThemeContext'; /** * 流动透明球体背景(科技感氛围层)。 * 参考同级看板项目的 StarBackground 实现,改为透明底:不覆盖 mec 自身的 bodyBgImage 渐变, * 只在其上叠加漂浮光球 / 星点 / 深空云雾,固定定位 zIndex 0,内容层以 zIndex 1 覆盖其上。 */ interface Orb { x: number; y: number; r: number; vx: number; vy: number; color: string; glowAlpha: number; pulsePhase: number; pulseSpeed: number; } export default function StarBackground() { const { theme } = useTheme(); const canvasRef = useRef(null); useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); if (!ctx) return; const resize = () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }; resize(); window.addEventListener('resize', resize); // 星点 const stars = Array.from({ length: 160 }, () => ({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, r: Math.random() * 1.2 + 0.2, opacity: Math.random() * 0.6 + 0.2, speed: Math.random() * 0.015 + 0.005, phase: Math.random() * Math.PI * 2, })); // 深空云雾 const nebula = Array.from({ length: 6 }, (_, i) => ({ x: (i / 6) * canvas.width + Math.random() * 200 - 100, y: Math.random() * canvas.height, r: Math.random() * 180 + 120, color: ['rgba(99,102,241,', 'rgba(56,189,248,', 'rgba(40,0,120,', 'rgba(0,80,160,', 'rgba(0,229,153,', 'rgba(120,100,255,'][i], })); // 流动透明球体:大球 + 小球混合 const darkOrbColors = [ 'rgba(220,240,255,', 'rgba(200,230,255,', 'rgba(255,255,255,', 'rgba(230,240,255,', 'rgba(56,189,248,', 'rgba(129,140,248,', 'rgba(52,211,153,', 'rgba(0,212,255,', ]; const lightOrbColors = [ 'rgba(160,185,230,', 'rgba(180,200,240,', 'rgba(200,215,245,', 'rgba(140,170,220,', 'rgba(100,140,210,', 'rgba(129,140,248,', 'rgba(120,160,220,', 'rgba(190,210,245,', ]; const orbColors = theme === 'dark' ? darkOrbColors : lightOrbColors; const orbGlowBase = theme === 'dark' ? 0.025 : 0.07; const orbs: Orb[] = Array.from({ length: 110 }, (_, i) => ({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, r: i < 40 ? (Math.random() * 30 + 35) : (Math.random() * 12 + 10), vx: (Math.random() - 0.5) * 2.2, vy: (Math.random() - 0.5) * 1.6, color: orbColors[i % orbColors.length], glowAlpha: Math.random() * orbGlowBase + orbGlowBase * 0.4, pulsePhase: Math.random() * Math.PI * 2, pulseSpeed: Math.random() * 0.012 + 0.006, })); let frame = 0; let animId: number; const draw = () => { frame++; const W = canvas.width; const H = canvas.height; // 透明底:只清空,不填充不透明背景,保留 mec 自身 bodyBgImage 渐变 ctx.clearRect(0, 0, W, H); // 星云雾 nebula.forEach(n => { const g = ctx.createRadialGradient(n.x, n.y, 0, n.x, n.y, n.r); const nebulaAlpha = theme === 'dark' ? '0.05' : '0.035'; g.addColorStop(0, n.color + nebulaAlpha + ')'); g.addColorStop(1, n.color + '0)'); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2); ctx.fill(); }); // 流动球体 orbs.forEach(orb => { orb.x += orb.vx; orb.y += orb.vy; if (orb.x < -orb.r) orb.x = W + orb.r; if (orb.x > W + orb.r) orb.x = -orb.r; if (orb.y < -orb.r) orb.y = H + orb.r; if (orb.y > H + orb.r) orb.y = -orb.r; const pulse = 0.7 + 0.3 * Math.sin(frame * orb.pulseSpeed + orb.pulsePhase); const alpha = orb.glowAlpha * pulse; // 外层光晕 const outerR = orb.r * 1.5; const outerGrad = ctx.createRadialGradient(orb.x, orb.y, 0, orb.x, orb.y, outerR); outerGrad.addColorStop(0, orb.color + (alpha * 0.9).toFixed(3) + ')'); outerGrad.addColorStop(0.5, orb.color + (alpha * 0.3).toFixed(3) + ')'); outerGrad.addColorStop(1, orb.color + '0)'); ctx.fillStyle = outerGrad; ctx.beginPath(); ctx.arc(orb.x, orb.y, outerR, 0, Math.PI * 2); ctx.fill(); // 内层球体 const innerGrad = ctx.createRadialGradient( orb.x - orb.r * 0.2, orb.y - orb.r * 0.2, 0, orb.x, orb.y, orb.r, ); innerGrad.addColorStop(0, orb.color + (alpha * 2.0).toFixed(3) + ')'); innerGrad.addColorStop(0.6, orb.color + (alpha * 0.8).toFixed(3) + ')'); innerGrad.addColorStop(1, orb.color + '0)'); ctx.fillStyle = innerGrad; ctx.beginPath(); ctx.arc(orb.x, orb.y, orb.r, 0, Math.PI * 2); ctx.fill(); // 高光点 const hlR = orb.r * 0.25; const hlGrad = ctx.createRadialGradient( orb.x - orb.r * 0.25, orb.y - orb.r * 0.25, 0, orb.x - orb.r * 0.25, orb.y - orb.r * 0.25, hlR, ); hlGrad.addColorStop(0, 'rgba(255,255,255,' + (alpha * 4).toFixed(3) + ')'); hlGrad.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = hlGrad; ctx.beginPath(); ctx.arc(orb.x - orb.r * 0.25, orb.y - orb.r * 0.25, hlR, 0, Math.PI * 2); ctx.fill(); }); // 星点闪烁 stars.forEach(s => { const twinkle = 0.3 + 0.7 * (0.5 + 0.5 * Math.sin(frame * s.speed + s.phase)); ctx.beginPath(); ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2); ctx.fillStyle = theme === 'dark' ? `rgba(180,220,255,${s.opacity * twinkle})` : `rgba(100,140,200,${s.opacity * twinkle * 0.5})`; ctx.fill(); if (s.r > 1.0) { const halo = ctx.createRadialGradient(s.x, s.y, 0, s.x, s.y, s.r * 4); halo.addColorStop(0, theme === 'dark' ? `rgba(100,200,255,${0.2 * twinkle})` : `rgba(80,130,220,${0.08 * twinkle})`); halo.addColorStop(1, 'rgba(0,0,0,0)'); ctx.fillStyle = halo; ctx.beginPath(); ctx.arc(s.x, s.y, s.r * 4, 0, Math.PI * 2); ctx.fill(); } }); animId = requestAnimationFrame(draw); }; draw(); return () => { cancelAnimationFrame(animId); window.removeEventListener('resize', resize); }; }, [theme]); return ( ); }