跳到正文

科技·工程

烟花班主任节

xwh15868902076约 14 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1
代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>班主任节快乐</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #000;
            overflow: hidden;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        .container {
            position: relative;
            width: 100vw;
            height: 100vh;
        }

        canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .message {
            position: absolute;
            top: 20%;
            left: 50%;
            transform: translateX(-50%);
            color: white;
            text-align: center;
            z-index: 10;
            text-shadow: 0 0 10px #fff, 0 0 20px #fff;
            opacity: 0;
            transition: opacity 2s;
        }

        .message.show {
            opacity: 1;
        }

        .message h1 {
            font-size: 3rem;
            margin-bottom: 20px;
        }

        .class-tag {
            display: block;
            font-size: 1.8rem;
            color: #ffcc00;
            margin-top: 10px;
        }

        .message p {
            font-size: 1.5rem;
            line-height: 1.6;
        }

        .launch-btn {
            position: absolute;
            bottom: 50px;
            left: 50%;
            transform: translateX(-50%);
            padding: 15px 30px;
            background: linear-gradient(45deg, #ff6b6b, #ffa502);
            border: none;
            border-radius: 50px;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            z-index: 10;
            box-shadow: 0 0 20px rgba(255, 105, 180, 0.7);
            transition: all 0.3s ease;
        }

        .launch-btn:hover {
            transform: translateX(-50%) scale(1.1);
            box-shadow: 0 0 30px rgba(255, 105, 180, 0.9);
        }

        .launch-btn:active {
            transform: translateX(-50%) scale(0.95);
        }
    </style>
</head>
<body>
    <div class="container">
        <canvas id="fireworksCanvas"></canvas>
        <div class="message" id="message">
            <h1>亲爱的班主任老师<span class="class-tag">——804班</span></h1>
            <p>感谢您的辛勤付出和无私奉献<br>
            您是我们成长路上的引路人<br>
            您用知识浇灌我们的心田<br>
            您用爱心温暖我们的童年<br>
            祝您班主任节快乐!<br>
            身体健康!工作顺利!</p>
        </div>
        <button class="launch-btn" id="launchBtn">发射烟花庆祝</button>
    </div>

    <script>
        const canvas = document.getElementById('fireworksCanvas');
        const ctx = canvas.getContext('2d');
        const message = document.getElementById('message');
        const launchBtn = document.getElementById('launchBtn');

        // 设置画布大小
        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }

        window.addEventListener('resize', resizeCanvas);
        resizeCanvas();

        // 烟花粒子类
        class Particle {
            constructor(x, y, color) {
                this.x = x;
                this.y = y;
                this.color = color;
                this.radius = Math.random() * 2 + 1;
                this.velocity = {
                    x: (Math.random() - 0.5) * 10,
                    y: (Math.random() - 0.5) * 10
                };
                this.alpha = 1;
                this.gravity = 0.05;
                this.decay = Math.random() * 0.03 + 0.01;
            }

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

            update() {
                this.velocity.y += this.gravity;
                this.x += this.velocity.x;
                this.y += this.velocity.y;
                this.alpha -= this.decay;

                return this.alpha > 0;
            }
        }

        // 烟花类
        class Firework {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.particles = [];
                this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
                this.particleCount = Math.floor(Math.random() * 100) + 100;

                // 创建粒子
                for (let i = 0; i < this.particleCount; i++) {
                    this.particles.push(new Particle(this.x, this.y, this.color));
                }
            }

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

            update() {
                // 更新所有粒子
                for (let i = this.particles.length - 1; i >= 0; i--) {
                    if (!this.particles[i].update()) {
                        this.particles.splice(i, 1);
                    }
                }

                return this.particles.length > 0;
            }
        }

        // 存储所有烟花
        let fireworks = [];

        // 创建随机烟花
        function createRandomFirework() {
            const x = Math.random() * canvas.width;
            const y = Math.random() * canvas.height / 2;
            fireworks.push(new Firework(x, y));
        }

        // 持续发射烟花的函数
        function startContinuousFireworks() {
            // 显示祝福语
            message.classList.add('show');

            // 立即发射一波烟花
            for (let j = 0; j < 5; j++) {
                setTimeout(() => {
                    createRandomFirework();
                }, j * 200);
            }

            // 每隔1秒继续发射烟花
            setInterval(() => {
                for (let j = 0; j < 3; j++) {
                    setTimeout(() => {
                        createRandomFirework();
                    }, j * 200);
                }
            }, 1000);
        }

        // 绑定按钮事件
        launchBtn.addEventListener('click', () => {
            // 按钮点击后消失
            launchBtn.style.display = 'none';
            startContinuousFireworks();
        });

        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            // 更新和绘制所有烟花
            for (let i = fireworks.length - 1; i >= 0; i--) {
                if (!fireworks[i].update()) {
                    fireworks.splice(i, 1);
                } else {
                    fireworks[i].draw();
                }
            }
        }

        animate();
    </script>
</body>
</html>
原文