当前位置:   article > 正文

过年过节通过html+css+js代码实现:超级好看的放烟花效果(含背景音乐)_新年网页特效

新年网页特效

文章目录:

一:放烟花 

1. 运行效果

2.代码

二:新年快乐

1.运行效果

2.代码


一:放烟花 

1. 运行效果

效果图◕‿◕✌✌✌ 

2.代码

新建文件——>修改后缀为".html"的格式——>然后在浏览器打开 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>带你赏烟花</title>
  6. <style>
  7. html,body{height:100%;margin:0;padding:0}
  8. ul,li{text-indent:0;text-decoration:none;margin:0;padding:0}
  9. img{border:0}
  10. //body{background-color:#000;color:#999;font:100%/18px helvetica, arial, sans-serif}
  11. body{
  12. background-image:url('https://pic.imgdb.cn/item/65904a9dc458853aef39cba8.png');no-repeat:background-image;background-size:100% 100%;
  13. color:#999;font:100%/18px helvetica, arial, sans-serif}
  14. canvas{cursor:crosshair;display:block;left:0;position:absolute;top:0;z-index:20}
  15. </style>
  16. </head>
  17. <body>
  18. <!--autoplay loop controls hidden id="audios" type="audio/mp3-->
  19. <audio src="https://www.joy127.com/url/108693.mp3" autoplay="autoplay" loop="loop" preload="auto" controls="controls" hidden id="audios" type="audio/mp3"></audio>
  20. <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.0/jquery.js"></script>
  21. <script>
  22. <!--自动播放(手机)-->
  23. window.onload = function() {
  24. var music = new Audio("music.mp3");
  25. music.play();
  26. };
  27. <!--单击自动播放(电脑)-->
  28. document.addEventListener('click', function() {
  29. document.getElementById('audios').play();
  30. });
  31. <!--加载自动播放(电脑)-->
  32. document.addEventListener('touchstart', function() {
  33. document.getElementById('audios').play();
  34. });
  35. </script>
  36. <script type="text/javascript">
  37. $(function(){
  38. var Fireworks = function(){
  39. var self = this;
  40. var rand = function(rMi, rMa){return ~~((Math.random()*(rMa-rMi+1))+rMi);}
  41. var hitTest = function(x1, y1, w1, h1, x2, y2, w2, h2){return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);};
  42. window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1E3/60)}}();
  43. self.init = function(){
  44. self.canvas = document.createElement('canvas');
  45. self.canvas.width = self.cw = $(window).innerWidth();
  46. self.canvas.height = self.ch = $(window).innerHeight();
  47. self.particles = [];
  48. self.partCount = 500; //密集度
  49. self.fireworks = [];
  50. self.mx = self.cw/2;
  51. self.my = self.ch/2;
  52. self.currentHue = 30;
  53. self.partSpeed = 3; //大小
  54. self.partSpeedVariance = 15; //爆炸范围
  55. self.partWind =10; //线条形状
  56. self.partFriction = 10; //烟花大小
  57. self.partGravity = 1; //下坠
  58. self.hueMin = 0;
  59. self.hueMax = 500; //颜色
  60. self.fworkSpeed = 2; //速度
  61. self.fworkAccel = 5; //导向显示
  62. self.hueVariance = 5;
  63. self.flickerDensity = 250;
  64. self.showShockwave = true;
  65. self.showTarget = false;
  66. self.clearAlpha = 70; //消失的速度
  67. $(document.body).append(self.canvas);
  68. self.ctx = self.canvas.getContext('2d');
  69. self.ctx.lineCap = 'round';
  70. self.ctx.lineJoin = 'round';
  71. self.lineWidth = 1; //粗细
  72. self.bindEvents();
  73. self.canvasLoop();
  74. self.canvas.onselectstart = function() {
  75. return false;
  76. };
  77. };
  78. self.createParticles = function(x,y, hue){
  79. var countdown = self.partCount;
  80. while(countdown--){
  81. var newParticle = {
  82. x: x,
  83. y: y,
  84. coordLast: [
  85. {x: x, y: y},
  86. {x: x, y: y},
  87. {x: x, y: y}
  88. ],
  89. angle: rand(0, 360),
  90. speed: rand(((self.partSpeed - self.partSpeedVariance) <= 0) ? 1 : self.partSpeed - self.partSpeedVariance, (self.partSpeed + self.partSpeedVariance)),
  91. friction: 1 - self.partFriction/100,
  92. gravity: self.partGravity/2,
  93. hue: rand(hue-self.hueVariance, hue+self.hueVariance),
  94. brightness: rand(50, 80),
  95. alpha: rand(40,100)/100,
  96. decay: rand(10, 50)/1000,
  97. wind: (rand(0, self.partWind) - (self.partWind/2))/25,
  98. lineWidth: self.lineWidth
  99. };
  100. self.particles.push(newParticle);
  101. }
  102. };
  103. self.updateParticles = function(){
  104. var i = self.particles.length;
  105. while(i--){
  106. var p = self.particles[i];
  107. var radians = p.angle * Math.PI / 180;
  108. var vx = Math.cos(radians) * p.speed;
  109. var vy = Math.sin(radians) * p.speed;
  110. p.speed *= p.friction;
  111. p.coordLast[2].x = p.coordLast[1].x;
  112. p.coordLast[2].y = p.coordLast[1].y;
  113. p.coordLast[1].x = p.coordLast[0].x;
  114. p.coordLast[1].y = p.coordLast[0].y;
  115. p.coordLast[0].x = p.x;
  116. p.coordLast[0].y = p.y;
  117. p.x += vx;
  118. p.y += vy;
  119. p.y += p.gravity;
  120. p.angle += p.wind;
  121. p.alpha -= p.decay;
  122. if(!hitTest(0,0,self.cw,self.ch,p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2) || p.alpha < .05){
  123. self.particles.splice(i, 1);
  124. }
  125. };
  126. };
  127. self.drawParticles = function(){
  128. var i = self.particles.length;
  129. while(i--){
  130. var p = self.particles[i];
  131. var coordRand = (rand(1,3)-1);
  132. self.ctx.beginPath();
  133. self.ctx.moveTo(Math.round(p.coordLast[coordRand].x), Math.round(p.coordLast[coordRand].y));
  134. self.ctx.lineTo(Math.round(p.x), Math.round(p.y));
  135. self.ctx.closePath();
  136. self.ctx.strokeStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+p.alpha+')';
  137. self.ctx.stroke();
  138. if(self.flickerDensity > 0){
  139. var inverseDensity = 50 - self.flickerDensity;
  140. if(rand(0, inverseDensity) === inverseDensity){
  141. self.ctx.beginPath();
  142. self.ctx.arc(Math.round(p.x), Math.round(p.y), rand(p.lineWidth,p.lineWidth+3)/2, 0, Math.PI*2, false)
  143. self.ctx.closePath();
  144. var randAlpha = rand(50,100)/100;
  145. self.ctx.fillStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+randAlpha+')';
  146. self.ctx.fill();
  147. }
  148. }
  149. };
  150. };
  151. self.createFireworks = function(startX, startY, targetX, targetY){
  152. var newFirework = {
  153. x: startX,
  154. y: startY,
  155. startX: startX,
  156. startY: startY,
  157. hitX: false,
  158. hitY: false,
  159. coordLast: [
  160. {x: startX, y: startY},
  161. {x: startX, y: startY},
  162. {x: startX, y: startY}
  163. ],
  164. targetX: targetX,
  165. targetY: targetY,
  166. speed: self.fworkSpeed,
  167. angle: Math.atan2(targetY - startY, targetX - startX),
  168. shockwaveAngle: Math.atan2(targetY - startY, targetX - startX)+(90*(Math.PI/180)),
  169. acceleration: self.fworkAccel/100,
  170. hue: self.currentHue,
  171. brightness: rand(50, 80),
  172. alpha: rand(50,100)/100,
  173. lineWidth: self.lineWidth
  174. };
  175. self.fireworks.push(newFirework);
  176. };
  177. self.updateFireworks = function(){
  178. var i = self.fireworks.length;
  179. while(i--){
  180. var f = self.fireworks[i];
  181. self.ctx.lineWidth = f.lineWidth;
  182. vx = Math.cos(f.angle) * f.speed,
  183. vy = Math.sin(f.angle) * f.speed;
  184. f.speed *= 1 + f.acceleration;
  185. f.coordLast[2].x = f.coordLast[1].x;
  186. f.coordLast[2].y = f.coordLast[1].y;
  187. f.coordLast[1].x = f.coordLast[0].x;
  188. f.coordLast[1].y = f.coordLast[0].y;
  189. f.coordLast[0].x = f.x;
  190. f.coordLast[0].y = f.y;
  191. if(f.startX >= f.targetX){
  192. if(f.x + vx <= f.targetX){
  193. f.x = f.targetX;
  194. f.hitX = true;
  195. } else {
  196. f.x += vx;
  197. }
  198. } else {
  199. if(f.x + vx >= f.targetX){
  200. f.x = f.targetX;
  201. f.hitX = true;
  202. } else {
  203. f.x += vx;
  204. }
  205. }
  206. if(f.startY >= f.targetY){
  207. if(f.y + vy <= f.targetY){
  208. f.y = f.targetY;
  209. f.hitY = true;
  210. } else {
  211. f.y += vy;
  212. }
  213. } else {
  214. if(f.y + vy >= f.targetY){
  215. f.y = f.targetY;
  216. f.hitY = true;
  217. } else {
  218. f.y += vy;
  219. }
  220. }
  221. if(f.hitX && f.hitY){
  222. self.createParticles(f.targetX, f.targetY, f.hue);
  223. self.fireworks.splice(i, 1);
  224. }
  225. };
  226. };
  227. self.drawFireworks = function(){
  228. var i = self.fireworks.length;
  229. self.ctx.globalCompositeOperation = 'lighter';
  230. while(i--){
  231. var f = self.fireworks[i];
  232. self.ctx.lineWidth = f.lineWidth;
  233. var coordRand = (rand(1,3)-1);
  234. self.ctx.beginPath();
  235. self.ctx.moveTo(Math.round(f.coordLast[coordRand].x), Math.round(f.coordLast[coordRand].y));
  236. self.ctx.lineTo(Math.round(f.x), Math.round(f.y));
  237. self.ctx.closePath();
  238. self.ctx.strokeStyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+f.alpha+')';
  239. self.ctx.stroke();
  240. if(self.showTarget){
  241. self.ctx.save();
  242. self.ctx.beginPath();
  243. self.ctx.arc(Math.round(f.targetX), Math.round(f.targetY), rand(1,8), 0, Math.PI*2, false)
  244. self.ctx.closePath();
  245. self.ctx.lineWidth = 1;
  246. self.ctx.stroke();
  247. self.ctx.restore();
  248. }
  249. if(self.showShockwave){
  250. self.ctx.save();
  251. self.ctx.translate(Math.round(f.x), Math.round(f.y));
  252. self.ctx.rotate(f.shockwaveAngle);
  253. self.ctx.beginPath();
  254. self.ctx.arc(0, 0, 1*(f.speed/5), 0, Math.PI, true);
  255. self.ctx.strokeStyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+rand(25, 60)/100+')';
  256. self.ctx.lineWidth = f.lineWidth;
  257. self.ctx.stroke();
  258. self.ctx.restore();
  259. }
  260. };
  261. };
  262. self.bindEvents = function(){
  263. $(window).on('resize', function(){
  264. clearTimeout(self.timeout);
  265. self.timeout = setTimeout(function() {
  266. self.canvas.width = self.cw = $(window).innerWidth();
  267. self.canvas.height = self.ch = $(window).innerHeight();
  268. self.ctx.lineCap = 'round';
  269. self.ctx.lineJoin = 'round';
  270. },1);
  271. });
  272. window.onload=function(){
  273. tttt()
  274. }
  275. var tttt = setInterval(function(){
  276. var e={'pageX':Math.random()*1900,'pageY':Math.random()*900}
  277. self.mx = e.pageX - self.canvas.offsetLeft;
  278. self.my = e.pageY - self.canvas.offsetTop;
  279. self.currentHue = rand(self.hueMin, self.hueMax);
  280. self.createFireworks(self.cw/2, self.ch, self.mx, self.my);
  281. },50)
  282. }
  283. self.clear = function(){
  284. self.particles = [];
  285. self.fireworks = [];
  286. self.ctx.clearRect(0, 0, self.cw, self.ch);
  287. };
  288. self.canvasLoop = function(){
  289. requestAnimFrame(self.canvasLoop, self.canvas);
  290. self.ctx.globalCompositeOperation = 'destination-out';
  291. self.ctx.fillStyle = 'rgba(0,0,0,'+self.clearAlpha/100+')';
  292. self.ctx.fillRect(0,0,self.cw,self.ch);
  293. self.updateFireworks();
  294. self.updateParticles();
  295. self.drawFireworks();
  296. self.drawParticles();
  297. };
  298. self.init();
  299. }
  300. var fworks = new Fireworks();
  301. });
  302. </script>
  303. </body>
  304. </html>

二:新年快乐

1.运行效果

2.代码

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>新年快乐</title>
  6. <style>
  7. html,body{
  8. margin:0px;
  9. width:100%;
  10. height:100%;
  11. overflow:hidden;
  12. background:#000;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <canvas id="canvas" style="position:absolute;width:100%;height:100%;z-index:8888"></canvas>
  18. <canvas style="position:absolute;width:100%;height:100%;z-index:9999" class="canvas" ></canvas>
  19. <div class="overlay">
  20. <div class="tabs">
  21. <div class="tabs-labels"><span class="tabs-label">Commands</span><span class="tabs-label">Info</span><span class="tabs-label">Share</span></div>
  22. <div class="tabs-panels">
  23. <ul class="tabs-panel commands">
  24. </ul>
  25. </div>
  26. </div>
  27. </div>
  28. <script>
  29. function initVars(){
  30. pi=Math.PI;
  31. ctx=canvas.getContext("2d");
  32. canvas.width=canvas.clientWidth;
  33. canvas.height=canvas.clientHeight;
  34. cx=canvas.width/2;
  35. cy=canvas.height/2;
  36. playerZ=-25;
  37. playerX=playerY=playerVX=playerVY=playerVZ=pitch=yaw=pitchV=yawV=0;
  38. scale=600;
  39. seedTimer=0;seedInterval=5,seedLife=100;gravity=.02;
  40. seeds=new Array();
  41. sparkPics=new Array();
  42. s="https://cantelope.org/NYE/";
  43. for(i=1;i<=10;++i){
  44. sparkPic=new Image();
  45. sparkPic.src=s+"spark"+i+".png";
  46. sparkPics.push(sparkPic);
  47. }
  48. sparks=new Array();
  49. pow1=new Audio(s+"pow1.ogg");
  50. pow2=new Audio(s+"pow2.ogg");
  51. pow3=new Audio(s+"pow3.ogg");
  52. pow4=new Audio(s+"pow4.ogg");
  53. frames = 0;
  54. }
  55. function rasterizePoint(x,y,z){
  56. var p,d;
  57. x-=playerX;
  58. y-=playerY;
  59. z-=playerZ;
  60. p=Math.atan2(x,z);
  61. d=Math.sqrt(x*x+z*z);
  62. x=Math.sin(p-yaw)*d;
  63. z=Math.cos(p-yaw)*d;
  64. p=Math.atan2(y,z);
  65. d=Math.sqrt(y*y+z*z);
  66. y=Math.sin(p-pitch)*d;
  67. z=Math.cos(p-pitch)*d;
  68. var rx1=-1000,ry1=1,rx2=1000,ry2=1,rx3=0,ry3=0,rx4=x,ry4=z,uc=(ry4-ry3)*(rx2-rx1)-(rx4-rx3)*(ry2-ry1);
  69. if(!uc) return {x:0,y:0,d:-1};
  70. var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;
  71. var ub=((rx2-rx1)*(ry1-ry3)-(ry2-ry1)*(rx1-rx3))/uc;
  72. if(!z)z=.000000001;
  73. if(ua>0&&ua<1&&ub>0&&ub<1){
  74. return {
  75. x:cx+(rx1+ua*(rx2-rx1))*scale,
  76. y:cy+y/z*scale,
  77. d:Math.sqrt(x*x+y*y+z*z)
  78. };
  79. }else{
  80. return {
  81. x:cx+(rx1+ua*(rx2-rx1))*scale,
  82. y:cy+y/z*scale,
  83. d:-1
  84. };
  85. }
  86. }
  87. function spawnSeed(){
  88. seed=new Object();
  89. seed.x=-50+Math.random()*100;
  90. seed.y=25;
  91. seed.z=-50+Math.random()*100;
  92. seed.vx=.1-Math.random()*.2;
  93. seed.vy=-1.5;//*(1+Math.random()/2);
  94. seed.vz=.1-Math.random()*.2;
  95. seed.born=frames;
  96. seeds.push(seed);
  97. }
  98. function splode(x,y,z){
  99. t=5+parseInt(Math.random()*150);
  100. sparkV=1+Math.random()*2.5;
  101. type=parseInt(Math.random()*3);
  102. switch(type){
  103. case 0:
  104. pic1=parseInt(Math.random()*10);
  105. break;
  106. case 1:
  107. pic1=parseInt(Math.random()*10);
  108. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  109. break;
  110. case 2:
  111. pic1=parseInt(Math.random()*10);
  112. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  113. do{ pic3=parseInt(Math.random()*10); }while(pic3==pic1 || pic3==pic2);
  114. break;
  115. }
  116. for(m=1;m<t;++m){
  117. spark=new Object();
  118. spark.x=x; spark.y=y; spark.z=z;
  119. p1=pi*2*Math.random();
  120. p2=pi*Math.random();
  121. v=sparkV*(1+Math.random()/6)
  122. spark.vx=Math.sin(p1)*Math.sin(p2)*v;
  123. spark.vz=Math.cos(p1)*Math.sin(p2)*v;
  124. spark.vy=Math.cos(p2)*v;
  125. switch(type){
  126. case 0: spark.img=sparkPics[pic1]; break;
  127. case 1:
  128. spark.img=sparkPics[parseInt(Math.random()*2)?pic1:pic2];
  129. break;
  130. case 2:
  131. switch(parseInt(Math.random()*3)){
  132. case 0: spark.img=sparkPics[pic1]; break;
  133. case 1: spark.img=sparkPics[pic2]; break;
  134. case 2: spark.img=sparkPics[pic3]; break;
  135. }
  136. break;
  137. }
  138. spark.radius=25+Math.random()*50;
  139. spark.alpha=1;
  140. spark.trail=new Array();
  141. sparks.push(spark);
  142. }
  143. switch(parseInt(Math.random()*4)){
  144. case 0: pow=new Audio(s+"pow1.ogg"); break;
  145. case 1: pow=new Audio(s+"pow2.ogg"); break;
  146. case 2: pow=new Audio(s+"pow3.ogg"); break;
  147. case 3: pow=new Audio(s+"pow4.ogg"); break;
  148. }
  149. d=Math.sqrt((x-playerX)*(x-playerX)+(y-playerY)*(y-playerY)+(z-playerZ)*(z-playerZ));
  150. pow.volume=1.5/(1+d/10);
  151. pow.play();
  152. }
  153. function doLogic(){
  154. if(seedTimer<frames){
  155. seedTimer=frames+seedInterval*Math.random()*10;
  156. spawnSeed();
  157. }
  158. for(i=0;i<seeds.length;++i){
  159. seeds[i].vy+=gravity;
  160. seeds[i].x+=seeds[i].vx;
  161. seeds[i].y+=seeds[i].vy;
  162. seeds[i].z+=seeds[i].vz;
  163. if(frames-seeds[i].born>seedLife){
  164. splode(seeds[i].x,seeds[i].y,seeds[i].z);
  165. seeds.splice(i,1);
  166. }
  167. }
  168. for(i=0;i<sparks.length;++i){
  169. if(sparks[i].alpha>0 && sparks[i].radius>5){
  170. sparks[i].alpha-=.01;
  171. sparks[i].radius/=1.02;
  172. sparks[i].vy+=gravity;
  173. point=new Object();
  174. point.x=sparks[i].x;
  175. point.y=sparks[i].y;
  176. point.z=sparks[i].z;
  177. if(sparks[i].trail.length){
  178. x=sparks[i].trail[sparks[i].trail.length-1].x;
  179. y=sparks[i].trail[sparks[i].trail.length-1].y;
  180. z=sparks[i].trail[sparks[i].trail.length-1].z;
  181. d=((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y)+(point.z-z)*(point.z-z));
  182. if(d>9){
  183. sparks[i].trail.push(point);
  184. }
  185. }else{
  186. sparks[i].trail.push(point);
  187. }
  188. if(sparks[i].trail.length>5)sparks[i].trail.splice(0,1);
  189. sparks[i].x+=sparks[i].vx;
  190. sparks[i].y+=sparks[i].vy;
  191. sparks[i].z+=sparks[i].vz;
  192. sparks[i].vx/=1.075;
  193. sparks[i].vy/=1.075;
  194. sparks[i].vz/=1.075;
  195. }else{
  196. sparks.splice(i,1);
  197. }
  198. }
  199. p=Math.atan2(playerX,playerZ);
  200. d=Math.sqrt(playerX*playerX+playerZ*playerZ);
  201. d+=Math.sin(frames/80)/1.25;
  202. t=Math.sin(frames/200)/40;
  203. playerX=Math.sin(p+t)*d;
  204. playerZ=Math.cos(p+t)*d;
  205. yaw=pi+p+t;
  206. }
  207. function rgb(col){
  208. var r = parseInt((.5+Math.sin(col)*.5)*16);
  209. var g = parseInt((.5+Math.cos(col)*.5)*16);
  210. var b = parseInt((.5-Math.sin(col)*.5)*16);
  211. return "#"+r.toString(16)+g.toString(16)+b.toString(16);
  212. }
  213. function draw(){
  214. ctx.clearRect(0,0,cx*2,cy*2);
  215. ctx.fillStyle="#ff8";
  216. for(i=-100;i<100;i+=3){
  217. for(j=-100;j<100;j+=4){
  218. x=i;z=j;y=25;
  219. point=rasterizePoint(x,y,z);
  220. if(point.d!=-1){
  221. size=250/(1+point.d);
  222. d = Math.sqrt(x * x + z * z);
  223. a = 0.75 - Math.pow(d / 100, 6) * 0.75;
  224. if(a>0){
  225. ctx.globalAlpha = a;
  226. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  227. }
  228. }
  229. }
  230. }
  231. ctx.globalAlpha=1;
  232. for(i=0;i<seeds.length;++i){
  233. point=rasterizePoint(seeds[i].x,seeds[i].y,seeds[i].z);
  234. if(point.d!=-1){
  235. size=200/(1+point.d);
  236. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  237. }
  238. }
  239. point1=new Object();
  240. for(i=0;i<sparks.length;++i){
  241. point=rasterizePoint(sparks[i].x,sparks[i].y,sparks[i].z);
  242. if(point.d!=-1){
  243. size=sparks[i].radius*200/(1+point.d);
  244. if(sparks[i].alpha<0)sparks[i].alpha=0;
  245. if(sparks[i].trail.length){
  246. point1.x=point.x;
  247. point1.y=point.y;
  248. switch(sparks[i].img){
  249. case sparkPics[0]:ctx.strokeStyle="#f84";break;
  250. case sparkPics[1]:ctx.strokeStyle="#84f";break;
  251. case sparkPics[2]:ctx.strokeStyle="#8ff";break;
  252. case sparkPics[3]:ctx.strokeStyle="#fff";break;
  253. case sparkPics[4]:ctx.strokeStyle="#4f8";break;
  254. case sparkPics[5]:ctx.strokeStyle="#f44";break;
  255. case sparkPics[6]:ctx.strokeStyle="#f84";break;
  256. case sparkPics[7]:ctx.strokeStyle="#84f";break;
  257. case sparkPics[8]:ctx.strokeStyle="#fff";break;
  258. case sparkPics[9]:ctx.strokeStyle="#44f";break;
  259. }
  260. for(j=sparks[i].trail.length-1;j>=0;--j){
  261. point2=rasterizePoint(sparks[i].trail[j].x,sparks[i].trail[j].y,sparks[i].trail[j].z);
  262. if(point2.d!=-1){
  263. ctx.globalAlpha=j/sparks[i].trail.length*sparks[i].alpha/2;
  264. ctx.beginPath();
  265. ctx.moveTo(point1.x,point1.y);
  266. ctx.lineWidth=1+sparks[i].radius*10/(sparks[i].trail.length-j)/(1+point2.d);
  267. ctx.lineTo(point2.x,point2.y);
  268. ctx.stroke();
  269. point1.x=point2.x;
  270. point1.y=point2.y;
  271. }
  272. }
  273. }
  274. ctx.globalAlpha=sparks[i].alpha;
  275. ctx.drawImage(sparks[i].img,point.x-size/2,point.y-size/2,size,size);
  276. }
  277. }
  278. }
  279. function frame(){
  280. if(frames>100000){
  281. seedTimer=0;
  282. frames=0;
  283. }
  284. frames++;
  285. draw();
  286. doLogic();
  287. requestAnimationFrame(frame);
  288. }
  289. window.addEventListener("resize",()=>{
  290. canvas.width=canvas.clientWidth;
  291. canvas.height=canvas.clientHeight;
  292. cx=canvas.width/2;
  293. cy=canvas.height/2;
  294. });
  295. initVars();
  296. frame();
  297. </script>
  298. <script>
  299. /*
  300. Shape Shifter
  301. =============
  302. A canvas experiment by Kenneth Cachia
  303. http://www.kennethcachia.com
  304. Updated code
  305. ------------
  306. https://github.com/kennethcachia/Shape-Shifter
  307. */
  308. var S = {
  309. init: function () {
  310. var action = window.location.href,
  311. i = action.indexOf('?a=');
  312. S.Drawing.init('.canvas');
  313. document.body.classList.add('body--ready');
  314. if (i !== -1) {
  315. S.UI.simulate(decodeURI(action).substring(i + 3));
  316. } else {
  317. S.UI.simulate('|#countdown 3||新|年|快|乐|#rectangle|');
  318. }
  319. S.Drawing.loop(function () {
  320. S.Shape.render();
  321. });
  322. }
  323. };
  324. S.Drawing = (function () {
  325. var canvas,
  326. context,
  327. renderFn
  328. requestFrame = window.requestAnimationFrame ||
  329. window.webkitRequestAnimationFrame ||
  330. window.mozRequestAnimationFrame ||
  331. window.oRequestAnimationFrame ||
  332. window.msRequestAnimationFrame ||
  333. function(callback) {
  334. window.setTimeout(callback, 1000 / 60);
  335. };
  336. return {
  337. init: function (el) {
  338. canvas = document.querySelector(el);
  339. context = canvas.getContext('2d');
  340. this.adjustCanvas();
  341. window.addEventListener('resize', function (e) {
  342. S.Drawing.adjustCanvas();
  343. });
  344. },
  345. loop: function (fn) {
  346. renderFn = !renderFn ? fn : renderFn;
  347. this.clearFrame();
  348. renderFn();
  349. requestFrame.call(window, this.loop.bind(this));
  350. },
  351. adjustCanvas: function () {
  352. canvas.width = window.innerWidth;
  353. canvas.height = window.innerHeight;
  354. },
  355. clearFrame: function () {
  356. context.clearRect(0, 0, canvas.width, canvas.height);
  357. },
  358. getArea: function () {
  359. return { w: canvas.width, h: canvas.height };
  360. },
  361. drawCircle: function (p, c) {
  362. context.fillStyle = c.render();
  363. context.beginPath();
  364. context.arc(p.x, p.y, p.z, 0, 2 * Math.PI, true);
  365. context.closePath();
  366. context.fill();
  367. }
  368. }
  369. }());
  370. S.UI = (function () {
  371. var canvas = document.querySelector('.canvas'),
  372. interval,
  373. isTouch = false, //('ontouchstart' in window || navigator.msMaxTouchPoints),
  374. currentAction,
  375. resizeTimer,
  376. time,
  377. maxShapeSize = 30,
  378. firstAction = true,
  379. sequence = [],
  380. cmd = '#';
  381. function formatTime(date) {
  382. var h = date.getHours(),
  383. m = date.getMinutes(),
  384. m = m < 10 ? '0' + m : m;
  385. return h + ':' + m;
  386. }
  387. function getValue(value) {
  388. return value && value.split(' ')[1];
  389. }
  390. function getAction(value) {
  391. value = value && value.split(' ')[0];
  392. return value && value[0] === cmd && value.substring(1);
  393. }
  394. function timedAction(fn, delay, max, reverse) {
  395. clearInterval(interval);
  396. currentAction = reverse ? max : 1;
  397. fn(currentAction);
  398. if (!max || (!reverse && currentAction < max) || (reverse && currentAction > 0)) {
  399. interval = setInterval(function () {
  400. currentAction = reverse ? currentAction - 1 : currentAction + 1;
  401. fn(currentAction);
  402. if ((!reverse && max && currentAction === max) || (reverse && currentAction === 0)) {
  403. clearInterval(interval);
  404. }
  405. }, delay);
  406. }
  407. }
  408. function reset(destroy) {
  409. clearInterval(interval);
  410. sequence = [];
  411. time = null;
  412. destroy && S.Shape.switchShape(S.ShapeBuilder.letter(''));
  413. }
  414. function performAction(value) {
  415. var action,
  416. value,
  417. current;
  418. // overlay.classList.remove('overlay--visible');
  419. sequence = typeof(value) === 'object' ? value : sequence.concat(value.split('|'));
  420. // input.value = '';
  421. // checkInputWidth();
  422. timedAction(function (index) {
  423. current = sequence.shift();
  424. action = getAction(current);
  425. value = getValue(current);
  426. switch (action) {
  427. case 'countdown':
  428. value = parseInt(value) || 10;
  429. value = value > 0 ? value : 10;
  430. timedAction(function (index) {
  431. if (index === 0) {
  432. if (sequence.length === 0) {
  433. S.Shape.switchShape(S.ShapeBuilder.letter(''));
  434. } else {
  435. performAction(sequence);
  436. }
  437. } else {
  438. S.Shape.switchShape(S.ShapeBuilder.letter(index), true);
  439. }
  440. }, 1000, value, true);
  441. break;
  442. case 'rectangle':
  443. value = value && value.split('x');
  444. value = (value && value.length === 2) ? value : [maxShapeSize, maxShapeSize / 2];
  445. S.Shape.switchShape(S.ShapeBuilder.rectangle(Math.min(maxShapeSize, parseInt(value[0])), Math.min(maxShapeSize, parseInt(value[1]))));
  446. break;
  447. case 'circle':
  448. value = parseInt(value) || maxShapeSize;
  449. value = Math.min(value, maxShapeSize);
  450. S.Shape.switchShape(S.ShapeBuilder.circle(value));
  451. break;
  452. case 'time':
  453. var t = formatTime(new Date());
  454. if (sequence.length > 0) {
  455. S.Shape.switchShape(S.ShapeBuilder.letter(t));
  456. } else {
  457. timedAction(function () {
  458. t = formatTime(new Date());
  459. if (t !== time) {
  460. time = t;
  461. S.Shape.switchShape(S.ShapeBuilder.letter(time));
  462. }
  463. }, 1000);
  464. }
  465. break;
  466. default:
  467. S.Shape.switchShape(S.ShapeBuilder.letter(current[0] === cmd ? 'What?' : current));
  468. }
  469. }, 2000, sequence.length);
  470. }
  471. function checkInputWidth(e) {
  472. if (input.value.length > 18) {
  473. ui.classList.add('ui--wide');
  474. } else {
  475. ui.classList.remove('ui--wide');
  476. }
  477. if (firstAction && input.value.length > 0) {
  478. ui.classList.add('ui--enter');
  479. } else {
  480. ui.classList.remove('ui--enter');
  481. }
  482. }
  483. function bindEvents() {
  484. document.body.addEventListener('keydown', function (e) {
  485. input.focus();
  486. if (e.keyCode === 13) {
  487. firstAction = false;
  488. reset();
  489. performAction(input.value);
  490. }
  491. });
  492. canvas.addEventListener('click', function (e) {
  493. overlay.classList.remove('overlay--visible');
  494. });
  495. }
  496. function init() {
  497. bindEvents();
  498. // input.focus();
  499. isTouch && document.body.classList.add('touch');
  500. }
  501. // Init
  502. init();
  503. return {
  504. simulate: function (action) {
  505. performAction(action);
  506. }
  507. }
  508. }());
  509. S.UI.Tabs = (function () {
  510. var tabs = document.querySelector('.tabs'),
  511. labels = document.querySelector('.tabs-labels'),
  512. triggers = document.querySelectorAll('.tabs-label'),
  513. panels = document.querySelectorAll('.tabs-panel');
  514. function activate(i) {
  515. triggers[i].classList.add('tabs-label--active');
  516. panels[i].classList.add('tabs-panel--active');
  517. }
  518. function bindEvents() {
  519. labels.addEventListener('click', function (e) {
  520. var el = e.target,
  521. index;
  522. if (el.classList.contains('tabs-label')) {
  523. for (var t = 0; t < triggers.length; t++) {
  524. triggers[t].classList.remove('tabs-label--active');
  525. panels[t].classList.remove('tabs-panel--active');
  526. if (el === triggers[t]) {
  527. index = t;
  528. }
  529. }
  530. activate(index);
  531. }
  532. });
  533. }
  534. function init() {
  535. activate(0);
  536. bindEvents();
  537. }
  538. // Init
  539. init();
  540. }());
  541. S.Point = function (args) {
  542. this.x = args.x;
  543. this.y = args.y;
  544. this.z = args.z;
  545. this.a = args.a;
  546. this.h = args.h;
  547. };
  548. S.Color = function (r, g, b, a) {
  549. this.r = r;
  550. this.g = g;
  551. this.b = b;
  552. this.a = a;
  553. };
  554. S.Color.prototype = {
  555. render: function () {
  556. return 'rgba(' + this.r + ',' + + this.g + ',' + this.b + ',' + this.a + ')';
  557. }
  558. };
  559. S.Dot = function (x, y) {
  560. this.p = new S.Point({
  561. x: x,
  562. y: y,
  563. z: 5,
  564. a: 1,
  565. h: 0
  566. });
  567. this.e = 0.07;
  568. this.s = true;
  569. this.c = new S.Color(255, 255, 255, this.p.a);
  570. this.t = this.clone();
  571. this.q = [];
  572. };
  573. S.Dot.prototype = {
  574. clone: function () {
  575. return new S.Point({
  576. x: this.x,
  577. y: this.y,
  578. z: this.z,
  579. a: this.a,
  580. h: this.h
  581. });
  582. },
  583. _draw: function () {
  584. this.c.a = this.p.a;
  585. S.Drawing.drawCircle(this.p, this.c);
  586. },
  587. _moveTowards: function (n) {
  588. var details = this.distanceTo(n, true),
  589. dx = details[0],
  590. dy = details[1],
  591. d = details[2],
  592. e = this.e * d;
  593. if (this.p.h === -1) {
  594. this.p.x = n.x;
  595. this.p.y = n.y;
  596. return true;
  597. }
  598. if (d > 1) {
  599. this.p.x -= ((dx / d) * e);
  600. this.p.y -= ((dy / d) * e);
  601. } else {
  602. if (this.p.h > 0) {
  603. this.p.h--;
  604. } else {
  605. return true;
  606. }
  607. }
  608. return false;
  609. },
  610. _update: function () {
  611. if (this._moveTowards(this.t)) {
  612. var p = this.q.shift();
  613. if (p) {
  614. this.t.x = p.x || this.p.x;
  615. this.t.y = p.y || this.p.y;
  616. this.t.z = p.z || this.p.z;
  617. this.t.a = p.a || this.p.a;
  618. this.p.h = p.h || 0;
  619. } else {
  620. if (this.s) {
  621. this.p.x -= Math.sin(Math.random() * 3.142);
  622. this.p.y -= Math.sin(Math.random() * 3.142);
  623. } else {
  624. this.move(new S.Point({
  625. x: this.p.x + (Math.random() * 50) - 25,
  626. y: this.p.y + (Math.random() * 50) - 25,
  627. }));
  628. }
  629. }
  630. }
  631. d = this.p.a - this.t.a;
  632. this.p.a = Math.max(0.1, this.p.a - (d * 0.05));
  633. d = this.p.z - this.t.z;
  634. this.p.z = Math.max(1, this.p.z - (d * 0.05));
  635. },
  636. distanceTo: function (n, details) {
  637. var dx = this.p.x - n.x,
  638. dy = this.p.y - n.y,
  639. d = Math.sqrt(dx * dx + dy * dy);
  640. return details ? [dx, dy, d] : d;
  641. },
  642. move: function (p, avoidStatic) {
  643. if (!avoidStatic || (avoidStatic && this.distanceTo(p) > 1)) {
  644. this.q.push(p);
  645. }
  646. },
  647. render: function () {
  648. this._update();
  649. this._draw();
  650. }
  651. }
  652. S.ShapeBuilder = (function () {
  653. var gap = 13,
  654. shapeCanvas = document.createElement('canvas'),
  655. shapeContext = shapeCanvas.getContext('2d'),
  656. fontSize = 500,
  657. fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, sans-serif';
  658. function fit() {
  659. shapeCanvas.width = Math.floor(window.innerWidth / gap) * gap;
  660. shapeCanvas.height = Math.floor(window.innerHeight / gap) * gap;
  661. shapeContext.fillStyle = 'red';
  662. shapeContext.textBaseline = 'middle';
  663. shapeContext.textAlign = 'center';
  664. }
  665. function processCanvas() {
  666. var pixels = shapeContext.getImageData(0, 0, shapeCanvas.width, shapeCanvas.height).data;
  667. dots = [],
  668. pixels,
  669. x = 0,
  670. y = 0,
  671. fx = shapeCanvas.width,
  672. fy = shapeCanvas.height,
  673. w = 0,
  674. h = 0;
  675. for (var p = 0; p < pixels.length; p += (4 * gap)) {
  676. if (pixels[p + 3] > 0) {
  677. dots.push(new S.Point({
  678. x: x,
  679. y: y
  680. }));
  681. w = x > w ? x : w;
  682. h = y > h ? y : h;
  683. fx = x < fx ? x : fx;
  684. fy = y < fy ? y : fy;
  685. }
  686. x += gap;
  687. if (x >= shapeCanvas.width) {
  688. x = 0;
  689. y += gap;
  690. p += gap * 4 * shapeCanvas.width;
  691. }
  692. }
  693. return { dots: dots, w: w + fx, h: h + fy };
  694. }
  695. function setFontSize(s) {
  696. shapeContext.font = 'bold ' + s + 'px ' + fontFamily;
  697. }
  698. function isNumber(n) {
  699. return !isNaN(parseFloat(n)) && isFinite(n);
  700. }
  701. function init() {
  702. fit();
  703. window.addEventListener('resize', fit);
  704. }
  705. // Init
  706. init();
  707. return {
  708. imageFile: function (url, callback) {
  709. var image = new Image(),
  710. a = S.Drawing.getArea();
  711. image.onload = function () {
  712. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  713. shapeContext.drawImage(this, 0, 0, a.h * 0.6, a.h * 0.6);
  714. callback(processCanvas());
  715. };
  716. image.onerror = function () {
  717. callback(S.ShapeBuilder.letter('What?'));
  718. }
  719. image.src = url;
  720. },
  721. circle: function (d) {
  722. var r = Math.max(0, d) / 2;
  723. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  724. shapeContext.beginPath();
  725. shapeContext.arc(r * gap, r * gap, r * gap, 0, 2 * Math.PI, false);
  726. shapeContext.fill();
  727. shapeContext.closePath();
  728. return processCanvas();
  729. },
  730. letter: function (l) {
  731. var s = 0;
  732. setFontSize(fontSize);
  733. s = Math.min(fontSize,
  734. (shapeCanvas.width / shapeContext.measureText(l).width) * 0.8 * fontSize,
  735. (shapeCanvas.height / fontSize) * (isNumber(l) ? 1 : 0.45) * fontSize);
  736. setFontSize(s);
  737. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  738. shapeContext.fillText(l, shapeCanvas.width / 2, shapeCanvas.height / 2);
  739. return processCanvas();
  740. },
  741. rectangle: function (w, h) {
  742. var dots = [],
  743. width = gap * w,
  744. height = gap * h;
  745. for (var y = 0; y < height; y += gap) {
  746. for (var x = 0; x < width; x += gap) {
  747. dots.push(new S.Point({
  748. x: x,
  749. y: y,
  750. }));
  751. }
  752. }
  753. return { dots: dots, w: width, h: height };
  754. }
  755. };
  756. }());
  757. S.Shape = (function () {
  758. var dots = [],
  759. width = 0,
  760. height = 0,
  761. cx = 0,
  762. cy = 0;
  763. function compensate() {
  764. var a = S.Drawing.getArea();
  765. cx = a.w / 2 - width / 2;
  766. cy = a.h / 2 - height / 2;
  767. }
  768. return {
  769. shuffleIdle: function () {
  770. var a = S.Drawing.getArea();
  771. for (var d = 0; d < dots.length; d++) {
  772. if (!dots[d].s) {
  773. dots[d].move({
  774. x: Math.random() * a.w,
  775. y: Math.random() * a.h
  776. });
  777. }
  778. }
  779. },
  780. switchShape: function (n, fast) {
  781. var size,
  782. a = S.Drawing.getArea();
  783. width = n.w;
  784. height = n.h;
  785. compensate();
  786. if (n.dots.length > dots.length) {
  787. size = n.dots.length - dots.length;
  788. for (var d = 1; d <= size; d++) {
  789. dots.push(new S.Dot(a.w / 2, a.h / 2));
  790. }
  791. }
  792. var d = 0,
  793. i = 0;
  794. while (n.dots.length > 0) {
  795. i = Math.floor(Math.random() * n.dots.length);
  796. dots[d].e = fast ? 0.25 : (dots[d].s ? 0.14 : 0.11);
  797. if (dots[d].s) {
  798. dots[d].move(new S.Point({
  799. z: Math.random() * 20 + 10,
  800. a: Math.random(),
  801. h: 18
  802. }));
  803. } else {
  804. dots[d].move(new S.Point({
  805. z: Math.random() * 5 + 5,
  806. h: fast ? 18 : 30
  807. }));
  808. }
  809. dots[d].s = true;
  810. dots[d].move(new S.Point({
  811. x: n.dots[i].x + cx,
  812. y: n.dots[i].y + cy,
  813. a: 1,
  814. z: 5,
  815. h: 0
  816. }));
  817. n.dots = n.dots.slice(0, i).concat(n.dots.slice(i + 1));
  818. d++;
  819. }
  820. for (var i = d; i < dots.length; i++) {
  821. if (dots[i].s) {
  822. dots[i].move(new S.Point({
  823. z: Math.random() * 20 + 10,
  824. a: Math.random(),
  825. h: 20
  826. }));
  827. dots[i].s = false;
  828. dots[i].e = 0.04;
  829. dots[i].move(new S.Point({
  830. x: Math.random() * a.w,
  831. y: Math.random() * a.h,
  832. a: 0.3, //.4
  833. z: Math.random() * 4,
  834. h: 0
  835. }));
  836. }
  837. }
  838. },
  839. render: function () {
  840. for (var d = 0; d < dots.length; d++) {
  841. dots[d].render();
  842. }
  843. }
  844. }
  845. }());
  846. S.init();
  847. </script>
  848. </body>
  849. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/85166
推荐阅读
相关标签
  

闽ICP备14008679号