Changed around line 1
+ class Particle {
+ constructor(x, y, canvas) {
+ this.x = x;
+ this.y = y;
+ this.canvas = canvas;
+ this.size = Math.random() * 5 + 1;
+ this.speedX = Math.random() * 3 - 1.5;
+ this.speedY = Math.random() * 3 - 1.5;
+ this.color = `hsl(${Math.random() * 60 + 200}, 70%, 50%)`;
+ }
+
+ update(mouseX, mouseY) {
+ this.x += this.speedX;
+ this.y += this.speedY;
+
+ const dx = mouseX - this.x;
+ const dy = mouseY - this.y;
+ const distance = Math.sqrt(dx * dx + dy * dy);
+
+ if (distance < 100) {
+ this.x -= (dx / distance) * 2;
+ this.y -= (dy / distance) * 2;
+ }
+
+ if (this.x < 0 || this.x > this.canvas.width) this.speedX *= -1;
+ if (this.y < 0 || this.y > this.canvas.height) this.speedY *= -1;
+ }
+
+ draw(ctx) {
+ ctx.fillStyle = this.color;
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
+ ctx.fill();
+ }
+ }
+
+ class ParticleSystem {
+ constructor() {
+ this.canvas = document.getElementById('particleCanvas');
+ this.ctx = this.canvas.getContext('2d');
+ this.particles = [];
+ this.mouseX = 0;
+ this.mouseY = 0;
+ this.colorMode = 'blue';
+
+ this.init();
+ this.setupEventListeners();
+ this.animate();
+ }
+
+ init() {
+ this.resize();
+ this.createParticles();
+ }
+
+ resize() {
+ this.canvas.width = window.innerWidth;
+ this.canvas.height = window.innerHeight;
+ }
+
+ createParticles() {
+ this.particles = [];
+ const numberOfParticles = Math.min(
+ Math.floor((this.canvas.width * this.canvas.height) / 9000),
+ 200
+ );
+
+ for (let i = 0; i < numberOfParticles; i++) {
+ const x = Math.random() * this.canvas.width;
+ const y = Math.random() * this.canvas.height;
+ this.particles.push(new Particle(x, y, this.canvas));
+ }
+ }
+
+ setupEventListeners() {
+ window.addEventListener('resize', () => this.resize());
+
+ this.canvas.addEventListener('mousemove', (e) => {
+ this.mouseX = e.clientX;
+ this.mouseY = e.clientY;
+ });
+
+ this.canvas.addEventListener('touchmove', (e) => {
+ e.preventDefault();
+ this.mouseX = e.touches[0].clientX;
+ this.mouseY = e.touches[0].clientY;
+ });
+
+ document.getElementById('resetBtn').addEventListener('click', () => {
+ this.createParticles();
+ });
+
+ document.getElementById('colorBtn').addEventListener('click', () => {
+ this.toggleColorMode();
+ });
+ }
+
+ toggleColorMode() {
+ const colors = {
+ blue: [200, 260],
+ purple: [260, 320],
+ red: [320, 380],
+ };
+
+ const modes = Object.keys(colors);
+ const currentIndex = modes.indexOf(this.colorMode);
+ this.colorMode = modes[(currentIndex + 1) % modes.length];
+
+ const in, max] = colors[this.colorMode];
+ this.particles.forEach(particle => {
+ particle.color = `hsl(${Math.random() * (max - min) + min}, 70%, 50%)`;
+ });
+ }
+
+ animate() {
+ this.ctx.fillStyle = 'rgba(42, 42, 74, 0.1)';
+ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
+
+ this.particles.forEach(particle => {
+ particle.update(this.mouseX, this.mouseY);
+ particle.draw(this.ctx);
+ });
+
+ requestAnimationFrame(() => this.animate());
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new ParticleSystem();
+ });