2022将至,前端程序员们应该一起放个烟花庆祝一下,走起

举报
孙叫兽 发表于 2022/01/14 00:16:59 2022/01/14
【摘要】 前言:小时候,在我印象中,每到快过年的时候就有很多卖炮仗的,一般也就是阳历的12月份到明年的正月15号卖炮仗的商家比较多,省下买辣条的钱去买炮仗,在老家也就过年和除夕两天及正月15日这几天放烟花和炮仗比较猛,现在年纪大了,听不得炮仗那种噪声了,也考虑到环保,工作之后的程序员以代码的形式演绎一下烟花的效果。 目录 效果图...

前言:小时候,在我印象中,每到快过年的时候就有很多卖炮仗的,一般也就是阳历的12月份到明年的正月15号卖炮仗的商家比较多,省下买辣条的钱去买炮仗,在老家也就过年和除夕两天及正月15日这几天放烟花和炮仗比较猛,现在年纪大了,听不得炮仗那种噪声了,也考虑到环保,工作之后的程序员以代码的形式演绎一下烟花的效果。

目录

效果图

项目结构

html

javascript

完整代码及必要的配图


效果图

首先我们下看一段小视频

前端程序员自制春节元宵烟花

项目结构

一个网页+两个配图

html

引入两个背景图片衬托一下,先写几段话用于在播放烟花的字幕,可以根据自己的兴趣随意添加,直接复制粘帖修改文字即可。


  
  
  1. <canvas id='cas' style="background-color:rgba(0,5,24,1)">浏览器不支持canvas</canvas>
  2. <div class="city"><img src="city.png" alt="" /></div>
  3. <img src="moon.png" alt="" id="moon" style="visibility: hidden;"/>
  4. <div style="display:none">
  5. <div class="shape">孙叫兽祝大家</div>
  6. <div class="shape">2022年</div>
  7. <div class="shape">新年快乐</div>
  8. <div class="shape">财富自由</div>
  9. <div class="shape">合家幸福</div>
  10. <div class="shape">万事如意</div>
  11. <div class="shape">心想事成</div>
  12. <div class="shape">财源广进</div>
  13. </div>

javascript

主要控制文字显示的位置,文字及烟花出现的频率。


  
  
  1. var canvas = document.getElementById("cas");
  2. var ocas = document.createElement("canvas");
  3. var octx = ocas.getContext("2d");
  4. var ctx = canvas.getContext("2d");
  5. ocas.width = canvas.width = window.innerWidth;
  6. ocas.height = canvas.height = window.innerHeight;
  7. var bigbooms = [];
  8. window.onload = function(){
  9. initAnimate()
  10. }
  11. function initAnimate(){
  12. drawBg();
  13. lastTime = new Date();
  14. animate();
  15. }
  16. var lastTime;
  17. function animate(){
  18. ctx.save();
  19. ctx.fillStyle = "rgba(0,5,24,0.1)";
  20. ctx.fillRect(0,0,canvas.width,canvas.height);
  21. ctx.restore();
  22. var newTime = new Date();
  23. if(newTime-lastTime>500+(window.innerHeight-767)/2){
  24. var random = Math.random()*100>2?true:false;
  25. var x = getRandom(canvas.width/5 , canvas.width*4/5);
  26. var y = getRandom(50 , 200);
  27. if(random){
  28. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  29. bigbooms.push(bigboom)
  30. }
  31. else {
  32. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:canvas.width/2 , y:200} , document.querySelectorAll(".shape")[parseInt(getRandom(0, document.querySelectorAll(".shape").length))]);
  33. bigbooms.push(bigboom)
  34. }
  35. lastTime = newTime;
  36. console.log(bigbooms)
  37. }
  38. stars.foreach(function(){
  39. this.paint();
  40. })
  41. drawMoon();
  42. bigbooms.foreach(function(index){
  43. var that = this;
  44. if(!this.dead){
  45. this._move();
  46. this._drawLight();
  47. }
  48. else{
  49. this.booms.foreach(function(index){
  50. if(!this.dead) {
  51. this.moveTo(index);
  52. }
  53. else if(index === that.booms.length-1){
  54. bigbooms[bigbooms.indexOf(that)] = null;
  55. }
  56. })
  57. }
  58. });
  59. raf(animate);
  60. }
  61. function drawMoon(){
  62. var moon = document.getElementById("moon");
  63. var centerX = canvas.width-200 , centerY = 100 , width = 80;
  64. if(moon.complete){
  65. ctx.drawImage(moon , centerX , centerY , width , width )
  66. }
  67. else {
  68. moon.onload = function(){
  69. ctx.drawImage(moon ,centerX , centerY , width , width)
  70. }
  71. }
  72. var index = 0;
  73. for(var i=0;i<10;i++){
  74. ctx.save();
  75. ctx.beginPath();
  76. ctx.arc(centerX+width/2 , centerY+width/2 , width/2+index , 0 , 2*Math.PI);
  77. ctx.fillStyle="rgba(240,219,120,0.005)";
  78. index+=2;
  79. ctx.fill();
  80. ctx.restore();
  81. }
  82. }
  83. Array.prototype.foreach = function(callback){
  84. for(var i=0;i<this.length;i++){
  85. if(this[i]!==null) callback.apply(this[i] , [i])
  86. }
  87. }
  88. var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); };
  89. canvas.onclick = function(){
  90. var x = event.clientX;
  91. var y = event.clientY;
  92. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  93. bigbooms.push(bigboom)
  94. }
  95. // canvas.addEventLisener("touchstart" , function(event){
  96. // var touch = event.targetTouches[0];
  97. // var x = event.pageX;
  98. // var y = event.pageY;
  99. // var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  100. // bigbooms.push(bigboom)
  101. // })
  102. var Boom = function(x,r,c,boomArea,shape){
  103. this.booms = [];
  104. this.x = x;
  105. this.y = (canvas.height+r);
  106. this.r = r;
  107. this.c = c;
  108. this.shape = shape || false;
  109. this.boomArea = boomArea;
  110. this.theta = 0;
  111. this.dead = false;
  112. this.ba = parseInt(getRandom(80 , 100));
  113. }
  114. Boom.prototype = {
  115. _paint:function(){
  116. ctx.save();
  117. ctx.beginPath();
  118. ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
  119. ctx.fillStyle = this.c;
  120. ctx.fill();
  121. ctx.restore();
  122. },
  123. _move:function(){
  124. var dx = this.boomArea.x - this.x , dy = this.boomArea.y - this.y;
  125. this.x = this.x+dx*0.01;
  126. this.y = this.y+dy*0.01;
  127. if(Math.abs(dx)<=this.ba && Math.abs(dy)<=this.ba){
  128. if(this.shape){
  129. this._shapBoom();
  130. }
  131. else this._boom();
  132. this.dead = true;
  133. }
  134. else {
  135. this._paint();
  136. }
  137. },
  138. _drawLight:function(){
  139. ctx.save();
  140. ctx.fillStyle = "rgba(255,228,150,0.3)";
  141. ctx.beginPath();
  142. ctx.arc(this.x , this.y , this.r+3*Math.random()+1 , 0 , 2*Math.PI);
  143. ctx.fill();
  144. ctx.restore();
  145. },
  146. _boom:function(){
  147. var fragNum = getRandom(30 , 200);
  148. var style = getRandom(0,10)>=5? 1 : 2;
  149. var color;
  150. if(style===1){
  151. color = {
  152. a:parseInt(getRandom(128,255)),
  153. b:parseInt(getRandom(128,255)),
  154. c:parseInt(getRandom(128,255))
  155. }
  156. }
  157. var fanwei = parseInt(getRandom(300, 400));
  158. for(var i=0;i<fragNum;i++){
  159. if(style===2){
  160. color = {
  161. a:parseInt(getRandom(128,255)),
  162. b:parseInt(getRandom(128,255)),
  163. c:parseInt(getRandom(128,255))
  164. }
  165. }
  166. var a = getRandom(-Math.PI, Math.PI);
  167. var x = getRandom(0, fanwei) * Math.cos(a) + this.x;
  168. var y = getRandom(0, fanwei) * Math.sin(a) + this.y;
  169. var radius = getRandom(0 , 2)
  170. var frag = new Frag(this.x , this.y , radius , color , x , y );
  171. this.booms.push(frag);
  172. }
  173. },
  174. _shapBoom:function(){
  175. var that = this;
  176. putValue(ocas , octx , this.shape , 5, function(dots){
  177. var dx = canvas.width/2-that.x;
  178. var dy = canvas.height/2-that.y;
  179. for(var i=0;i<dots.length;i++){
  180. color = {a:dots[i].a,b:dots[i].b,c:dots[i].c}
  181. var x = dots[i].x;
  182. var y = dots[i].y;
  183. var radius = 1;
  184. var frag = new Frag(that.x , that.y , radius , color , x-dx , y-dy);
  185. that.booms.push(frag);
  186. }
  187. })
  188. }
  189. }
  190. function putValue(canvas , context , ele , dr , callback){
  191. context.clearRect(0,0,canvas.width,canvas.height);
  192. var img = new Image();
  193. if(ele.innerHTML.indexOf("img")>=0){
  194. img.src = ele.getElementsByTagName("img")[0].src;
  195. imgload(img , function(){
  196. context.drawImage(img , canvas.width/2 - img.width/2 , canvas.height/2 - img.width/2);
  197. dots = getimgData(canvas , context , dr);
  198. callback(dots);
  199. })
  200. }
  201. else {
  202. var text = ele.innerHTML;
  203. context.save();
  204. var fontSize =200;
  205. context.font = fontSize+"px 宋体 bold";
  206. context.textAlign = "center";
  207. context.textBaseline = "middle";
  208. context.fillStyle = "rgba("+parseInt(getRandom(128,255))+","+parseInt(getRandom(128,255))+","+parseInt(getRandom(128,255))+" , 1)";
  209. context.fillText(text , canvas.width/2 , canvas.height/2);
  210. context.restore();
  211. dots = getimgData(canvas , context , dr);
  212. callback(dots);
  213. }
  214. }
  215. function imgload(img , callback){
  216. if(img.complete){
  217. callback.call(img);
  218. }
  219. else {
  220. img.onload = function(){
  221. callback.call(this);
  222. }
  223. }
  224. }
  225. function getimgData(canvas , context , dr){
  226. var imgData = context.getImageData(0,0,canvas.width , canvas.height);
  227. context.clearRect(0,0,canvas.width , canvas.height);
  228. var dots = [];
  229. for(var x=0;x<imgData.width;x+=dr){
  230. for(var y=0;y<imgData.height;y+=dr){
  231. var i = (y*imgData.width + x)*4;
  232. if(imgData.data[i+3] > 128){
  233. var dot = {x:x , y:y , a:imgData.data[i] , b:imgData.data[i+1] , c:imgData.data[i+2]};
  234. dots.push(dot);
  235. }
  236. }
  237. }
  238. return dots;
  239. }
  240. function getRandom(a , b){
  241. return Math.random()*(b-a)+a;
  242. }
  243. var maxRadius = 1 , stars=[];
  244. function drawBg(){
  245. for(var i=0;i<100;i++){
  246. var r = Math.random()*maxRadius;
  247. var x = Math.random()*canvas.width;
  248. var y = Math.random()*2*canvas.height - canvas.height;
  249. var star = new Star(x , y , r);
  250. stars.push(star);
  251. star.paint()
  252. }
  253. }
  254. var Star = function(x,y,r){
  255. this.x = x;this.y=y;this.r=r;
  256. }
  257. Star.prototype = {
  258. paint:function(){
  259. ctx.save();
  260. ctx.beginPath();
  261. ctx.arc(this.x , this.y , this.r , 0 , 2*Math.PI);
  262. ctx.fillStyle = "rgba(255,255,255,"+this.r+")";
  263. ctx.fill();
  264. ctx.restore();
  265. }
  266. }
  267. var focallength = 250;
  268. var Frag = function(centerX , centerY , radius , color ,tx , ty){
  269. this.tx = tx;
  270. this.ty = ty;
  271. this.x = centerX;
  272. this.y = centerY;
  273. this.dead = false;
  274. this.centerX = centerX;
  275. this.centerY = centerY;
  276. this.radius = radius;
  277. this.color = color;
  278. }
  279. Frag.prototype = {
  280. paint:function(){
  281. ctx.save();
  282. ctx.beginPath();
  283. ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
  284. ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)";
  285. ctx.fill()
  286. ctx.restore();
  287. },
  288. moveTo:function(index){
  289. this.ty = this.ty+0.3;
  290. var dx = this.tx - this.x , dy = this.ty - this.y;
  291. this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1);
  292. this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1);
  293. if(dx===0 && Math.abs(dy)<=80){
  294. this.dead = true;
  295. }
  296. this.paint();
  297. }
  298. }

完整代码及必要的配图

点我下载

好啦,本期内容就分享到这里,我们下期见!

文章来源: sunmenglei.blog.csdn.net,作者:孙叫兽,版权归原作者所有,如需转载,请联系作者。

原文链接:sunmenglei.blog.csdn.net/article/details/122484504

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。