【JS+H5+CSS实现烟花特效】

话不多说直接上代码

注意:背景图路径是picture/star.jpg,自己在同级目录先创键picture目录再下载一张图片命名为star.jpg

HTML:

html 复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Fireworks Animation</title>
        <link rel="stylesheet" href="style.css" />
    </head>

    <body>
        <canvas id="fireworksCanvas"></canvas>
        <script src="script.js"></script>
    </body>
</html>

JS:

javascript 复制代码
    // 获取canvas元素
    const canvas = document.getElementById('fireworksCanvas');
    const ctx = canvas.getContext('2d');

    // 设置canvas宽度和高度为窗口宽度和高度
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    let fireworks = [];

    // 创建烟花类
    class Firework {
        constructor() {
            this.x = Math.random() * canvas.width; // 随机生成烟花的起始x坐标
            this.y = canvas.height; // 烟花起始y坐标为画布底部
            this.radius = 2; // 烟花半径
            this.speed = 7; // 烟花上升速度
            this.color = 'white'; // 烟花颜色
            this.exploded = false; // 烟花是否已经爆炸
            this.particles = []; // 烟花爆炸后的粒子数组
            this.explosionHeight = Math.random() * (canvas.height * 0.4) + (canvas.height * 0.1); // 生成随机的爆炸高度
            this.initialAlpha = 1; // 初始透明度
            this.currentAlpha = this.initialAlpha; // 当前透明度
        }

        update() {
            if (!this.exploded) {
                this.y -= this.speed; // 烟花向上运动
                // 逐渐减少烟花的透明度
                this.currentAlpha -= 0.005;
                if (this.currentAlpha < 0) {
                    this.currentAlpha = 0;
                }
                if (this.y <= this.explosionHeight) {
                    this.explode();
                }
            } else {
                this.particles.forEach((particle, index) => {
                    particle.update(); // 更新粒子的运动状态
                    if (particle.alpha <= 0) {
                        this.particles.splice(index, 1);
                    }
                });
            }
        }

        draw() {
            ctx.beginPath(); // 开始绘制路径
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); // 绘制一个以(x, y)为中心,半径为radius的圆
            ctx.fillStyle = this.color; // 设置填充颜色为烟花的颜色
            ctx.globalAlpha = this.currentAlpha; // 使用当前透明度
            ctx.fill(); // 填充圆
            ctx.closePath(); // 结束绘制路径

            if (this.exploded) {
                this.particles.forEach(particle => {
                    particle.draw(); // 绘制每个粒子
                });
            }
        }

        explode() {
            this.exploded = true; // 设置烟花为已经爆炸状态
            for (let i = 0; i < 100; i++) {
                let particle = new Particle(this.x, this.y); // 创建一个新的粒子,位置为烟花的位置
                this.particles.push(particle); // 将新创建的粒子添加到烟花的粒子数组中
            }
        }
    }

    // 创建粒子类
        class Particle {
        constructor(x, y) { // 粒子类的构造函数,接收x和y坐标作为参数
            this.x = x; // 设置粒子的x坐标
            this.y = y; // 设置粒子的y坐标
            this.radius = 2; // 设置粒子的半径
            this.speedX = Math.random() * 5 - 2; // 随机生成粒子在x轴上的速度
            this.speedY = Math.random() * 5 - 2; // 随机生成粒子在y轴上的速度
            this.color = `hsl(${Math.random() * 360}, 100%, 50%)`; // 随机生成粒子的颜色
            this.alpha = 1.2; // 设置粒子的透明度为1.2
            this.fade = Math.random() * 0.02 + 0.01; // 随机生成粒子透明度的减少速度
        }

        update() { // 更新粒子的位置和透明度
            this.x += this.speedX; // 更新粒子的x坐标
            this.y += this.speedY; // 更新粒子的y坐标
            this.alpha -= this.fade; // 减少粒子的透明度
        }

        draw() { // 绘制粒子
            ctx.beginPath(); // 开始绘制路径
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); // 绘制一个以(x, y)为中心,半径为radius的圆
            ctx.fillStyle = this.color; // 设置填充颜色为粒子的颜色
            ctx.globalAlpha = this.alpha; // 设置全局透明度为粒子的透明度
            ctx.fill(); // 填充圆
            ctx.closePath(); // 结束绘制路径
        }
    }

    function animate() { // 动画函数,更新画布内容并绘制烟花和粒子
        ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; // 设置画布背景颜色及透明度
        ctx.fillRect(0, 0, canvas.width, canvas.height); // 填充矩形覆盖画布

        fireworks.forEach((firework, index) => { // 遍历烟花数组
            firework.update(); // 更新烟花的位置和状态
            firework.draw(); // 绘制烟花及其粒子
            if (firework.exploded && firework.particles.length === 0) { // 如果烟花已经爆炸且没有粒子了
                fireworks.splice(index, 1); // 从烟花数组中移除该烟花
            }
        });

        requestAnimationFrame(animate); // 递归调用自身,实现连续动画效果
    }

    function init() { // 初始化函数
        setInterval(() => { // 每隔2秒执行一次
            let firework = new Firework(); // 创建一个新的烟花对象
            fireworks.push(firework); // 将新创建的烟花对象添加到烟花数组中
        }, 100);//每次循环的时间不能太长

        animate(); // 调用动画函数开始动画
    }

    init();

CSS:

css 复制代码
body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    background: url('picture/star.jpg') no-repeat center center fixed;
    background-size: cover;
}
canvas {
    display: block;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 0.5; /* 设置画布的透明度为0.5 */
}

代码可以直接用。设计思路如下:

1.设计了烟花类,粒子类

2.烟花从上升开始,直到爆炸,爆炸点创建100个粒子对象,粒子用来模拟烟花爆炸后散开的状态

3.烟花和粒子分别拥有初始透明度,经过随机的透明度衰减,达到完全透明,来模拟烟花和粒子的消失

相关推荐
^^为欢几何^^4 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
Hello-Mr.Wang9 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把12 分钟前
Java基础之IO流
java·开发语言
WG_1713 分钟前
C++多态
开发语言·c++·面试
宇卿.20 分钟前
Java键盘输入语句
java·开发语言
Amo Xiang29 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
程序员凡尘36 分钟前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
friklogff42 分钟前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
重生之我在20年代敲代码2 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring