用HTML5实现祝贺用3D动态烟花

一时失言乱红尘 2023-01-04 10:25 158阅读 0赞

用HTML5实现祝贺用3D动态烟花

将下列代码保存为html文件名,如:新年快乐3D动态烟花.HTML,用浏览器代开运行此文件,就可看到效果,当在浏览器(Microsoft Edge)打开时有礼花爆炸声响。演示图如下:

20210219113847570.gif

源码如下:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>烟花动画特效</title>
  6. <style>
  7. :root {
  8. font-family: "Aleo", sans-serif;
  9. color:red
  10. }
  11. html,
  12. body {
  13. width: 100%;
  14. height: 100%;
  15. padding: 0;
  16. margin: 0;
  17. background: rgb(119, 13, 13);
  18. }
  19. canvas {
  20. width: 100%;
  21. height: 100%;
  22. }
  23. .middle {
  24. position: absolute;
  25. top: 50%;
  26. left: 50%;
  27. transform: translate(-50%, -50%);
  28. text-align: center;
  29. user-select: none;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="middle">
  35. <h1 class="label">祝您快乐!万事如意 </h1>
  36. <h2 class="label">某某 恭祝 </h2>
  37. </div>
  38. <canvas id="canvas"></canvas><script>
  39. function initVars(){
  40. pi=Math.PI;
  41. ctx=canvas.getContext("2d");
  42. canvas.width=canvas.clientWidth;
  43. canvas.height=canvas.clientHeight;
  44. cx=canvas.width/2;
  45. cy=canvas.height/2;
  46. playerZ=-25;
  47. playerX=playerY=playerVX=playerVY=playerVZ=pitch=yaw=pitchV=yawV=0;
  48. scale=600;
  49. seedTimer=0;seedInterval=5,seedLife=100;gravity=.02;
  50. seeds=new Array();
  51. sparkPics=new Array();
  52. s="https://cantelope.org/NYE/";
  53. for(i=1;i<=10;++i){
  54. sparkPic=new Image();
  55. sparkPic.src=s+"spark"+i+".png";
  56. sparkPics.push(sparkPic);
  57. }
  58. sparks=new Array();
  59. pow1=new Audio(s+"pow1.ogg");
  60. pow2=new Audio(s+"pow2.ogg");
  61. pow3=new Audio(s+"pow3.ogg");
  62. pow4=new Audio(s+"pow4.ogg");
  63. frames = 0;
  64. }
  65. function rasterizePoint(x,y,z){
  66. var p,d;
  67. x-=playerX;
  68. y-=playerY;
  69. z-=playerZ;
  70. p=Math.atan2(x,z);
  71. d=Math.sqrt(x*x+z*z);
  72. x=Math.sin(p-yaw)*d;
  73. z=Math.cos(p-yaw)*d;
  74. p=Math.atan2(y,z);
  75. d=Math.sqrt(y*y+z*z);
  76. y=Math.sin(p-pitch)*d;
  77. z=Math.cos(p-pitch)*d;
  78. 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);
  79. if(!uc) return {x:0,y:0,d:-1};
  80. var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;
  81. var ub=((rx2-rx1)*(ry1-ry3)-(ry2-ry1)*(rx1-rx3))/uc;
  82. if(!z)z=.000000001;
  83. if(ua>0&&ua<1&&ub>0&&ub<1){
  84. return {
  85. x:cx+(rx1+ua*(rx2-rx1))*scale,
  86. y:cy+y/z*scale,
  87. d:Math.sqrt(x*x+y*y+z*z)
  88. };
  89. }else{
  90. return {
  91. x:cx+(rx1+ua*(rx2-rx1))*scale,
  92. y:cy+y/z*scale,
  93. d:-1
  94. };
  95. }
  96. }
  97. function spawnSeed(){
  98. seed=new Object();
  99. seed.x=-50+Math.random()*100;
  100. seed.y=25;
  101. seed.z=-50+Math.random()*100;
  102. seed.vx=.1-Math.random()*.2;
  103. seed.vy=-1.5;//*(1+Math.random()/2);
  104. seed.vz=.1-Math.random()*.2;
  105. seed.born=frames;
  106. seeds.push(seed);
  107. }
  108. function splode(x,y,z){
  109. t=5+parseInt(Math.random()*150);
  110. sparkV=1+Math.random()*2.5;
  111. type=parseInt(Math.random()*3);
  112. switch(type){
  113. case 0:
  114. pic1=parseInt(Math.random()*10);
  115. break;
  116. case 1:
  117. pic1=parseInt(Math.random()*10);
  118. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  119. break;
  120. case 2:
  121. pic1=parseInt(Math.random()*10);
  122. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  123. do{ pic3=parseInt(Math.random()*10); }while(pic3==pic1 || pic3==pic2);
  124. break;
  125. }
  126. for(m=1;m<t;++m){
  127. spark=new Object();
  128. spark.x=x; spark.y=y; spark.z=z;
  129. p1=pi*2*Math.random();
  130. p2=pi*Math.random();
  131. v=sparkV*(1+Math.random()/6)
  132. spark.vx=Math.sin(p1)*Math.sin(p2)*v;
  133. spark.vz=Math.cos(p1)*Math.sin(p2)*v;
  134. spark.vy=Math.cos(p2)*v;
  135. switch(type){
  136. case 0: spark.img=sparkPics[pic1]; break;
  137. case 1:
  138. spark.img=sparkPics[parseInt(Math.random()*2)?pic1:pic2];
  139. break;
  140. case 2:
  141. switch(parseInt(Math.random()*3)){
  142. case 0: spark.img=sparkPics[pic1]; break;
  143. case 1: spark.img=sparkPics[pic2]; break;
  144. case 2: spark.img=sparkPics[pic3]; break;
  145. }
  146. break;
  147. }
  148. spark.radius=25+Math.random()*50;
  149. spark.alpha=1;
  150. spark.trail=new Array();
  151. sparks.push(spark);
  152. }
  153. switch(parseInt(Math.random()*4)){
  154. case 0: pow=new Audio(s+"pow1.ogg"); break;
  155. case 1: pow=new Audio(s+"pow2.ogg"); break;
  156. case 2: pow=new Audio(s+"pow3.ogg"); break;
  157. case 3: pow=new Audio(s+"pow4.ogg"); break;
  158. }
  159. d=Math.sqrt((x-playerX)*(x-playerX)+(y-playerY)*(y-playerY)+(z-playerZ)*(z-playerZ));
  160. pow.volume=1.5/(1+d/10);
  161. pow.play();
  162. }
  163. function doLogic(){
  164. if(seedTimer<frames){
  165. seedTimer=frames+seedInterval*Math.random()*10;
  166. spawnSeed();
  167. }
  168. for(i=0;i<seeds.length;++i){
  169. seeds[i].vy+=gravity;
  170. seeds[i].x+=seeds[i].vx;
  171. seeds[i].y+=seeds[i].vy;
  172. seeds[i].z+=seeds[i].vz;
  173. if(frames-seeds[i].born>seedLife){
  174. splode(seeds[i].x,seeds[i].y,seeds[i].z);
  175. seeds.splice(i,1);
  176. }
  177. }
  178. for(i=0;i<sparks.length;++i){
  179. if(sparks[i].alpha>0 && sparks[i].radius>5){
  180. sparks[i].alpha-=.01;
  181. sparks[i].radius/=1.02;
  182. sparks[i].vy+=gravity;
  183. point=new Object();
  184. point.x=sparks[i].x;
  185. point.y=sparks[i].y;
  186. point.z=sparks[i].z;
  187. if(sparks[i].trail.length){
  188. x=sparks[i].trail[sparks[i].trail.length-1].x;
  189. y=sparks[i].trail[sparks[i].trail.length-1].y;
  190. z=sparks[i].trail[sparks[i].trail.length-1].z;
  191. d=((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y)+(point.z-z)*(point.z-z));
  192. if(d>9){
  193. sparks[i].trail.push(point);
  194. }
  195. }else{
  196. sparks[i].trail.push(point);
  197. }
  198. if(sparks[i].trail.length>5)sparks[i].trail.splice(0,1);
  199. sparks[i].x+=sparks[i].vx;
  200. sparks[i].y+=sparks[i].vy;
  201. sparks[i].z+=sparks[i].vz;
  202. sparks[i].vx/=1.075;
  203. sparks[i].vy/=1.075;
  204. sparks[i].vz/=1.075;
  205. }else{
  206. sparks.splice(i,1);
  207. }
  208. }
  209. p=Math.atan2(playerX,playerZ);
  210. d=Math.sqrt(playerX*playerX+playerZ*playerZ);
  211. d+=Math.sin(frames/80)/1.25;
  212. t=Math.sin(frames/200)/40;
  213. playerX=Math.sin(p+t)*d;
  214. playerZ=Math.cos(p+t)*d;
  215. yaw=pi+p+t;
  216. }
  217. function rgb(col){
  218. var r = parseInt((.5+Math.sin(col)*.5)*16);
  219. var g = parseInt((.5+Math.cos(col)*.5)*16);
  220. var b = parseInt((.5-Math.sin(col)*.5)*16);
  221. return "#"+r.toString(16)+g.toString(16)+b.toString(16);
  222. }
  223. function draw(){
  224. ctx.clearRect(0,0,cx*2,cy*2);
  225. ctx.fillStyle="#ff8";
  226. for(i=-100;i<100;i+=3){
  227. for(j=-100;j<100;j+=4){
  228. x=i;z=j;y=25;
  229. point=rasterizePoint(x,y,z);
  230. if(point.d!=-1){
  231. size=250/(1+point.d);
  232. d = Math.sqrt(x * x + z * z);
  233. a = 0.75 - Math.pow(d / 100, 6) * 0.75;
  234. if(a>0){
  235. ctx.globalAlpha = a;
  236. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  237. }
  238. }
  239. }
  240. }
  241. ctx.globalAlpha=1;
  242. for(i=0;i<seeds.length;++i){
  243. point=rasterizePoint(seeds[i].x,seeds[i].y,seeds[i].z);
  244. if(point.d!=-1){
  245. size=200/(1+point.d);
  246. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  247. }
  248. }
  249. point1=new Object();
  250. for(i=0;i<sparks.length;++i){
  251. point=rasterizePoint(sparks[i].x,sparks[i].y,sparks[i].z);
  252. if(point.d!=-1){
  253. size=sparks[i].radius*200/(1+point.d);
  254. if(sparks[i].alpha<0)sparks[i].alpha=0;
  255. if(sparks[i].trail.length){
  256. point1.x=point.x;
  257. point1.y=point.y;
  258. switch(sparks[i].img){
  259. case sparkPics[0]:ctx.strokeStyle="#f84";break;
  260. case sparkPics[1]:ctx.strokeStyle="#84f";break;
  261. case sparkPics[2]:ctx.strokeStyle="#8ff";break;
  262. case sparkPics[3]:ctx.strokeStyle="#fff";break;
  263. case sparkPics[4]:ctx.strokeStyle="#4f8";break;
  264. case sparkPics[5]:ctx.strokeStyle="#f44";break;
  265. case sparkPics[6]:ctx.strokeStyle="#f84";break;
  266. case sparkPics[7]:ctx.strokeStyle="#84f";break;
  267. case sparkPics[8]:ctx.strokeStyle="#fff";break;
  268. case sparkPics[9]:ctx.strokeStyle="#44f";break;
  269. }
  270. for(j=sparks[i].trail.length-1;j>=0;--j){
  271. point2=rasterizePoint(sparks[i].trail[j].x,sparks[i].trail[j].y,sparks[i].trail[j].z);
  272. if(point2.d!=-1){
  273. ctx.globalAlpha=j/sparks[i].trail.length*sparks[i].alpha/2;
  274. ctx.beginPath();
  275. ctx.moveTo(point1.x,point1.y);
  276. ctx.lineWidth=1+sparks[i].radius*10/(sparks[i].trail.length-j)/(1+point2.d);
  277. ctx.lineTo(point2.x,point2.y);
  278. ctx.stroke();
  279. point1.x=point2.x;
  280. point1.y=point2.y;
  281. }
  282. }
  283. }
  284. ctx.globalAlpha=sparks[i].alpha;
  285. ctx.drawImage(sparks[i].img,point.x-size/2,point.y-size/2,size,size);
  286. }
  287. }
  288. }
  289. function frame(){
  290. if(frames>100000){
  291. seedTimer=0;
  292. frames=0;
  293. }
  294. frames++;
  295. draw();
  296. doLogic();
  297. requestAnimationFrame(frame);
  298. }
  299. window.addEventListener("resize",()=>{
  300. canvas.width=canvas.clientWidth;
  301. canvas.height=canvas.clientHeight;
  302. cx=canvas.width/2;
  303. cy=canvas.height/2;
  304. });
  305. initVars();
  306. frame();
  307. </script>
  308. </body>
  309. </html>

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HTML语言写一个3D地球

    很抱歉,使用HTML语言创建3D地球是不可能的。HTML是一种用于创建网页的语言,其中的标签和属性主要用于呈现文本、图像、链接等二维内容,而不适用于创建三维内容。如果您希望创建

    相关 HTML和CSS构建3D世界

    使用HTML和CSS构建三维世界 去年,我做了一个程序示例,来展示CSS的三维转换功能如何被用于创建三维环境的。在当时,这个例子是一个关于通过CSS能够达到什么效果的示例