网页特效代码烟花爆炸

要在网页上实现烟花爆炸的特效,通常需要结合HTML、CSS和JavaScript。创建一个高级的烟花爆炸特效通常涉及更复杂的动画、粒子系统以及可能的物理模拟。由于网页环境通常受限于性能和功能,我们可以使用像canvas这样的HTML5元素结合JavaScript来创建一个相对高级的烟花特效。

如果您想要一个高级的烟花爆炸特效,那么我们可以考虑使用更复杂的物理模拟、颜色变化、粒子间的交互以及更丰富的动画效果。以下是一个更高级的烟花爆炸特效的示例,使用了requestAnimationFrame进行动画循环,并考虑了颜色变化、粒子速度变化以及烟花爆炸的随机性。

代码

<!DOCTYPE html>
<html lang=“en”>
<head>

<meta charset="UTF-8">    
<meta name="viewport" content="width=device-width, initial-scale=1.0">    
<title>烟花盛开特效</title>    
<style>    
    body, html {    
        margin: 0;    
        padding: 0;    
        overflow: hidden;    
        background-color: #000;    
    }    
    #fireworksCanvas {    
        position: absolute;    
        top: 50%;    
        left: 50%;    
        transform: translate(-50%, -50%);    
    }    
</style>    

</head>
<body>

<canvas id="fireworksCanvas" width="800" height="600"></canvas>    
<script>    
    const canvas = document.getElementById('fireworksCanvas');    
    const ctx = canvas.getContext('2d');    
    const particles = [];    

    class Particle {    
        constructor(x, y, dx, dy, size, color, life) {    
            this.x = x;    
            this.y = y;    
            this.dx = dx;    
            this.dy = dy;    
            this.size = size;    
            this.color = color;    
            this.gravity = 0.1;    
            this.drag = 0.02;    
            this.life = life;    
        }    

        update() {    
            this.dy += this.gravity;    
            this.dx *= (1 - this.drag);    
            this.dy *= (1 - this.drag);    
            this.x += this.dx;    
            this.y += this.dy;    
            this.size *= 0.95;    
            this.life--;    

            if (this.life <= 0) {    
                this.dead = true;    
            }    
        }    

        draw() {    
            if (!this.dead) {    
                ctx.beginPath();    
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);    
                ctx.fillStyle = this.color;    
                ctx.fill();    
                ctx.closePath();    
            }    
        }    
    }    

    function createFireworkExplosion(centerX, centerY) {    
        const numParticles = 200;    
        const spread = 50;    
        const initialVelocity = 3;    
        const sizeRange = [2, 6];    
        const colorRange = ['#FF1493', '#FFA500', '#00BFFF', '#FF0000', '#8B4513'];    
        const lifeRange = [30, 60];    

        for (let i = 0; i < numParticles; i++) {    
            const angle = Math.random() * Math.PI * 2;    
            const dx = Math.cos(angle) * spread * Math.random();    
            const dy = Math.sin(angle) * spread * Math.random();    
            const size = Math.random() * (sizeRange[1] - sizeRange[0]) + sizeRange[0];    
            const color = colorRange[Math.floor(Math.random() * colorRange.length)];    
            const life = Math.random() * (lifeRange[1] - lifeRange[0]) + lifeRange[0];    
            particles.push(new Particle(centerX, centerY, dx + initialVelocity, dy + initialVelocity, size, color, life));    
        }    
    }    

    function animate() {    
        ctx.clearRect(0, 0, canvas.width, canvas.height);    

        for (let i = particles.length - 1; i >= 0; i--) {    
            particles[i].update();    
            particles[i].draw();    

            if (particles[i].dead) {    
                particles.splice(i, 1);    
            }    
        }    

        // 创建新的烟花爆炸    
        if (Math.random() < 0.01) {    
            createFireworkExplosion(canvas.width / 2, canvas.height / 2);    
        }    

        requestAnimationFrame(animate);    
    }    

    animate();    
</script>    

</body>
</html>

过期时间:永久公开
创建于:2024-03-21 16:40
133