代码 HTML和JavaScript创建的烟花动画效果

2024-11-19 04:21:02 +0800 CST views 1066

该文本展示了一个使用HTML和JavaScript创建的烟花动画效果。通过在画布上绘制烟花和粒子,代码实现了烟花的生成、更新和绘制。包含了画布的自适应调整和烟花爆炸后粒子的动画效果,整体背景为黑色,营造出烟花绽放的视觉效果。
images

<!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 {
            margin: 0;
            background: black;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <canvas id="fireworksCanvas"></canvas>
    <script>
        const canvas = document.getElementById("fireworksCanvas");
        const ctx = canvas.getContext("2d");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }

        window.addEventListener("resize", resizeCanvas, false);

        class Firework {
            constructor() {
                this.x = Math.random() * canvas.width;
                this.y = canvas.height;
                this.sx = Math.random() * 3 - 1.5;
                this.sy = Math.random() * -3 - 3;
                this.size = Math.random() * 2 + 1;
                this.shouldExplode = false;

                const colorVal = Math.round(0xffffff * Math.random());
                const r = colorVal >> 16;
                const g = (colorVal >> 8) & 255;
                const b = colorVal & 255;
                this.r = r;
                this.g = g;
                this.b = b;
            }

            update() {
                if (this.sy >= -2 || this.y <= 100 || this.x <= 0 || this.x >= canvas.width) {
                    this.shouldExplode = true;
                } else {
                    this.sy += 0.01;
                }
                this.x += this.sx;
                this.y += this.sy;
            }

            draw() {
                ctx.fillStyle = `rgb(${this.r},${this.g},${this.b})`;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fill();
            }
        }

        class Particle {
            constructor(x, y, r, g, b) {
                this.x = x;
                this.y = y;
                this.sx = Math.random() * 3 - 1.5;
                this.sy = Math.random() * 3 - 1.5;
                this.size = Math.random() * 2 + 1;
                this.life = 100;
                this.r = r;
                this.g = g;
                this.b = b;
            }

            update() {
                this.x += this.sx;
                this.y += this.sy;
                this.life -= 1;
            }

            draw() {
                ctx.fillStyle = `rgba(${this.r},${this.g},${this.b},${this.life / 100})`;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fill();
            }
        }

        const fireworks = [new Firework()];
        const particles = [];

        function animate() {
            ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            if (Math.random() < 0.05) {
                fireworks.push(new Firework());
            }

            for (let i = fireworks.length - 1; i >= 0; i--) {
                fireworks[i].update();
                fireworks[i].draw();
                if (fireworks[i].shouldExplode) {
                    for (let j = 0; j < 50; j++) {
                        particles.push(new Particle(fireworks[i].x, fireworks[i].y, fireworks[i].r, fireworks[i].g, fireworks[i].b));
                    }
                    fireworks.splice(i, 1);
                }
            }

            for (let i = particles.length - 1; i >= 0; i--) {
                particles[i].update();
                particles[i].draw();
                if (particles[i].life <= 0) {
                    particles.splice(i, 1);
                }
            }

            requestAnimationFrame(animate);
        }

        animate();
    </script>
</body>

</html>

复制全文 生成海报 前端开发 动画 图形编程 JavaScript HTML

推荐文章

JavaScript 的模板字符串
2024-11-18 22:44:09 +0800 CST
404错误页面的HTML代码
2024-11-19 06:55:51 +0800 CST
H5端向App端通信(Uniapp 必会)
2025-02-20 10:32:26 +0800 CST
thinkphp swoole websocket 结合的demo
2024-11-18 10:18:17 +0800 CST
Gin 与 Layui 分页 HTML 生成工具
2024-11-19 09:20:21 +0800 CST
Shell 里给变量赋值为多行文本
2024-11-18 20:25:45 +0800 CST
使用 Git 制作升级包
2024-11-19 02:19:48 +0800 CST
Vue3中如何处理路由和导航?
2024-11-18 16:56:14 +0800 CST
go错误处理
2024-11-18 18:17:38 +0800 CST
在Rust项目中使用SQLite数据库
2024-11-19 08:48:00 +0800 CST
MySQL死锁 - 更新插入导致死锁
2024-11-19 05:53:50 +0800 CST
如何实现虚拟滚动
2024-11-18 20:50:47 +0800 CST
一些好玩且实用的开源AI工具
2024-11-19 09:31:57 +0800 CST
Vue3结合Driver.js实现新手指引功能
2024-11-19 08:46:50 +0800 CST
mysql int bigint 自增索引范围
2024-11-18 07:29:12 +0800 CST
随机分数html
2025-01-25 10:56:34 +0800 CST
php内置函数除法取整和取余数
2024-11-19 10:11:51 +0800 CST
Vue中的表单处理有哪几种方式?
2024-11-18 01:32:42 +0800 CST
php常用的正则表达式
2024-11-19 03:48:35 +0800 CST
Go 1.23 中的新包:unique
2024-11-18 12:32:57 +0800 CST
程序员茄子在线接单