2022-12-15 16:45:16.0
无极低码 :https://wheart.cn

效果图

 利用html5cavnas实现烟花特效,文字特效,新年倒计时,月亮动画特效

 1.找素材背景

百度搜了几张图片

2.抠图

网上找了在线美图秀秀,发现要钱,就顺便po解了一下,扣出素材

 3.编写代码

添加代码结构
 

  1. <div id="h1"></div>
  2. <div style="height:100vh;overflow:hidden;">
  3.     <canvas id='cas' style="background-color:rgba(0,5,24,1);">浏览器不支持canvas</canvas>
  4.     <div class="city">
  5.         <img class='img1' src="img/city.png"/>
  6.         <img class='img2' src="img/build.png"/>
  7.         <img class='img3' src="img/dfmz.png"/>
  8.     </div>
  9.     <img src="img/moon.png" id="myoon"  />
  10.     <img src="img/man.png" id="person"/>
  11.     <div style="display:none">
  12.         <div class="shape">新年快乐</div>
  13.         <div class="shape">合家幸福</div>
  14.         <div class="shape">万事如意</div>
  15.         <div class="shape">心想事成</div>
  16.         <div class="shape">财源广进</div>
  17.         <div class="shape">四海增辉</div>
  18.         <div class="shape">鹏程万里</div>
  19.         <div class="shape">五福临门</div>
  20.         <div class="shape">六六大顺</div>
  21.         <div class="shape">步步高升</div>
  22.     </div>
  23. </div>

4.添加样式

     

  1.    body{margin:0;padding:0;overflow: hidden;background-color: black;}
  2.         .city{
  3.             width:100%;
  4.             height:50vh;
  5.         position:fixed;
  6.         bottom: -12vh;
  7.         z-index: 100
  8.     }
  9.     .img1{
  10.         position:absolute;
  11.         z-index: 998;
  12.         left:  20vw;
  13.         width: 60vw;
  14.         height: 80vh;
  15.         bottom: -30vh;
  16.     }
  17.     .img2{
  18.         position:absolute;
  19.         z-index: 997;
  20.         height: 15vw;
  21.         right:  0;
  22.         bottom: 5vh;
  23.     }
  24.     .img3{
  25.         position:absolute;
  26.         z-index: 999;
  27.         height: 30vw;
  28.         left:  0;
  29.         bottom: 5vh;
  30.     }
  31.     #person{
  32.         position: fixed;top: 70px;right: 180px;width: 200px;height: 200px;
  33.         -webkit-transform: rotate(-15deg);
  34.     }
  35.     #myoon{
  36.         position: fixed;top: 30px;right: 150px;width: 240px;height: 240px;
  37.         border-radius:240px;
  38.         border: 0;
  39.         -webkit-animation: spin 100s linear infinite,light 100s  linear infinite;
  40.     }
  41.         /* 光晕效果 */
  42.     @-webkit-keyframes light {
  43.      from { background-color:rgba(255,255,255,0.8); -webkit-box-shadow: 0 0 5vw rgba(255,255,255,0.7); }
  44.      50% { background-color: rgba(255,255,255,0.7); -webkit-box-shadow: 0 0 10vw rgba(255,255,255,0.5); }
  45.      to { background-color:  rgba(255,255,255,0.6); -webkit-box-shadow: 0 0 15vw rgba(255,255,255,0.3); }
  46.     }
  47.     /* 旋转动画 */
  48.     @-webkit-keyframes spin {
  49.         0% {
  50.         -webkit-transform: rotate(0deg);
  51.         -ms-transform: rotate(0deg);
  52.         transform: rotate(0deg);
  53.         }
  54.         100% {
  55.         -webkit-transform: rotate(360deg);
  56.         -ms-transform: rotate(360deg);
  57.         transform: rotate(360deg);
  58.         }
  59.     }
  60.     /* 倒计时 */
  61.     #h1{
  62.       display: block;
  63.       position: fixed;
  64.       font-family: 'Love Ya Like A Sister', cursive;
  65.       font-size: 2vw;
  66.       font-weight: bolder;
  67.       background-color: transparent;
  68.       color: red;
  69.       padding:  20px;
  70.     }
  71.     #h1 span{
  72.         font-family: 'Times New Roman', Times, serif;
  73.         color: white;
  74.         font-size: 3vw;
  75.     }

5.添加烟花js

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

6.添加倒计时js

  1. var ptimer = setInterval( function time(){
  2. var nowtime = new Date().getTime(); // 现在时间转换为时间戳
  3. var futruetime = new Date('2023,01,01').getTime(); // 未来时间转换为时间戳
  4. var msec = futruetime-nowtime; // 毫秒 未来时间-现在时间
  5. var time = (msec/1000); // 毫秒/1000
  6. var day = parseInt(time/86400); // 天 24*60*60*1000
  7. var hour = parseInt(time/3600)-24*day; // 小时 60*60 总小时数-过去的小时数=现在的小时数
  8. var minute = parseInt(time%3600/60); // 分 -(day*24) 以60秒为一整份 取余 剩下秒数 秒数/60 就是分钟数
  9. var second = parseInt(time%60); // 以60秒为一整份 取余 剩下秒数
  10. document.getElementById('h1').innerHTML="距离2023年还有<span >"+day+"</span>天<span >"+hour+"</span>小时<span >"+minute+"</span>分<span >"+second+"</span>秒";
  11. return true;
  12. },0,1000); // 添加计时器

7.添加音效

<audio autoplay="autoplay"  loop><source src="写资源地址,这里放的鞭炮"></audio>
<audio autoplay="autoplay" ><source src="写资源地址,这里放的锣鼓”></audio>

无极低码 :https://wheart.cn
最新文章