Spaces:
Paused
Paused
feat: complete HUBAPI overhaul (Security, i18n RTL, Dark/Light Mode, A11y & Performance fixes)
587459d Download components/AgentParticleCanvas.tsx from samirhosninet/HubAPI: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/AgentParticleCanvas.tsx
- Command line
-
hf download hf://spaces/samirhosninet/HubAPI/components/AgentParticleCanvas.tsx
-
curl -L -o AgentParticleCanvas.tsx https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/AgentParticleCanvas.tsx
12.5 kB
| "use client"; | |
| import { useEffect, useRef } from "react"; | |
| import { useLanguage } from "@/components/LanguageContext"; | |
| interface Particle { | |
| x: number; | |
| y: number; | |
| vx: number; | |
| vy: number; | |
| radius: number; | |
| baseAlpha: number; | |
| pulseOffset: number; | |
| type: "node" | "relay" | "hub"; | |
| color: string; | |
| } | |
| interface Spark { | |
| x: number; | |
| y: number; | |
| vx: number; | |
| vy: number; | |
| radius: number; | |
| color: string; | |
| alpha: number; | |
| decay: number; | |
| } | |
| interface Shockwave { | |
| x: number; | |
| y: number; | |
| radius: number; | |
| maxRadius: number; | |
| speed: number; | |
| active: boolean; | |
| } | |
| // HUBAPI Theme Design System consistent colors (Hex: #FFBE7B, #FE8FB5, #A07EFB) | |
| const COLOR_AMBER = "255, 190, 123"; // #FFBE7B (Amber / Gold) | |
| const COLOR_ROSE = "254, 143, 181"; // #FE8FB5 (Rose / Pink) | |
| const COLOR_PURPLE = "160, 126, 251"; // #A07EFB (Purple / Violet) | |
| const COLOR_CYAN = "34, 211, 238"; // #22D3EE (Cyan Plasma) | |
| const COLOR_BLUE = "59, 130, 246"; // #3B82F6 (Blue Glow) | |
| const COLOR_WHITE = "242, 237, 230"; | |
| interface AgentParticleCanvasProps { | |
| className?: string; | |
| mouseX?: number; | |
| mouseY?: number; | |
| isHovered?: boolean; | |
| } | |
| export function AgentParticleCanvas({ | |
| className, | |
| mouseX = 0, | |
| mouseY = 0, | |
| isHovered = false, | |
| }: AgentParticleCanvasProps) { | |
| const canvasRef = useRef<HTMLCanvasElement>(null); | |
| const { isAmbientMode } = useLanguage(); | |
| // High-performance refs to pass real-time interaction states into canvas loop without recreation | |
| const mouseRef = useRef({ x: mouseX, y: mouseY }); | |
| const hoverRef = useRef(isHovered); | |
| const triggerShockwaveRef = useRef(false); | |
| const prevHoveredRef = useRef(isHovered); | |
| // Sync refs instantly with fresh prop changes | |
| useEffect(() => { | |
| mouseRef.current = { x: mouseX, y: mouseY }; | |
| if (isHovered && !prevHoveredRef.current) { | |
| triggerShockwaveRef.current = true; | |
| } | |
| hoverRef.current = isHovered; | |
| prevHoveredRef.current = isHovered; | |
| }, [mouseX, mouseY, isHovered]); | |
| useEffect(() => { | |
| const canvas = canvasRef.current; | |
| if (!canvas) return; | |
| const ctx = canvas.getContext("2d"); | |
| if (!ctx) return; | |
| if (isAmbientMode) { | |
| // Pause canvas loop completely for CPU saving | |
| ctx.clearRect(0, 0, canvas.width, canvas.height); | |
| return; | |
| } | |
| let animId: number; | |
| let W = 0, H = 0; | |
| let particles: Particle[] = []; | |
| let sparks: Spark[] = []; | |
| let shockwaves: Shockwave[] = []; | |
| const resize = () => { | |
| W = canvas.offsetWidth; | |
| H = canvas.offsetHeight; | |
| canvas.width = W * window.devicePixelRatio; | |
| canvas.height = H * window.devicePixelRatio; | |
| ctx.scale(window.devicePixelRatio, window.devicePixelRatio); | |
| init(); | |
| }; | |
| const init = () => { | |
| const COUNT = Math.max(40, Math.floor((W * H) / 4500)); | |
| particles = Array.from({ length: COUNT }, () => { | |
| const roll = Math.random(); | |
| const type: Particle["type"] = roll < 0.08 ? "hub" : roll < 0.28 ? "relay" : "node"; | |
| let color = COLOR_AMBER; | |
| if (type === "hub") { | |
| color = COLOR_AMBER; | |
| } else if (type === "relay") { | |
| color = COLOR_ROSE; | |
| } else { | |
| const colorRoll = Math.random(); | |
| if (colorRoll < 0.45) { | |
| color = COLOR_AMBER; | |
| } else if (colorRoll < 0.75) { | |
| color = COLOR_PURPLE; | |
| } else { | |
| color = COLOR_ROSE; | |
| } | |
| } | |
| return { | |
| x: Math.random() * W, | |
| y: Math.random() * H, | |
| vx: (Math.random() - 0.5) * (type === "hub" ? 0.15 : 0.4), | |
| vy: (Math.random() - 0.5) * (type === "hub" ? 0.15 : 0.4), | |
| radius: type === "hub" ? 5 : type === "relay" ? 3.5 : 2.2, | |
| baseAlpha: type === "hub" ? 0.9 : type === "relay" ? 0.75 : 0.5 + Math.random() * 0.3, | |
| pulseOffset: Math.random() * Math.PI * 2, | |
| type, | |
| color, | |
| }; | |
| }); | |
| sparks = []; | |
| shockwaves = []; | |
| }; | |
| let t = 0; | |
| let sparkSpawnTimer = 0; | |
| const draw = () => { | |
| ctx.clearRect(0, 0, W, H); | |
| t += 0.014; | |
| // 1. Calculate dynamic robot center in pixels | |
| const isDesktop = W >= 768; // Matching CSS responsive breakpoint | |
| let rx = W * 0.5; | |
| let ry = H * 0.5; | |
| if (isDesktop) { | |
| // Center of right side interactive grid area | |
| rx = W * 0.5; | |
| ry = H * 0.5; | |
| // Parallax offset | |
| rx += mouseRef.current.x * 60; | |
| ry += mouseRef.current.y * 60; | |
| } else { | |
| // Centered on mobile | |
| rx = W * 0.5; | |
| ry = H * 0.4; | |
| // Float offset to match robot's animation in CSS | |
| ry += Math.sin(t * 1.5) * 12; | |
| } | |
| // 2. Spawn thruster sparks periodically (Option 2) | |
| sparkSpawnTimer++; | |
| if (sparkSpawnTimer % 4 === 0) { | |
| // Spawns sparks directly below left and right hip thruster nozzle positions | |
| const thrusterY = ry + 80; | |
| const leftThrusterX = rx - 35; | |
| const rightThrusterX = rx + 35; | |
| // Left Nozzle Spark | |
| sparks.push({ | |
| x: leftThrusterX + (Math.random() - 0.5) * 6, | |
| y: thrusterY, | |
| vx: (Math.random() - 0.5) * 1.4, | |
| vy: 2 + Math.random() * 2, | |
| radius: 1.5 + Math.random() * 1.5, | |
| color: Math.random() > 0.5 ? COLOR_CYAN : COLOR_BLUE, | |
| alpha: 0.9, | |
| decay: 0.02 + Math.random() * 0.015, | |
| }); | |
| // Right Nozzle Spark | |
| sparks.push({ | |
| x: rightThrusterX + (Math.random() - 0.5) * 6, | |
| y: thrusterY, | |
| vx: (Math.random() - 0.5) * 1.4, | |
| vy: 2 + Math.random() * 2, | |
| radius: 1.5 + Math.random() * 1.5, | |
| color: Math.random() > 0.5 ? COLOR_CYAN : COLOR_BLUE, | |
| alpha: 0.9, | |
| decay: 0.02 + Math.random() * 0.015, | |
| }); | |
| } | |
| // 3. Handle shockwaves trigger (Option 3) | |
| if (triggerShockwaveRef.current) { | |
| shockwaves.push({ | |
| x: rx, | |
| y: ry, | |
| radius: 0, | |
| maxRadius: Math.max(W, H) * 0.9, | |
| speed: 6.5, | |
| active: true, | |
| }); | |
| triggerShockwaveRef.current = false; | |
| } | |
| // 4. Draw & update expanding shockwaves | |
| for (const sw of shockwaves) { | |
| if (!sw.active) continue; | |
| sw.radius += sw.speed; | |
| if (sw.radius > sw.maxRadius) { | |
| sw.active = false; | |
| continue; | |
| } | |
| ctx.beginPath(); | |
| ctx.arc(sw.x, sw.y, sw.radius, 0, Math.PI * 2); | |
| ctx.strokeStyle = `rgba(${COLOR_AMBER}, ${0.12 * (1 - sw.radius / sw.maxRadius)})`; | |
| ctx.lineWidth = 1.8; | |
| ctx.stroke(); | |
| } | |
| shockwaves = shockwaves.filter(sw => sw.active); | |
| // 5. Draw & update thruster sparks | |
| for (const s of sparks) { | |
| s.x += s.vx; | |
| s.y += s.vy; | |
| s.alpha -= s.decay; | |
| ctx.beginPath(); | |
| ctx.arc(s.x, s.y, s.radius, 0, Math.PI * 2); | |
| ctx.fillStyle = `rgba(${s.color}, ${s.alpha})`; | |
| ctx.fill(); | |
| } | |
| sparks = sparks.filter(s => s.alpha > 0); | |
| /* ββ EDGES ββ */ | |
| for (let i = 0; i < particles.length; i++) { | |
| for (let j = i + 1; j < particles.length; j++) { | |
| const a = particles[i]; | |
| const b = particles[j]; | |
| const dx = a.x - b.x; | |
| const dy = a.y - b.y; | |
| const dist = Math.sqrt(dx * dx + dy * dy); | |
| const MAX = a.type === "hub" || b.type === "hub" ? 200 : 130; | |
| if (dist < MAX) { | |
| const t01 = 1 - dist / MAX; | |
| const isHot = a.type !== "node" || b.type !== "node"; | |
| let alpha = isHot ? t01 * 0.45 : t01 * 0.18; | |
| // Edges match the design system color of the nodes | |
| const edgeColor = isHot | |
| ? (a.type === "hub" || b.type === "hub" ? COLOR_AMBER : COLOR_ROSE) | |
| : a.color; | |
| // Highlight connections close to shockwave | |
| for (const sw of shockwaves) { | |
| if (sw.active) { | |
| const distA = Math.sqrt((a.x - sw.x)**2 + (a.y - sw.y)**2); | |
| if (Math.abs(distA - sw.radius) < 40) { | |
| alpha *= 2.2; | |
| } | |
| } | |
| } | |
| ctx.beginPath(); | |
| ctx.moveTo(a.x, a.y); | |
| ctx.lineTo(b.x, b.y); | |
| ctx.strokeStyle = `rgba(${edgeColor}, ${Math.min(alpha, 0.9)})`; | |
| ctx.lineWidth = isHot ? 0.8 : 0.4; | |
| ctx.stroke(); | |
| /* travelling data-dot on hot edges */ | |
| if (isHot && dist < MAX * 0.8) { | |
| const phase = ((t * 0.5 + i * 0.37 + j * 0.19) % 1); | |
| const px = a.x + (b.x - a.x) * phase; | |
| const py = a.y + (b.y - a.y) * phase; | |
| ctx.beginPath(); | |
| ctx.arc(px, py, 1.8, 0, Math.PI * 2); | |
| ctx.fillStyle = `rgba(${edgeColor}, 0.85)`; | |
| ctx.fill(); | |
| } | |
| } | |
| } | |
| } | |
| /* ββ NODES ββ */ | |
| for (const p of particles) { | |
| // move + wrap | |
| p.x += p.vx; if (p.x < -10) p.x = W + 10; if (p.x > W + 10) p.x = -10; | |
| p.y += p.vy; if (p.y < -10) p.y = H + 10; if (p.y > H + 10) p.y = -10; | |
| // Apply repulsion force from robot chassis position (Option 1) | |
| const dxRobot = p.x - rx; | |
| const dyRobot = p.y - ry; | |
| const distRobot = Math.sqrt(dxRobot * dxRobot + dyRobot * dyRobot); | |
| if (distRobot < 190) { | |
| const force = (190 - distRobot) / 190; | |
| // Pushes away gently (repulsion shield) | |
| p.x += (dxRobot / distRobot) * force * 1.8; | |
| p.y += (dyRobot / distRobot) * force * 1.8; | |
| // Slight orbital rotational drag to wrap the grid nodes around robot boundaries smoothly | |
| p.x += (-dyRobot / distRobot) * force * 0.7; | |
| p.y += (dxRobot / distRobot) * force * 0.7; | |
| } | |
| const pulse = 0.75 + 0.25 * Math.sin(t * 1.6 + p.pulseOffset); | |
| let r = p.radius * pulse; | |
| let alpha = p.baseAlpha * pulse; | |
| // Shockwave react (Option 3): Nodes glow and expand as shockwave sweeps through | |
| for (const sw of shockwaves) { | |
| if (sw.active) { | |
| const distSW = Math.sqrt((p.x - sw.x)**2 + (p.y - sw.y)**2); | |
| if (Math.abs(distSW - sw.radius) < 30) { | |
| const proximity = 1 - Math.abs(distSW - sw.radius) / 30; | |
| alpha = Math.min(alpha + proximity * 0.65, 1.0); | |
| r = r * (1 + proximity * 0.4); | |
| } | |
| } | |
| } | |
| if (p.type === "hub") { | |
| /* outer glow ring */ | |
| const grd = ctx.createRadialGradient(p.x, p.y, r, p.x, p.y, r + 15); | |
| grd.addColorStop(0, `rgba(${COLOR_AMBER}, 0.3)`); | |
| grd.addColorStop(1, `rgba(${COLOR_AMBER}, 0)`); | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r + 15, 0, Math.PI * 2); | |
| ctx.fillStyle = grd; | |
| ctx.fill(); | |
| /* ring stroke */ | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r + 6, 0, Math.PI * 2); | |
| ctx.strokeStyle = `rgba(${COLOR_AMBER}, ${alpha * 0.4})`; | |
| ctx.lineWidth = 0.8; | |
| ctx.stroke(); | |
| /* core */ | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r, 0, Math.PI * 2); | |
| ctx.fillStyle = `rgba(${COLOR_WHITE}, ${alpha})`; | |
| ctx.fill(); | |
| } else if (p.type === "relay") { | |
| /* outer ring */ | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r + 4, 0, Math.PI * 2); | |
| ctx.strokeStyle = `rgba(${COLOR_ROSE}, ${alpha * 0.4})`; | |
| ctx.lineWidth = 0.8; | |
| ctx.stroke(); | |
| /* core */ | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r, 0, Math.PI * 2); | |
| ctx.fillStyle = `rgba(${COLOR_ROSE}, ${alpha})`; | |
| ctx.fill(); | |
| } else { | |
| /* plain node β colored using design system colors */ | |
| ctx.beginPath(); | |
| ctx.arc(p.x, p.y, r, 0, Math.PI * 2); | |
| ctx.fillStyle = `rgba(${p.color}, ${alpha})`; | |
| ctx.fill(); | |
| } | |
| } | |
| animId = requestAnimationFrame(draw); | |
| }; | |
| resize(); | |
| draw(); | |
| const ro = new ResizeObserver(resize); | |
| ro.observe(canvas); | |
| return () => { | |
| if (animId) cancelAnimationFrame(animId); | |
| ro.disconnect(); | |
| }; | |
| }, [isAmbientMode]); | |
| return ( | |
| <canvas | |
| ref={canvasRef} | |
| className={className} | |
| style={{ display: "block", width: "100%", height: "100%" }} | |
| /> | |
| ); | |
| } | |