import React, { useEffect, useRef } from 'react'; /** * 深海八爪 · 幽光 —— 登录页背景动画 * * 七层景深(由后到前): * 1. 景深渐变背景(浅→深,带 vignette) * 2. 顶部光柱(brand-primary,缓 breathing) * 3. 中层浮游粒子(brand-primary 微辉光) * 4. 触手剪影 + 吸盘 Mexican wave 发光(右下角) * 5. 上升气泡(白色,正弦摆动) * 6. 前景闪烁粒子(brand-gradient-to,快移视差) * 7. 生命脉冲(每 6-10s 一次环形扩散) * * 立体感来源: * - 鼠标视差(各层不同步移动,后层慢、前层快) * - 触手 sucker 波浪式呼吸(像 Mexican wave) * - 偶发生命脉冲(节奏惊喜) * * 主题适配: * - 所有发光层用 --brand-primary / --brand-gradient-to(CSS 变量实时读取) * - 暗色模式背景换为午夜蓝→墨黑 * - prefers-reduced-motion:关闭脉冲 / breathing / 视差,只留缓慢气泡 * * 性能: * - 单 canvas + 单 rAF 循环 * - 设备自适应粒子数(移动端减半) * - 主题色每 500ms 读一次(不是每帧) */ // ─── 工具函数 ───────────────────────────────────────────────── const hexToRgb = (hex) => { if (!hex) return { r: 99, g: 102, b: 241 }; // brand-primary 兜底 let h = hex.replace('#', '').trim(); if (h.length === 3) h = h .split('') .map((c) => c + c) .join(''); if (h.length === 8) h = h.slice(0, 6); // 忽略 alpha if (!/^[0-9a-fA-F]{6}$/.test(h)) return { r: 99, g: 102, b: 241 }; const num = parseInt(h, 16); return { r: (num >> 16) & 255, g: (num >> 8) & 255, b: num & 255 }; }; const rgba = (c, a) => `rgba(${c.r}, ${c.g}, ${c.b}, ${a})`; const lerp = (a, b, t) => a + (b - a) * t; const easeOutCubic = (t) => 1 - Math.pow(1 - t, 3); // 三阶 Bezier 求 t 处坐标(给触手沿曲线放置吸盘用) const bezierPoint = (p0, p1, p2, p3, t) => { const mt = 1 - t; return ( mt * mt * mt * p0 + 3 * mt * mt * t * p1 + 3 * mt * t * t * p2 + t * t * t * p3 ); }; // ─── 粒子类 ────────────────────────────────────────────────── class Plankton { constructor(width, height) { this.w = width; this.h = height; this.reset(true); } reset(randomY = false) { this.x = Math.random() * this.w; this.y = randomY ? Math.random() * this.h : Math.random() < 0.5 ? -10 : this.h + 10; this.size = Math.random() * 1.5 + 0.5; this.vx = (Math.random() - 0.5) * 0.2; this.vy = (Math.random() - 0.5) * 0.15; this.alpha = Math.random() * 0.35 + 0.2; this.phase = Math.random() * Math.PI * 2; } update() { this.x += this.vx; this.y += this.vy + Math.sin(this.phase) * 0.15; this.phase += 0.015; if ( this.x < -20 || this.x > this.w + 20 || this.y < -20 || this.y > this.h + 20 ) this.reset(); } resize(w, h) { this.w = w; this.h = h; } draw(ctx, ox, oy, color) { const x = this.x + ox; const y = this.y + oy; const r = this.size * 4; const grad = ctx.createRadialGradient(x, y, 0, x, y, r); grad.addColorStop(0, rgba(color, this.alpha)); grad.addColorStop(1, rgba(color, 0)); ctx.fillStyle = grad; ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha * 0.5})`; ctx.beginPath(); ctx.arc(x, y, this.size, 0, Math.PI * 2); ctx.fill(); } } class Sparkle extends Plankton { reset(randomY = false) { super.reset(randomY); this.size = Math.random() * 2.2 + 0.8; this.vx *= 2.5; this.vy *= 2.5; this.alpha = Math.random() * 0.5 + 0.35; } } class Bubble { constructor(width, height) { this.w = width; this.h = height; this.reset(true); } reset(randomY = false) { this.x = Math.random() * this.w; this.y = randomY ? Math.random() * this.h : this.h + 30; this.size = Math.random() * 8 + 3; this.vy = -(Math.random() * 0.7 + 0.3); this.swayPhase = Math.random() * Math.PI * 2; this.alpha = Math.random() * 0.4 + 0.3; } update() { this.y += this.vy; this.x += Math.sin(this.swayPhase) * 0.5; this.swayPhase += 0.03; if (this.y < -30) this.reset(false); } resize(w, h) { this.w = w; this.h = h; } draw(ctx, ox, oy) { const x = this.x + ox; const y = this.y + oy; ctx.strokeStyle = `rgba(255, 255, 255, ${this.alpha * 0.6})`; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(x, y, this.size, 0, Math.PI * 2); ctx.stroke(); ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha * 0.5})`; ctx.beginPath(); ctx.arc( x - this.size * 0.3, y - this.size * 0.3, this.size * 0.28, 0, Math.PI * 2, ); ctx.fill(); } } class LightPillar { constructor(width, index, total) { this.idx = index; this.total = total; // 这些参数只在构造时随机一次,resize 时不能重抽, // 否则窗口拖拽时光柱会"跳"位置和宽度。 this.offset = (Math.random() - 0.5) * 80; this.width = 120 + Math.random() * 100; this.angle = -0.18 + Math.random() * 0.36; this.intensity = 0.05 + Math.random() * 0.05; this.breathPhase = Math.random() * Math.PI * 2; this.resize(width); } resize(width) { // 只更新跟 width 相关的 baseX 基准,固定 offset 保持不变 this.baseX = ((this.idx + 0.5) / this.total) * width + this.offset; } update(breathOn) { if (breathOn) this.breathPhase += 0.003; } draw(ctx, ox, height, color) { const breath = 0.6 + Math.sin(this.breathPhase) * 0.4; const x = this.baseX + ox * 0.3; ctx.save(); ctx.translate(x, 0); ctx.rotate(this.angle); const grad = ctx.createLinearGradient(0, 0, 0, height * 1.3); grad.addColorStop(0, rgba(color, this.intensity * breath)); grad.addColorStop(1, rgba(color, 0)); ctx.fillStyle = grad; ctx.fillRect(-this.width / 2, -50, this.width, height * 1.4); ctx.restore(); } } class Tentacle { constructor(width, height, suckerCount) { this.w = width; this.h = height; this.suckerCount = suckerCount; this.suckerPhases = Array.from({ length: suckerCount }, (_, i) => i * 0.6); this.swayPhase = 0; } resize(w, h) { this.w = w; this.h = h; } update() { this.swayPhase += 0.004; for (let i = 0; i < this.suckerCount; i++) this.suckerPhases[i] += 0.045; } draw(ctx, ox, oy, color, isDark) { const baseX = this.w; const baseY = this.h; const length = Math.min(this.w, this.h) * 0.7; const sway = Math.sin(this.swayPhase) * 40; // tip 跟随鼠标视差;base 锚定在角落 const endX = baseX - length * 0.5 + sway + ox; const endY = baseY - length * 0.7 + oy; const c2X = baseX - length * 0.4 + sway * 1.4 + ox * 0.7; const c2Y = baseY - length * 0.55 + oy * 0.7; const c1X = baseX - length * 0.1 + ox * 0.3; const c1Y = baseY - length * 0.3 + oy * 0.3; // 分段绘制以做出尖端渐细的效果 const segs = 32; const taperFrom = 38; const taperTo = 8; ctx.strokeStyle = isDark ? 'rgba(0, 5, 15, 0.55)' : 'rgba(0, 25, 40, 0.45)'; ctx.lineCap = 'round'; let prevX = baseX; let prevY = baseY; for (let i = 1; i <= segs; i++) { const t = i / segs; const x = bezierPoint(baseX, c1X, c2X, endX, t); const y = bezierPoint(baseY, c1Y, c2Y, endY, t); ctx.lineWidth = lerp(taperFrom, taperTo, t); ctx.beginPath(); ctx.moveTo(prevX, prevY); ctx.lineTo(x, y); ctx.stroke(); prevX = x; prevY = y; } // 吸盘:沿曲线均匀分布,各自相位错开形成 Mexican wave for (let i = 0; i < this.suckerCount; i++) { const t = 0.18 + (i / (this.suckerCount - 1)) * 0.78; const x = bezierPoint(baseX, c1X, c2X, endX, t); const y = bezierPoint(baseY, c1Y, c2Y, endY, t); const breath = 0.5 + Math.sin(this.suckerPhases[i]) * 0.5; const glow = 5 + breath * 8; // 外层光晕 const grad = ctx.createRadialGradient(x, y, 0, x, y, glow * 3); grad.addColorStop(0, rgba(color, 0.6 + breath * 0.3)); grad.addColorStop(0.4, rgba(color, 0.3 + breath * 0.2)); grad.addColorStop(1, rgba(color, 0)); ctx.fillStyle = grad; ctx.beginPath(); ctx.arc(x, y, glow * 3, 0, Math.PI * 2); ctx.fill(); // 内核亮点 ctx.fillStyle = `rgba(255, 255, 255, ${0.7 + breath * 0.3})`; ctx.beginPath(); ctx.arc(x, y, glow * 0.45, 0, Math.PI * 2); ctx.fill(); } } } class LifePulse { constructor(width, height) { this.w = width; this.h = height; this.active = false; this.startT = 0; this.nextT = performance.now() + 4000 + Math.random() * 3000; this.duration = 3200; } resize(w, h) { this.w = w; this.h = h; } update(now, enabled) { if (!enabled) return; if (!this.active && now >= this.nextT) { // 从屏幕外随机一侧触发 const side = Math.floor(Math.random() * 4); const m = 100; if (side === 0) { this.ox = Math.random() * this.w; this.oy = -m; } else if (side === 1) { this.ox = this.w + m; this.oy = Math.random() * this.h; } else if (side === 2) { this.ox = Math.random() * this.w; this.oy = this.h + m; } else { this.ox = -m; this.oy = Math.random() * this.h; } this.active = true; this.startT = now; this.maxR = Math.hypot(this.w, this.h) * 0.7; } if (this.active && now - this.startT >= this.duration) { this.active = false; this.nextT = now + 6000 + Math.random() * 4000; } } draw(ctx, now, color) { if (!this.active) return; const p = (now - this.startT) / this.duration; const radius = this.maxR * easeOutCubic(p); const alpha = p < 0.5 ? p * 2 * 0.35 : (1 - p) * 2 * 0.35; const inner = Math.max(0, radius - 50); const outer = radius + 50; const grad = ctx.createRadialGradient( this.ox, this.oy, inner, this.ox, this.oy, outer, ); grad.addColorStop(0, rgba(color, 0)); grad.addColorStop(0.5, rgba(color, alpha)); grad.addColorStop(1, rgba(color, 0)); ctx.fillStyle = grad; ctx.fillRect(0, 0, this.w, this.h); } } // ─── 主题 / 模式读取 ──────────────────────────────────────── const readTheme = () => { const cs = getComputedStyle(document.documentElement); const get = (k, fallback) => { const v = cs.getPropertyValue(k).trim(); return v || fallback; }; return { primary: hexToRgb(get('--brand-primary', '#1668dc')), accent: hexToRgb(get('--brand-gradient-to', '#0958d9')), }; }; const detectDark = () => { if (document.documentElement.classList.contains('dark')) return true; if (document.body && document.body.getAttribute('theme-mode') === 'dark') return true; // ThemeContext 在浅色模式会移除上面两个标记,此时必须先看用户显式选择, // 否则"强制浅色 + 系统深色"会被 prefers-color-scheme 兜底误判成暗色 try { const stored = localStorage.getItem('theme-mode'); if (stored === 'dark') return true; if (stored === 'light') return false; } catch { // localStorage 不可用(隐私模式等)时继续走系统偏好 } return ( window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ); }; // ─── 组件 ───────────────────────────────────────────────── const OceanBackground = () => { const canvasRef = useRef(null); useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); let frameId = 0; let themeTickId = 0; let width = window.innerWidth; let height = window.innerHeight; const dpr = Math.min(window.devicePixelRatio || 1, 1.5); // 上限避免移动端开销 // 设备 / 偏好检测 const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent, ); const reducedMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; // 元素数量自适应。宽矩形光柱左右边缘无法自然羽化,会形成明显条带,统一关闭。 const COUNT = reducedMotion ? { plankton: 25, sparkle: 0, bubble: 6, pillar: 0 } : isMobile ? { plankton: 40, sparkle: 12, bubble: 10, pillar: 0 } : { plankton: 80, sparkle: 28, bubble: 16, pillar: 0 }; // 主题色 + 暗色,500ms 一更新(getComputedStyle 不算贵,但没必要每帧) let theme = readTheme(); let isDark = detectDark(); themeTickId = setInterval(() => { theme = readTheme(); isDark = detectDark(); }, 500); // 鼠标视差状态 let mouseTargetX = 0; let mouseTargetY = 0; let mouseX = 0; let mouseY = 0; const onMouseMove = (e) => { if (reducedMotion) return; mouseTargetX = (e.clientX / width - 0.5) * 2; mouseTargetY = (e.clientY / height - 0.5) * 2; }; // 实例化 const plankton = Array.from( { length: COUNT.plankton }, () => new Plankton(width, height), ); const sparkles = Array.from( { length: COUNT.sparkle }, () => new Sparkle(width, height), ); const bubbles = Array.from( { length: COUNT.bubble }, () => new Bubble(width, height), ); const pillars = Array.from( { length: COUNT.pillar }, (_, i) => new LightPillar(width, i, COUNT.pillar || 1), ); const tentacle = reducedMotion ? null : new Tentacle(width, height, isMobile ? 6 : 8); const pulse = new LifePulse(width, height); // ─ resize ───────────────────────── const resize = () => { width = window.innerWidth; height = window.innerHeight; canvas.width = width * dpr; canvas.height = height * dpr; canvas.style.width = width + 'px'; canvas.style.height = height + 'px'; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); plankton.forEach((p) => p.resize(width, height)); sparkles.forEach((p) => p.resize(width, height)); bubbles.forEach((b) => b.resize(width, height)); pillars.forEach((p) => p.resize(width)); if (tentacle) tentacle.resize(width, height); pulse.resize(width, height); }; // ─ 景深渐变背景 ─────────────────────── const drawBackground = () => { // 主渐变:浅色 = 水青→深 teal,暗色 = 午夜蓝→墨黑 const grad = ctx.createLinearGradient(0, 0, 0, height); if (isDark) { grad.addColorStop(0, '#0d1f33'); grad.addColorStop(0.5, '#0a1525'); grad.addColorStop(1, '#02060d'); } else { grad.addColorStop(0, '#b8e0e8'); grad.addColorStop(0.45, '#4a8fa8'); grad.addColorStop(1, '#0d3854'); } ctx.fillStyle = grad; ctx.fillRect(0, 0, width, height); // 四角 vignette,加强深度感 const cx = width / 2; const cy = height / 2; const r0 = Math.min(width, height) * 0.35; const r1 = Math.max(width, height) * 0.75; const vig = ctx.createRadialGradient(cx, cy, r0, cx, cy, r1); vig.addColorStop(0, 'rgba(0, 0, 0, 0)'); vig.addColorStop( 1, isDark ? 'rgba(0, 0, 0, 0.55)' : 'rgba(0, 10, 25, 0.35)', ); ctx.fillStyle = vig; ctx.fillRect(0, 0, width, height); }; // ─ 主循环 ──────────────────────────── const animate = () => { const now = performance.now(); // 鼠标视差 lerp(reducedMotion 时跳过) if (!reducedMotion) { mouseX = lerp(mouseX, mouseTargetX, 0.05); mouseY = lerp(mouseY, mouseTargetY, 0.05); } drawBackground(); // Layer 2: 光柱(后景,视差最少) pillars.forEach((p) => { p.update(!reducedMotion); p.draw(ctx, mouseX * 10, height, theme.primary); }); // Layer 3: 中层浮游(视差中等) plankton.forEach((p) => { p.update(); p.draw(ctx, mouseX * 18, mouseY * 12, theme.primary); }); // Layer 4: 触手(视差中前) if (tentacle) { tentacle.update(); tentacle.draw(ctx, mouseX * 25, mouseY * 18, theme.primary, isDark); } // Layer 5: 气泡(视差中前) bubbles.forEach((b) => { b.update(); b.draw(ctx, mouseX * 30, mouseY * 20); }); // Layer 6: 前景闪烁粒子(视差最大,给"动感") sparkles.forEach((s) => { s.update(); s.draw(ctx, mouseX * 40, mouseY * 28, theme.accent); }); // Layer 7: 生命脉冲(reducedMotion 时不启用) pulse.update(now, !reducedMotion); pulse.draw(ctx, now, theme.primary); frameId = requestAnimationFrame(animate); }; window.addEventListener('resize', resize); window.addEventListener('mousemove', onMouseMove); resize(); animate(); return () => { window.removeEventListener('resize', resize); window.removeEventListener('mousemove', onMouseMove); cancelAnimationFrame(frameId); clearInterval(themeTickId); }; }, []); return (