难道你也不能放烟花嘛?那就来看看这个吧!
创始人
2024-05-12 06:57:14
0

又到了一年一度的春节时期啦!昨天呢是北方的小年,今天是南方的小年,看到大家可以愉快的放烟花,过大年很是羡慕呀!辞旧岁,贺新春,今年我呀要放烟花,过春节!🧨。

这个特效简单的使用了前端三件套即可完成,html,js,css,canvas整体效果如下GIF图所示(码内隐藏特殊变量,找到有惊喜!)

背景音乐是《China-E》个人感觉很有新年的感觉,整个China系列的歌曲都很nice,该特效的寓意就是开门大吉,辞旧迎新,2023年的大门向你敞开,新的一年想你招手,小兔子抱着锦鲤,也预示着吉祥,山鱼在这里祝大家前兔无量,大展宏兔!

就是开心,就是玩,就是兔个吉利!,话不多说上代码!

比较多的css代码,所以单独放在了一个文件下,如果用的时候出现图片丢失的问题,可以看看路径写对了没

/* 如果单独放记得去掉style标签哦 */

比比比比较多的js代码,注意同上

// 烟花生成
window.requestAnimationFrame = (function () {return window.requestAnimationFrame ||window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||function (callback) {window.setTimeout(callback, 1000)}
})();
// 获取画布
var area = document.getElementById("mycanvas");
area.width = document.documentElement.clientWidth;
area.height = document.documentElement.clientHeight;var ctx = area.getContext("2d");hue = 120;
timerTick = 0;
timerTotal = 5;
fireworks = [];
particles = [];function random(min, max) {return Math.random() * (max - min) + min;
}function distans(sx, sy, tx, ty) {var xdistan = sx - tx;var ydistan = sy - ty;return Math.sqrt((Math.pow(xdistan, 2) + Math.pow(ydistan, 2)));
}function Firework(sx, sy, tx, ty) {this.x = sx;this.y = sy;this.sx = sx;this.sy = sy;this.tx = tx;this.ty = ty;this.targetDistances = distans(sx, sy, tx, ty);this.distancesc = 0;this.shanyu = [];this.shanyucount = 3;while (this.shanyucount--) {this.shanyu.push([this.x, this.y]);}this.angle = Math.atan2(ty - sy, tx - sx);this.speed = 2;this.jiasudu = 1.05;this.brightness = random(50, 70);this.targetRad = 5;
}Firework.prototype.update = function (index) {this.shanyu.pop();this.shanyu.push([this.x, this.y]);if (this.targetRad < 8) {this.targetRad += 0.3;} else {this.targetRad = 1;}this.speed *= this.jiasudu;var vx = Math.cos(this.angle) * this.speed;var vy = Math.sin(this.angle) * this.speed;this.distancesc = distans(this.sx, this.sy, this.x + vx, this.y + vy);if (this.distancesc >= this.targetDistances) {createparticals(this.tx, this.ty);fireworks.splice(index, 1)} else {this.x += vx;this.y += vy;}
}Firework.prototype.draw = function () {ctx.beginPath();ctx.moveTo(this.shanyu[this.shanyu.length - 1][0], this.shanyu[this.shanyu.length - 1][1]);ctx.lineTo(this.x, this.y);ctx.strokeStyle = 'hsl(' + hue + ',100%,' + this.brightness + '%)';ctx.stroke();ctx.beginPath();ctx.arc(this.tx, this.ty, this.targetRad, 0, Math.PI * 2);ctx.stroke();
}function Particle(x, y) {this.x = x;this.y = y;this.shanyu = [];this.shanyucount = 10;while (this.shanyucount--) {this.shanyu.push([this.x, this.y]);}this.angle = random(0, 2 * Math.PI);this.speed = random(1, 10);this.mocal = 0.95;this.gravity = 0.98;this.hue = random(hue - 20, hue + 20);this.brightness = random(50, 80);this.alpha = 1;this.decay = random(0.015, 0.03);
}Particle.prototype.update = function (index) {this.shanyu.pop();this.shanyu.unshift([this.x, this.y]);this.speed *= this.mocal;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)}
}
Particle.prototype.draw = function () {ctx.beginPath();ctx.moveTo(this.shanyu[this.shanyu.length - 1][0], this.shanyu[this.shanyu.length - 1][1]);ctx.lineTo(this.x, this.y);ctx.strokeStyle = 'hsl(' + hue + ',100%,' + this.brightness + '%)';ctx.stroke();
}function createparticals(x, y) {var particalcount = 500;while (particalcount--) {particles.push(new Particle(x, y))}
}var clientw = document.documentElement.clientWidth;
var clienth = document.documentElement.clientHeight;function loop() {requestAnimationFrame(loop);hue += 0.5;ctx.globalCompositeOperation = 'destination-out';ctx.fillRect(0, 0, clientw, clienth);ctx.fillStyle = 'rgb(0,0,0,0.5)';ctx.globalCompositeOperation = 'lighter';var i = fireworks.length;while (i--) {fireworks[i].draw();fireworks[i].update(i);}var i = particles.length;while (i--) {particles[i].draw();particles[i].update(i);}if (timerTick >= timerTotal) {fireworks.push(new Firework(clientw / 2, clienth, random(0, clientw), random(0, clienth)));timerTick = 0;} else {timerTick++;}
}
window.οnlοad = loop();
window.onload = function starttime() {ptimer = setTimeout(starttime, 1000);
}
// 音乐控制
unmuteButton.addEventListener('click', function () {video.muted = false;
});
unmuteButton2.addEventListener('click', function () {video.muted = true;
});
结束喽,下一篇新春特效就是下一年喽!
点赞:您的赞赏是我前进的动力! 👍
收藏:您的支持我是创作的源泉! ⭐
评论:您的建议是我改进的良药! ✍
山鱼的个人社区:欢迎大家加入我的个人社区—— 山鱼社区

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...