Changed around line 1
+ const canvas = document.getElementById('particleCanvas');
+ const ctx = canvas.getContext('2d');
+ let particles = [];
+ let mouse = { x: null, y: null };
+ let colorMode = 'random';
+
+ class Particle {
+ constructor(x, y, size, speed) {
+ this.x = x;
+ this.y = y;
+ this.size = size;
+ this.speedX = (Math.random() * speed) - speed/2;
+ this.speedY = (Math.random() * speed) - speed/2;
+ this.color = this.getColor();
+ }
+
+ getColor() {
+ if (colorMode === 'random') {
+ return `hsl(${Math.random() * 360}, 50%, 50%)`;
+ } else {
+ return '#6c5ce7';
+ }
+ }
+
+ update() {
+ this.x += this.speedX;
+ this.y += this.speedY;
+
+ if (this.x < 0 || this.x > canvas.width) this.speedX *= -1;
+ if (this.y < 0 || this.y > canvas.height) this.speedY *= -1;
+
+ this.draw();
+ }
+
+ draw() {
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
+ ctx.fillStyle = this.color;
+ ctx.fill();
+ }
+ }
+
+ function initParticles(count, size, speed) {
+ particles = [];
+ for (let i = 0; i < count; i++) {
+ const x = Math.random() * canvas.width;
+ const y = Math.random() * canvas.height;
+ particles.push(new Particle(x, y, size, speed));
+ }
+ }
+
+ function animate() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ particles.forEach(particle => {
+ particle.update();
+
+ if (mouse.x && mouse.y) {
+ const dx = particle.x - mouse.x;
+ const dy = particle.y - mouse.y;
+ const distance = Math.sqrt(dx * dx + dy * dy);
+
+ if (distance < 100) {
+ ctx.beginPath();
+ ctx.moveTo(particle.x, particle.y);
+ ctx.lineTo(mouse.x, mouse.y);
+ ctx.strokeStyle = particle.color;
+ ctx.lineWidth = 0.2;
+ ctx.stroke();
+ }
+ }
+ });
+
+ requestAnimationFrame(animate);
+ }
+
+ function resizeCanvas() {
+ canvas.width = window.innerWidth;
+ canvas.height = window.innerHeight * 0.7;
+ initParticles(
+ particleCount.value,
+ particleSize.value,
+ particleSpeed.value
+ );
+ }
+
+ // Event Listeners
+ window.addEventListener('resize', resizeCanvas);
+ window.addEventListener('mousemove', (e) => {
+ mouse.x = e.x;
+ mouse.y = e.y;
+ });
+ window.addEventListener('mouseout', () => {
+ mouse.x = null;
+ mouse.y = null;
+ });
+
+ // Control Elements
+ const particleCount = document.getElementById('particleCount');
+ const particleSpeed = document.getElementById('particleSpeed');
+ const particleSize = document.getElementById('particleSize');
+ const colorModeBtn = document.getElementById('colorMode');
+
+ particleCount.addEventListener('input', () => {
+ initParticles(
+ particleCount.value,
+ particleSize.value,
+ particleSpeed.value
+ );
+ });
+
+ particleSpeed.addEventListener('input', () => {
+ particles.forEach(particle => {
+ particle.speedX = (Math.random() * particleSpeed.value) - particleSpeed.value/2;
+ particle.speedY = (Math.random() * particleSpeed.value) - particleSpeed.value/2;
+ });
+ });
+
+ particleSize.addEventListener('input', () => {
+ particles.forEach(particle => {
+ particle.size = particleSize.value;
+ });
+ });
+
+ colorModeBtn.addEventListener('click', () => {
+ colorMode = colorMode === 'random' ? 'solid' : 'random';
+ particles.forEach(particle => {
+ particle.color = particle.getColor();
+ });
+ });
+
+ // Initialization
+ resizeCanvas();
+ animate();