Spaces:
Paused
Paused
File size: 12,532 Bytes
d8804ab 587459d d8804ab 587459d d8804ab 587459d d8804ab 587459d d8804ab 587459d d8804ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | "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%" }}
/>
);
}
|