<canvas id="canvas"></canvas> <div class="content"> <h1>Animated Background</h1> <p>Beautiful floating particles with smooth animations</p> </div>
* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; overflow: hidden; background: #1a1a1a; } #canvas { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; } .content { position: relative; z-index: 2; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; color: white; text-align: center; padding: 20px; } h1 { font-size: 3rem; font-weight: 300; margin-bottom: 1rem; text-shadow: 2px 2px 4px rgba(0,0,0,0.3); } p { font-size: 1.2rem; font-weight: 300; opacity: 0.9; max-width: 600px; }
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; initParticles(); }); const particlesArray = []; const numberOfParticles = 80; const primaryColor = '#f08080'; class Particle { constructor() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.size = Math.random() * 3 + 1; this.speedX = Math.random() * 1 - 0.5; this.speedY = Math.random() * 1 - 0.5; this.opacity = Math.random() * 0.5 + 0.3; } update() { this.x += this.speedX; this.y += this.speedY; if (this.x > canvas.width || this.x < 0) { this.speedX = -this.speedX; } if (this.y > canvas.height || this.y < 0) { this.speedY = -this.speedY; } } draw() { ctx.fillStyle = primaryColor; ctx.globalAlpha = this.opacity; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fill(); ctx.globalAlpha = 1; } } function initParticles() { particlesArray.length = 0; for (let i = 0; i < numberOfParticles; i++) { particlesArray.push(new Particle()); } } function connectParticles() { for (let a = 0; a < particlesArray.length; a++) { for (let b = a; b < particlesArray.length; b++) { const dx = particlesArray[a].x - particlesArray[b].x; const dy = particlesArray[a].y - particlesArray[b].y; const distance = Math.sqrt(dx * dx + dy * dy); if (distance < 120) { ctx.strokeStyle = primaryColor; ctx.globalAlpha = 0.15 * (1 - distance / 120); ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(particlesArray[a].x, particlesArray[a].y); ctx.lineTo(particlesArray[b].x, particlesArray[b].y); ctx.stroke(); ctx.globalAlpha = 1; } } } } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < particlesArray.length; i++) { particlesArray[i].update(); particlesArray[i].draw(); } connectParticles(); requestAnimationFrame(animate); } initParticles(); animate();
Login to leave a comment
No comments yet. Be the first!
View Project
No comments yet. Be the first!