程序员的另一种浪漫?简单HTML代码生成春节烟花,快去展示给爱的人吧

先上效果图

 

进入网页时会有少许绚丽的烟花。点击屏幕生成更加绚丽多彩的烟花

不想复制的已经打包放在下方链接,以供大家下载使用

https://download.csdn.net/download/xiaomeng1998_/88815753

不想下载的已经提供源码直接复制即可。

一、HTML部分

首先是html部分这里可以替换成自己想要的内容,比如爱人的名字 

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>春节烟花庆祝</title>

</head>
<body>
   <div class="greetings">
        <h1>春节快乐!</h1>
        <h1>舒娜我爱你!</h1>
        <p>欢迎来到我们的烟花庆祝活动</p>
    </div>
    <canvas id="fireworksCanvas"></canvas>
</body>
</html>

二、CSS部分

直接拿来复制即可

<style>
    body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: black;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

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

.greetings h1, .greetings p {
    z-index: 2;
    text-align: center;
}

/* 简单的动画效果增强视觉 */
.greetings h1 {
    animation: glow 1s ease-in-out infinite alternate;
}

.greetings p {
    animation: glow 2s ease-in-out infinite alternate;
}

@keyframes glow {
    from {
        text-shadow: 0 0 10px red, 0 0 20px red, 0 0 30px red, 0 0 40px red, 0 0 50px red, 0 0 60px red;
    }
    to {
        text-shadow: 0 0 20px yellow, 0 0 30px yellow, 0 0 40px yellow, 0 0 50px yellow, 0 0 60px yellow, 0 0 70px yellow;
    }
}

</style>

三、JavaScript部分

<script>
  document.addEventListener('DOMContentLoaded', function() {
    var canvas = document.getElementById('fireworksCanvas');
    var ctx = canvas.getContext('2d');

    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    function Particle(x, y, vx, vy, gravity) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.gravity = gravity;
        this.alpha = 1;
        this.color = `hsla(${Math.random() * 360}, 100%, 50%, ${this.alpha})`;
    }

    Particle.prototype.update = function() {
        this.vy += this.gravity;
        this.x += this.vx;
        this.y += this.vy;
        this.alpha -= 0.015;
    };

    Particle.prototype.draw = function() {
        ctx.globalCompositeOperation = 'lighter';
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, Math.random() * 3 + 1, 0, 2 * Math.PI);
        ctx.fill();
    };

    var particles = [];

    function spawnFireworks() {
        // 增加生成烟花的数量
        var count = Math.random() * 20 + 20; // 原来是 Math.random() * 10 + 10
        for (var i = 0; i < count; i++) {
            var particle = new Particle(canvas.width * Math.random(), canvas.height * Math.random(), Math.random() * 6 - 3, Math.random() * -3 - 1, 0.05);
            particles.push(particle);
        }
    }

    function explode(x, y) {
        // 增加每次点击产生烟花的粒子数
        var count = 100; // 原来是 100
        for (var i = 0; i < count; i++) {
            var vx = Math.random() * 5 - 2.5;
            var vy = Math.random() * 5 - 2.5;
            particles.push(new Particle(x, y, vx, vy, 0.04));
        }
    }

    function animate() {
        requestAnimationFrame(animate);
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        particles.forEach(function(particle, index) {
            particle.update();
            particle.draw();
            if (particle.alpha <= 0) {
                particles.splice(index, 1);
            }
        });
    }

    // 减小生成烟花的间隔时间,让烟花更频繁地出现
    setInterval(spawnFireworks, 500); // 原来是 1000

    canvas.addEventListener('click', function(e) {
        explode(e.clientX, e.clientY);
    });

    animate();
});


</script>

将其复制到网页编辑器即可

为了方便大家复制使用,全部使用的是js原生语法。

最后编写不易,如果那个人喜欢的话,记得多多点赞,多多收藏,感谢关注。

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小孟的报错日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值