生日主题的烟花特效HTML,CSS,JS

目录

图片展示

完整代码

关键点解释


图片展示

完整代码

html 复制代码
<!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;
            overflow: hidden;
            background: #000;
            color: white;
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }
        #fireworksCanvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        .happy-birthday {
            position: relative;
            z-index: 10;
            font-size: 3rem;
            font-weight: bold;
            text-align: center;
            color: #fff;
            text-shadow: 0 0 20px rgba(255, 255, 255, 0.8),
                         0 0 30px rgba(255, 255, 0, 0.8),
                         0 0 40px rgba(255, 0, 0, 0.8);
            animation: glow 2s infinite alternate;
        }
        @keyframes glow {
            from {
                text-shadow: 0 0 10px rgba(255, 255, 255, 0.6),
                             0 0 20px rgba(255, 255, 0, 0.6),
                             0 0 30px rgba(255, 0, 0, 0.6);
            }
            to {
                text-shadow: 0 0 20px rgba(255, 255, 255, 0.8),
                             0 0 40px rgba(255, 255, 0, 0.8),
                             0 0 60px rgba(255, 0, 0, 0.8);
            }
        }
    </style>
</head>
<body>
    <canvas id="fireworksCanvas"></canvas>
    <div class="happy-birthday">🎉 生日快乐!🎉</div>

    <script>
        // 初始化画布
        const canvas = document.getElementById('fireworksCanvas');
        const ctx = canvas.getContext('2d');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        // 烟花类
        class Firework {
            constructor(x, y, colors) {
                this.x = x;
                this.y = y;
                this.colors = colors;
                this.particles = [];
                this.createParticles();
            }

            createParticles() {
                for (let i = 0; i < 100; i++) {
                    const angle = Math.random() * 2 * Math.PI;
                    const speed = Math.random() * 4 + 2;
                    const color = this.colors[Math.floor(Math.random() * this.colors.length)];
                    this.particles.push({
                        x: this.x,
                        y: this.y,
                        vx: Math.cos(angle) * speed,
                        vy: Math.sin(angle) * speed,
                        alpha: 1,
                        color: color
                    });
                }
            }

            update() {
                this.particles.forEach(p => {
                    p.x += p.vx;
                    p.y += p.vy;
                    p.alpha -= 0.02;
                });
                this.particles = this.particles.filter(p => p.alpha > 0);
            }

            draw() {
                this.particles.forEach(p => {
                    ctx.save();
                    ctx.globalAlpha = p.alpha;
                    ctx.fillStyle = p.color;
                    ctx.beginPath();
                    ctx.arc(p.x, p.y, 3, 0, Math.PI * 2);
                    ctx.fill();
                    ctx.restore();
                });
            }

            isDone() {
                return this.particles.length === 0;
            }
        }

        // 声明 fireworks 为 let
        let fireworks = [];
        const colors = ['#ff3d3d', '#ff9c3d', '#ffe03d', '#3dff83', '#3db9ff', '#9c3dff', '#ff3de8'];

        // 添加烟花
        function addFirework() {
            const x = Math.random() * canvas.width;
            const y = Math.random() * canvas.height / 2;
            fireworks.push(new Firework(x, y, colors));
        }

        // 主循环
        function loop() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            if (Math.random() < 0.05) addFirework();

            fireworks.forEach(firework => {
                firework.update();
                firework.draw();
            });

            // 重新赋值,确保 fireworks 变量可变
            fireworks = fireworks.filter(firework => !firework.isDone());
            requestAnimationFrame(loop);
        }

        loop();

        // 窗口大小调整
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });
    </script>
</body>
</html>

关键点解释

  1. 修复错误

    • fireworks 声明改为 let,确保可以在循环中通过 fireworks = fireworks.filter(...) 重新赋值。
  2. 烟花功能

    • 每个烟花是由 Firework 类生成的粒子组成,模拟了绚丽的散开效果。
  3. 生日主题

    • 中心的文字动画通过 @keyframes glow 实现,配合文字 🎉 生日快乐!🎉 增加了节日氛围。
  4. 响应式设计

    • 监听 resize 事件,确保画布随窗口调整大小。

嗨,我是命运之光。如果你觉得我的分享有价值,不妨通过以下方式表达你的支持:👍 点赞来表达你的喜爱,📁 关注以获取我的最新消息,💬 评论与我交流你的见解。我会继续努力,为你带来更多精彩和实用的内容。

点击这里👉 ,获取最新动态,⚡️ 让信息传递更加迅速。

相关推荐
yuhaiqiang11 分钟前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
红色石头本尊28 分钟前
1-umi-前端工程化搭建
前端
真夜35 分钟前
关于对echart盒子设置百分比读取的宽高没有撑开盒子解决方案
前端
楠木6851 小时前
RAG 资料库 Demo 完整开发流程
前端·ai编程
肠胃炎1 小时前
挂载方式部署项目
服务器·前端·nginx
像我这样帅的人丶你还1 小时前
使用 Next.js + Prisma + MySQL 开发全栈项目
前端
FPGA小迷弟1 小时前
FPGA 时序约束基础:从时钟定义到输入输出延迟的完整设置
前端·学习·fpga开发·verilog·fpga
Kel1 小时前
深入剖析 openai-node 源码:一个工业级 TypeScript SDK 的架构之美
javascript·人工智能·架构
毛骗导演1 小时前
@tencent-weixin/openclaw-weixin 插件深度解析(四):API 协议与数据流设计
前端·架构
毛骗导演2 小时前
@tencent-weixin/openclaw-weixin 插件深度解析(二):消息处理系统架构
前端·架构