【HTML实现烟花特效】

```html

<!DOCTYPE html>

<html lang="zh">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>烟花特效</title>

    <style>

        body {

            margin: 0;

            overflow: hidden;

        }

        canvas {

            display: block;

        }

    </style>

</head>

<body>

    <canvas id="fireworks"></canvas>

    <script>

        const canvas = document.getElementById('fireworks');

        const ctx = canvas.getContext('2d');

        canvas.width = window.innerWidth;

        canvas.height = window.innerHeight;

 

        class Firework {

            constructor(x, y, color) {

                this.x = x;

                this.y = y;

                this.color = color;

                this.particles = [];

                for (let i = 0; i < 50; i++) {

                    this.particles.push(new Particle(this.x, this.y, this.color));

                }

            }

 

            update() {

                this.particles.forEach((particle, index) => {

                    if (particle.alpha <= 0) {

                        this.particles.splice(index, 1);

                    } else {

                        particle.update();

                    }

                });

            }

 

            draw() {

                this.particles.forEach(particle => particle.draw());

            }

        }

 

        class Particle {

            constructor(x, y, color) {

                this.x = x;

                this.y = y;

                this.color = color;

                this.size = Math.random() * 5 + 1;

                this.speedX = Math.random() * 3 - 1.5;

                this.speedY = Math.random() * 3 - 1.5;

                this.alpha = 1;

            }

 

            update() {

                this.x += this.speedX;

                this.y += this.speedY;

                this.alpha -= 0.01;

            }

 

            draw() {

                ctx.globalAlpha = this.alpha;

                ctx.fillStyle = this.color;

                ctx.beginPath();

                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);

                ctx.closePath();

                ctx.fill();

            }

        }

 

        const fireworks = [];

 

        function animate() {

            ctx.clearRect(0, 0, canvas.width, canvas.height);

            fireworks.forEach((firework, index) => {

                if (firework.particles.length === 0) {

                    fireworks.splice(index, 1);

                } else {

                    firework.update();

                    firework.draw();

                }

            });

            requestAnimationFrame(animate);

        }

 

        canvas.addEventListener('click', (e) => {

            const x = e.clientX;

            const y = e.clientY;

            const color = `hsl(${Math.random() * 360}, 50%, 50%)`;

            fireworks.push(new Firework(x, y, color));

        });

 

        animate();

    </script>

</body>

</html>

```

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值