Particle Galaxy Animation
Home
Snippets
Particle Galaxy Animation
HTML
CSS
JS
<canvas id="galaxy"></canvas>
body { margin: 0; height: 100vh; background: radial-gradient(circle at center, #0d0d1a, #000); overflow: hidden; } canvas { display: block; }
const canvas = document.getElementById("galaxy"); const ctx = canvas.getContext("2d"); let w, h; const particles = []; const numParticles = 120; function resizeCanvas() { w = canvas.width = window.innerWidth; h = canvas.height = window.innerHeight; } window.addEventListener("resize", resizeCanvas); resizeCanvas(); class Particle { constructor() { this.x = Math.random() * w; this.y = Math.random() * h; this.radius = Math.random() * 2 + 1; this.color = `hsl(${Math.random() * 360}, 70%, 60%)`; this.speedX = (Math.random() - 0.5) * 0.8; this.speedY = (Math.random() - 0.5) * 0.8; } move() { this.x += this.speedX; this.y += this.speedY; if (this.x < 0 || this.x > w) this.speedX *= -1; if (this.y < 0 || this.y > h) this.speedY *= -1; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); } } for (let i = 0; i < numParticles; i++) { particles.push(new Particle()); } let mouse = { x: null, y: null }; window.addEventListener("mousemove", e => { mouse.x = e.clientX; mouse.y = e.clientY; }); function connectParticles() { for (let i = 0; i < particles.length; i++) { for (let j = i; j < particles.length; j++) { const dx = particles[i].x - particles[j].x; const dy = particles[i].y - particles[j].y; const dist = Math.sqrt(dx * dx + dy * dy); if (dist < 120) { ctx.strokeStyle = "rgba(255,255,255,0.1)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(particles[i].x, particles[i].y); ctx.lineTo(particles[j].x, particles[j].y); ctx.stroke(); } } } } function animate() { ctx.clearRect(0, 0, w, h); for (let particle of particles) { particle.move(); particle.draw(); if (mouse.x && mouse.y) { const dx = mouse.x - particle.x; const dy = mouse.y - particle.y; const dist = Math.sqrt(dx * dx + dy * dy); if (dist < 150) { particle.x += dx * 0.01; particle.y += dy * 0.01; } } } connectParticles(); requestAnimationFrame(animate); } animate();
Ad #1
Ad #2
Scroll to Top