class ParticleSystem {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.particles = [];
this.mouse = { x: 0, y: 0 };
this.setupCanvas();
this.bindEvents();
this.animate();
}
setupCanvas() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
bindEvents() {
this.canvas.addEventListener('mousemove', (e) => {
this.mouse.x = e.clientX;
this.mouse.y = e.clientY;
this.createParticle();
});
}
createParticle() {
const particle = {
x: this.mouse.x,
y: this.mouse.y,
vx: (Math.random() - 0.5) * 2,
vy: (Math.random() - 0.5) * 2,
size: Math.random() * 3 + 1,
color: `hsl(${Math.random() * 360}, 70%, 50%)`,
life: 1
};
this.particles.push(particle);
}
updateParticles() {
this.particles = this.particles.filter(particle => {
particle.x += particle.vx;
particle.y += particle.vy;
particle.life -= 0.01;
return particle.life > 0;
});
}
drawParticles() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.particles.forEach(particle => {
this.ctx.globalAlpha = particle.life;
this.ctx.fillStyle = particle.color;
this.ctx.beginPath();
this.ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
this.ctx.fill();
});
}
animate() {
this.updateParticles();
this.drawParticles();
requestAnimationFrame(() => this.animate());
}
}