import { useEffect, useRef, useState, type CSSProperties, type ReactNode, } from 'react' import { H5El } from './H5El' import { toggleH5Theme, useNomadroTheme } from '@/utils/h5Theme' const isH5 = process.env.TARO_ENV === 'h5' function Constellation({ active }: { active: boolean }) { const canvasRef = useRef(null) useEffect(() => { if (!isH5 || !active) return const canvas = canvasRef.current const context = canvas?.getContext('2d') if (!canvas || !context) return const media = window.matchMedia('(prefers-reduced-motion: reduce)') let frame = 0 let width = 0 let height = 0 let last = 0 let pointer = { x: -1000, y: -1000 } let points: { x: number y: number vx: number vy: number r: number color: string }[] = [] const resize = () => { width = Math.max( window.innerWidth || 0, document.documentElement.clientWidth || 0, window.visualViewport?.width || 0, ) height = Math.max( window.innerHeight || 0, document.documentElement.clientHeight || 0, window.visualViewport?.height || 0, ) const ratio = Math.min(window.devicePixelRatio || 1, 1.5) canvas.style.width = `${width}px` canvas.style.height = `${height}px` canvas.width = Math.floor(width * ratio) canvas.height = Math.floor(height * ratio) context.setTransform(ratio, 0, 0, ratio, 0, 0) points = Array.from({ length: width < 700 ? 28 : 52 }, (_, i) => ({ x: Math.random() * width, y: Math.random() * height, vx: (Math.random() - 0.5) * 0.28, vy: (Math.random() - 0.5) * 0.28, r: 0.9 + Math.random() * 1.4, color: ['78,205,196', '255,107,107', '255,230,109'][i % 3]!, })) } const draw = (time: number) => { if (document.hidden || media.matches) return const delta = Math.min((time - last) / 16.67 || 1, 2) last = time context.clearRect(0, 0, width, height) points.forEach((p, index) => { p.x = (p.x + p.vx * delta + width) % width p.y = (p.y + p.vy * delta + height) % height const near = Math.hypot(pointer.x - p.x, pointer.y - p.y) < 240 context.beginPath() context.arc(p.x, p.y, p.r, 0, Math.PI * 2) context.fillStyle = `rgba(${p.color},${near ? 0.55 : 0.28})` context.fill() if (near) { context.beginPath() context.arc(p.x, p.y, p.r + 4, 0, Math.PI * 2) context.fillStyle = `rgba(${p.color},0.1)` context.fill() } if (width > 700) { points.slice(index + 1).forEach((q) => { const distance = Math.hypot(q.x - p.x, q.y - p.y) if (distance > 140) return context.beginPath() context.moveTo(p.x, p.y) context.lineTo(q.x, q.y) context.strokeStyle = `rgba(78,205,196,${(1 - distance / 140) * 0.16})` context.lineWidth = 1.05 context.stroke() }) } }) frame = requestAnimationFrame(draw) } const restart = () => { cancelAnimationFrame(frame) last = 0 context.clearRect(0, 0, width, height) if (!document.hidden && !media.matches) { frame = requestAnimationFrame(draw) } } const move = (event: PointerEvent) => { if (event.pointerType === 'mouse') { pointer = { x: event.clientX, y: event.clientY } } } resize() restart() window.addEventListener('resize', resize) window.addEventListener('pointermove', move, { passive: true }) document.addEventListener('visibilitychange', restart) media.addEventListener('change', restart) return () => { cancelAnimationFrame(frame) window.removeEventListener('resize', resize) window.removeEventListener('pointermove', move) document.removeEventListener('visibilitychange', restart) media.removeEventListener('change', restart) context.clearRect(0, 0, width, height) } }, [active]) if (!isH5) return null return ( ) } function FloatingEmojiField() { const EMOJIS = [ { e: '☕', x: '8%', y: '22%', delay: '0s', dur: '9s', size: '1.1rem' }, { e: '🌏', x: '88%', y: '20%', delay: '1.2s', dur: '11s', size: '1.15rem' }, { e: '✨', x: '78%', y: '62%', delay: '2.4s', dur: '10s', size: '0.95rem' }, { e: '💬', x: '14%', y: '68%', delay: '0.8s', dur: '12s', size: '1rem' }, { e: '🚀', x: '52%', y: '12%', delay: '3s', dur: '10.5s', size: '0.9rem' }, ] return ( {EMOJIS.map((item) => ( {item.e} ))} ) } export function FxBelt() { const ROW = ['☕', '🌏', '💬', '✨', '📡', '🚀'] const line = [...ROW, ...ROW, ...ROW].join(' ') return ( {line} {line} ) } export function SocialExperience({ children, showEmojiField = false, }: { children: ReactNode showEmojiField?: boolean }) { const theme = useNomadroTheme() const [immersive, setImmersive] = useState(true) const rootRef = useRef(null) useEffect(() => { if (!isH5 || !immersive) return const root = rootRef.current if (!root) return let frame = 0 const move = (event: PointerEvent) => { if ( event.pointerType !== 'mouse' || window.matchMedia('(prefers-reduced-motion: reduce)').matches ) { return } cancelAnimationFrame(frame) frame = requestAnimationFrame(() => { root.style.setProperty('--pointer-x', `${event.clientX}px`) root.style.setProperty('--pointer-y', `${event.clientY}px`) const card = (event.target as Element).closest( '.social-card, .social-list-item, .social-panel, .social-member-card, .social-person-card, .connection-way', ) if (card) { const rect = card.getBoundingClientRect() card.style.setProperty('--spot-x', `${event.clientX - rect.left}px`) card.style.setProperty('--spot-y', `${event.clientY - rect.top}px`) } }) } root.addEventListener('pointermove', move, { passive: true }) return () => { cancelAnimationFrame(frame) root.removeEventListener('pointermove', move) } }, [immersive]) // 微信禁止裸
/,否则整页黑屏;H5 才挂 pointer ref if (!isH5) { return ( {children} toggleH5Theme(theme)}>{theme === 'dark' ? '☀' : '☾'} ) } return (
{showEmojiField ? : null} {children} toggleH5Theme(theme)}>{theme === 'dark' ? '☀' : '☾'} setImmersive((v) => !v)} aria-pressed={immersive} title='切换视觉动效强度' > {immersive ? '✨' : '⚡'} 动效 · {immersive ? '沉浸' : '轻量'}
) } export { FloatingEmojiField }