|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import './ParticleBackground.css'; |
| 3 | + |
| 4 | +interface Particle { |
| 5 | + x: number; |
| 6 | + y: number; |
| 7 | + size: number; |
| 8 | + speedX: number; |
| 9 | + speedY: number; |
| 10 | + opacity: number; |
| 11 | +} |
| 12 | + |
| 13 | +const ParticleBackground: React.FC = () => { |
| 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 | + const resizeCanvas = () => { |
| 24 | + canvas.width = window.innerWidth; |
| 25 | + canvas.height = window.innerHeight; |
| 26 | + }; |
| 27 | + |
| 28 | + resizeCanvas(); |
| 29 | + window.addEventListener('resize', resizeCanvas); |
| 30 | + |
| 31 | + const particles: Particle[] = []; |
| 32 | + const particleCount = 50; |
| 33 | + |
| 34 | + for (let i = 0; i < particleCount; i++) { |
| 35 | + particles.push({ |
| 36 | + x: Math.random() * canvas.width, |
| 37 | + y: Math.random() * canvas.height, |
| 38 | + size: Math.random() * 2 + 1, |
| 39 | + speedX: (Math.random() - 0.5) * 0.5, |
| 40 | + speedY: (Math.random() - 0.5) * 0.5, |
| 41 | + opacity: Math.random() * 0.5 + 0.2, |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + const animate = () => { |
| 46 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 47 | + |
| 48 | + particles.forEach((particle, index) => { |
| 49 | + particle.x += particle.speedX; |
| 50 | + particle.y += particle.speedY; |
| 51 | + |
| 52 | + if (particle.x < 0 || particle.x > canvas.width) particle.speedX *= -1; |
| 53 | + if (particle.y < 0 || particle.y > canvas.height) particle.speedY *= -1; |
| 54 | + |
| 55 | + ctx.beginPath(); |
| 56 | + ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2); |
| 57 | + ctx.fillStyle = `rgba(142, 192, 124, ${particle.opacity})`; |
| 58 | + ctx.fill(); |
| 59 | + |
| 60 | + for (let j = index + 1; j < particles.length; j++) { |
| 61 | + const dx = particles[j].x - particle.x; |
| 62 | + const dy = particles[j].y - particle.y; |
| 63 | + const distance = Math.sqrt(dx * dx + dy * dy); |
| 64 | + |
| 65 | + if (distance < 120) { |
| 66 | + ctx.beginPath(); |
| 67 | + ctx.strokeStyle = `rgba(142, 192, 124, ${0.15 * (1 - distance / 120)})`; |
| 68 | + ctx.lineWidth = 1; |
| 69 | + ctx.moveTo(particle.x, particle.y); |
| 70 | + ctx.lineTo(particles[j].x, particles[j].y); |
| 71 | + ctx.stroke(); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + requestAnimationFrame(animate); |
| 77 | + }; |
| 78 | + |
| 79 | + animate(); |
| 80 | + |
| 81 | + return () => { |
| 82 | + window.removeEventListener('resize', resizeCanvas); |
| 83 | + }; |
| 84 | + }, []); |
| 85 | + |
| 86 | + return <canvas ref={canvasRef} className="particle-background" />; |
| 87 | +}; |
| 88 | + |
| 89 | +export default ParticleBackground; |
0 commit comments