夜空特效程序html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fireworks</title>
    <style>
        body, html {
            margin: 0;
            padding: 0;
            overflow: hidden;
            height: 100%;
            background: #000;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        function createGradient() {
            const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
            gradient.addColorStop(0, '#001848');
            gradient.addColorStop(1, '#000000');
            return gradient;
        }

        function drawBackground() {
            ctx.fillStyle = createGradient();
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        }

        function drawStars() {
            const starCount = 100;
            for (let i = 0; i < starCount; i++) {
                const x = Math.random() * canvas.width;
                const y = Math.random() * canvas.height * 0.5;
                const radius = Math.random() * 2;
                ctx.beginPath();
                ctx.arc(x, y, radius, 0, Math.PI * 2);
                ctx.fillStyle = 'white';
                ctx.fill();
                ctx.closePath();
            }
        }

        function random(min, max) {
            return Math.random() * (max - min) + min;
        }

        class Firework {
            constructor() {
                this.x = random(0, canvas.width);
                this.y = canvas.height;
                this.targetX = random(0, canvas.width);
                this.targetY = random(0, canvas.height / 2);
                this.speed = random(2, 5);
                this.angle = Math.atan2(this.targetY - this.y, this.targetX - this.x);
                this.coordinates = Array.from({ length: 3 }, () => [this.x, this.y]);
                this.distanceToTarget = Math.hypot(this.targetX - this.x, this.targetY - this.y);
                this.distanceTraveled = 0;
                this.brightness = random(50, 70);
                this.targetRadius = 1;
            }

            update(index) {
                this.coordinates.pop();
                this.coordinates.unshift([this.x, this.y]);

                this.targetRadius = this.targetRadius < 12 ? this.targetRadius + 0.5 : 1;

                this.x += Math.cos(this.angle) * this.speed;
                this.y += Math.sin(this.angle) * this.speed;

                this.distanceTraveled = Math.hypot(this.x - this.targetX, this.y - this.targetY);

                if (this.distanceTraveled >= this.distanceToTarget) {
                    createParticles(this.targetX, this.targetY);
                    fireworks.splice(index, 1);
                }
            }

            draw() {
                ctx.beginPath();
                ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
                ctx.lineTo(this.x, this.y);
                ctx.strokeStyle = `hsl(${random(0, 360)}, 100%, ${this.brightness}%)`;
                ctx.stroke();

                ctx.beginPath();
                ctx.arc(this.targetX, this.targetY, this.targetRadius, 0, Math.PI * 2);
                ctx.stroke();
            }
        }

        class Particle {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.coordinates = Array.from({ length: 5 }, () => [this.x, this.y]);
                this.angle = random(0, Math.PI * 2);
                this.speed = random(2, 12);
                this.friction = 0.95;
                this.gravity = 1.2;
                this.hue = random(0, 360);
                this.brightness = random(50, 80);
                this.alpha = 1;
                this.decay = random(0.01, 0.02);
                this.size = random(3, 5);
            }

            update(index) {
                this.coordinates.pop();
                this.coordinates.unshift([this.x, this.y]);

                this.speed *= this.friction;
                this.x += Math.cos(this.angle) * this.speed;
                this.y += Math.sin(this.angle) * this.speed + this.gravity;
                this.alpha -= this.decay;

                if (this.alpha <= this.decay) {
                    particles.splice(index, 1);
                }
            }

            draw() {
                ctx.beginPath();
                ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
                ctx.lineTo(this.x, this.y);
                ctx.strokeStyle = `hsla(${this.hue}, 100%, ${this.brightness}%, ${this.alpha})`;
                ctx.lineWidth = this.size;
                ctx.stroke();
            }
        }

        const fireworks = [];
        const particles = [];

        function createParticles(x, y) {
            for (let i = 0; i < 50; i++) {  // 减少粒子的数量
                particles.push(new Particle(x, y));
            }
        }

        function loop() {
            requestAnimationFrame(loop);

            ctx.globalCompositeOperation = 'destination-out';
            ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            ctx.globalCompositeOperation = 'lighter';

            drawBackground();
            drawStars();

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

            particles.forEach((particle, index) => {
                particle.draw();
                particle.update(index);
            });

            if (Math.random() < 0.1) {  // 减少烟花生成的频率
                const bursts = random(1, 2);  // 减少每次生成的烟花数量
                for (let i = 0; i < bursts; i++) {
                    fireworks.push(new Firework());
                }
            }
        }

        window.onload = loop;
        window.onresize = () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        };
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值