您的位置:首页 > 汽车 > 新车 > 【JS+H5+CSS实现烟花特效】

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

2024/10/20 0:29:32 来源:https://blog.csdn.net/qq_45458713/article/details/140303148  浏览:    关键词:【JS+H5+CSS实现烟花特效】

话不多说直接上代码

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

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:

    // 获取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.2this.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:

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.烟花和粒子分别拥有初始透明度,经过随机的透明度衰减,达到完全透明,来模拟烟花和粒子的消失

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com