3D particle explosion
Home
Snippets
3D particle explosion
HTML
CSS
JS
<body> <div class="box"> <div class="face front"></div> <div class="face back"></div> <div class="face left"></div> <div class="face right"></div> <div class="face top"></div> <div class="face bottom"></div> </div> <canvas id="canvas"></canvas> </body>
body { display: flex; justify-content: center; align-items: center; height: 100vh; background: #141414; overflow: hidden; perspective: 1000px; } .box { position: absolute; width: 100px; height: 100px; transform-style: preserve-3d; animation: rotateBox 5s linear infinite; } .face { position: absolute; width: 100px; height: 100px; background: rgba(255, 255, 255, 0.1); border: 2px solid #00ff99; display: flex; justify-content: center; align-items: center; } .front { transform: translateZ(50px); } .back { transform: rotateY(180deg) translateZ(50px); } .left { transform: rotateY(-90deg) translateZ(50px); } .right { transform: rotateY(90deg) translateZ(50px); } .top { transform: rotateX(90deg) translateZ(50px); } .bottom { transform: rotateX(-90deg) translateZ(50px); } @keyframes rotateBox { 0% { transform: rotateX(0deg) rotateY(0deg); } 100% { transform: rotateX(360deg) rotateY(360deg); } } canvas { position: absolute; top: 0; left: 0; }
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const particlesArray = []; const colors = ['#ff0066', '#ffcc00', '#00ff99', '#0066ff']; class Particle { constructor() { this.x = window.innerWidth / 2; this.y = window.innerHeight / 2; this.size = Math.random() * 5 + 1; this.color = colors[Math.floor(Math.random() * colors.length)]; this.speedX = (Math.random() - 0.5) * 5; this.speedY = (Math.random() - 0.5) * 5; this.life = 100; } update() { this.x += this.speedX; this.y += this.speedY; this.size *= 0.95; this.life -= 1; } draw() { ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fill(); } } function handleParticles() { for (let i = 0; i < particlesArray.length; i++) { particlesArray[i].update(); particlesArray[i].draw(); if (particlesArray[i].life <= 0 || particlesArray[i].size <= 0.2) { particlesArray.splice(i, 1); i--; } } } function createParticles() { for (let i = 0; i < 5; i++) { particlesArray.push(new Particle()); } } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); createParticles(); handleParticles(); requestAnimationFrame(animate); } animate();
Ad #1
Ad #2
Scroll to Top