|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +interface Particle { |
| 4 | + x: number; |
| 5 | + y: number; |
| 6 | + vx: number; |
| 7 | + vy: number; |
| 8 | + radius: number; |
| 9 | + color: string; |
| 10 | + alpha: number; |
| 11 | +} |
| 12 | + |
| 13 | +export function ParticleBackground() { |
| 14 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const canvas = canvasRef.current; |
| 18 | + if (!canvas) return; |
| 19 | + |
| 20 | + const ctx = canvas.getContext("2d"); |
| 21 | + if (!ctx) return; |
| 22 | + |
| 23 | + // Configurar tamaño del canvas |
| 24 | + const resizeCanvas = () => { |
| 25 | + canvas.width = window.innerWidth; |
| 26 | + canvas.height = window.innerHeight; |
| 27 | + }; |
| 28 | + resizeCanvas(); |
| 29 | + window.addEventListener("resize", resizeCanvas); |
| 30 | + |
| 31 | + // Colores temáticos (física = cyan, química = pink/purple) |
| 32 | + const colors = [ |
| 33 | + "rgba(6, 182, 212, ", // cyan-500 (física) |
| 34 | + "rgba(59, 130, 246, ", // blue-500 (física) |
| 35 | + "rgba(236, 72, 153, ", // pink-500 (química) |
| 36 | + "rgba(168, 85, 247, ", // purple-500 (química) |
| 37 | + ]; |
| 38 | + |
| 39 | + // Crear partículas |
| 40 | + const particleCount = 60; |
| 41 | + const particles: Particle[] = []; |
| 42 | + |
| 43 | + for (let i = 0; i < particleCount; i++) { |
| 44 | + particles.push({ |
| 45 | + x: Math.random() * canvas.width, |
| 46 | + y: Math.random() * canvas.height, |
| 47 | + vx: (Math.random() - 0.5) * 0.5, |
| 48 | + vy: (Math.random() - 0.5) * 0.5, |
| 49 | + radius: Math.random() * 2 + 1, |
| 50 | + color: colors[Math.floor(Math.random() * colors.length)], |
| 51 | + alpha: Math.random() * 0.5 + 0.3, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Función de animación |
| 56 | + const animate = () => { |
| 57 | + ctx.clearRect(0, 0, canvas.width, canvas.height); // Limpiar completamente en vez de trail |
| 58 | + |
| 59 | + // Actualizar y dibujar partículas |
| 60 | + particles.forEach((particle) => { |
| 61 | + // Mover partícula |
| 62 | + particle.x += particle.vx; |
| 63 | + particle.y += particle.vy; |
| 64 | + |
| 65 | + // Rebotar en los bordes |
| 66 | + if (particle.x < 0 || particle.x > canvas.width) particle.vx *= -1; |
| 67 | + if (particle.y < 0 || particle.y > canvas.height) particle.vy *= -1; |
| 68 | + |
| 69 | + // Dibujar partícula |
| 70 | + ctx.beginPath(); |
| 71 | + ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); |
| 72 | + ctx.fillStyle = particle.color + particle.alpha + ")"; |
| 73 | + ctx.fill(); |
| 74 | + |
| 75 | + // Efecto de glow |
| 76 | + ctx.shadowBlur = 10; |
| 77 | + ctx.shadowColor = particle.color + "0.8)"; |
| 78 | + }); |
| 79 | + |
| 80 | + // Conectar partículas cercanas |
| 81 | + for (let i = 0; i < particles.length; i++) { |
| 82 | + for (let j = i + 1; j < particles.length; j++) { |
| 83 | + const dx = particles[i].x - particles[j].x; |
| 84 | + const dy = particles[i].y - particles[j].y; |
| 85 | + const distance = Math.sqrt(dx * dx + dy * dy); |
| 86 | + |
| 87 | + if (distance < 120) { |
| 88 | + ctx.beginPath(); |
| 89 | + ctx.moveTo(particles[i].x, particles[i].y); |
| 90 | + ctx.lineTo(particles[j].x, particles[j].y); |
| 91 | + ctx.strokeStyle = `rgba(100, 200, 255, ${ |
| 92 | + 0.15 * (1 - distance / 120) |
| 93 | + })`; |
| 94 | + ctx.lineWidth = 0.5; |
| 95 | + ctx.stroke(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + ctx.shadowBlur = 0; |
| 101 | + requestAnimationFrame(animate); |
| 102 | + }; |
| 103 | + |
| 104 | + animate(); |
| 105 | + |
| 106 | + return () => { |
| 107 | + window.removeEventListener("resize", resizeCanvas); |
| 108 | + }; |
| 109 | + }, []); |
| 110 | + |
| 111 | + return ( |
| 112 | + <canvas |
| 113 | + ref={canvasRef} |
| 114 | + className="fixed inset-0 z-0 pointer-events-none" |
| 115 | + style={{ background: "transparent" }} |
| 116 | + /> |
| 117 | + ); |
| 118 | +} |
0 commit comments