例子特效

  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. #canvas{
  15. position:absolute;
  16. width:100%;
  17. height:100%;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <canvas id="canvas"></canvas>
  23. <script>
  24. function project3D(x,y,z,vars){
  25. var p,d;
  26. x-=vars.camX;
  27. y-=vars.camY-8;
  28. z-=vars.camZ;
  29. p=Math.atan2(x,z);
  30. d=Math.sqrt(x*x+z*z);
  31. x=Math.sin(p-vars.yaw)*d;
  32. z=Math.cos(p-vars.yaw)*d;
  33. p=Math.atan2(y,z);
  34. d=Math.sqrt(y*y+z*z);
  35. y=Math.sin(p-vars.pitch)*d;
  36. z=Math.cos(p-vars.pitch)*d;
  37. var rx1=-1000;
  38. var ry1=1;
  39. var rx2=1000;
  40. var ry2=1;
  41. var rx3=0;
  42. var ry3=0;
  43. var rx4=x;
  44. var ry4=z;
  45. var uc=(ry4-ry3)*(rx2-rx1)-(rx4-rx3)*(ry2-ry1);
  46. var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;
  47. var ub=((rx2-rx1)*(ry1-ry3)-(ry2-ry1)*(rx1-rx3))/uc;
  48. if(!z)z=0.000000001;
  49. if(ua>0&&ua<1&&ub>0&&ub<1){
  50. return {
  51. x:vars.cx+(rx1+ua*(rx2-rx1))*vars.scale,
  52. y:vars.cy+y/z*vars.scale,
  53. d:(x*x+y*y+z*z)
  54. };
  55. }else{
  56. return { d:-1 };
  57. }
  58. }
  59. function elevation(x,y,z){
  60. var dist = Math.sqrt(x*x+y*y+z*z);
  61. if(dist && z/dist>=-1 && z/dist <=1) return Math.acos(z / dist);
  62. return 0.00000001;
  63. }
  64. function rgb(col){
  65. col += 0.000001;
  66. var r = parseInt((0.5+Math.sin(col)*0.5)*16);
  67. var g = parseInt((0.5+Math.cos(col)*0.5)*16);
  68. var b = parseInt((0.5-Math.sin(col)*0.5)*16);
  69. return "#"+r.toString(16)+g.toString(16)+b.toString(16);
  70. }
  71. function interpolateColors(RGB1,RGB2,degree){
  72. var w2=degree;
  73. var w1=1-w2;
  74. return [w1*RGB1[0]+w2*RGB2[0],w1*RGB1[1]+w2*RGB2[1],w1*RGB1[2]+w2*RGB2[2]];
  75. }
  76. function rgbArray(col){
  77. col += 0.000001;
  78. var r = parseInt((0.5+Math.sin(col)*0.5)*256);
  79. var g = parseInt((0.5+Math.cos(col)*0.5)*256);
  80. var b = parseInt((0.5-Math.sin(col)*0.5)*256);
  81. return [r, g, b];
  82. }
  83. function colorString(arr){
  84. var r = parseInt(arr[0]);
  85. var g = parseInt(arr[1]);
  86. var b = parseInt(arr[2]);
  87. return "#"+("0" + r.toString(16) ).slice (-2)+("0" + g.toString(16) ).slice (-2)+("0" + b.toString(16) ).slice (-2);
  88. }
  89. function process(vars){
  90. if(vars.points.length<vars.initParticles) for(var i=0;i<5;++i) spawnParticle(vars);
  91. var p,d,t;
  92. p = Math.atan2(vars.camX, vars.camZ);
  93. d = Math.sqrt(vars.camX * vars.camX + vars.camZ * vars.camZ);
  94. d -= Math.sin(vars.frameNo / 80) / 25;
  95. t = Math.cos(vars.frameNo / 300) / 165;
  96. vars.camX = Math.sin(p + t) * d;
  97. vars.camZ = Math.cos(p + t) * d;
  98. vars.camY = -Math.sin(vars.frameNo / 220) * 15;
  99. vars.yaw = Math.PI + p + t;
  100. vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2;
  101. var t;
  102. for(var i=0;i<vars.points.length;++i){
  103. x=vars.points[i].x;
  104. y=vars.points[i].y;
  105. z=vars.points[i].z;
  106. d=Math.sqrt(x*x+z*z)/1.0075;
  107. t=.1/(1+d*d/5);
  108. p=Math.atan2(x,z)+t;
  109. vars.points[i].x=Math.sin(p)*d;
  110. vars.points[i].z=Math.cos(p)*d;
  111. vars.points[i].y+=vars.points[i].vy*t*((Math.sqrt(vars.distributionRadius)-d)*2);
  112. if(vars.points[i].y>vars.vortexHeight/2 || d<.25){
  113. vars.points.splice(i,1);
  114. spawnParticle(vars);
  115. }
  116. }
  117. }
  118. function drawFloor(vars){
  119. var x,y,z,d,point,a;
  120. for (var i = -25; i <= 25; i += 1) {
  121. for (var j = -25; j <= 25; j += 1) {
  122. x = i*2;
  123. z = j*2;
  124. y = vars.floor;
  125. d = Math.sqrt(x * x + z * z);
  126. point = project3D(x, y-d*d/85, z, vars);
  127. if (point.d != -1) {
  128. size = 1 + 15000 / (1 + point.d);
  129. a = 0.15 - Math.pow(d / 50, 4) * 0.15;
  130. if (a > 0) {
  131. vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(d/26-vars.frameNo/40),[0,128,32],.5+Math.sin(d/6-vars.frameNo/8)/2));
  132. vars.ctx.globalAlpha = a;
  133. vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  134. }
  135. }
  136. }
  137. }
  138. vars.ctx.fillStyle = "#82f";
  139. for (var i = -25; i <= 25; i += 1) {
  140. for (var j = -25; j <= 25; j += 1) {
  141. x = i*2;
  142. z = j*2;
  143. y = -vars.floor;
  144. d = Math.sqrt(x * x + z * z);
  145. point = project3D(x, y+d*d/85, z, vars);
  146. if (point.d != -1) {
  147. size = 1 + 15000 / (1 + point.d);
  148. a = 0.15 - Math.pow(d / 50, 4) * 0.15;
  149. if (a > 0) {
  150. vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(-d/26-vars.frameNo/40),[32,0,128],.5+Math.sin(-d/6-vars.frameNo/8)/2));
  151. vars.ctx.globalAlpha = a;
  152. vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. function sortFunction(a,b){
  159. return b.dist-a.dist;
  160. }
  161. function draw(vars){
  162. vars.ctx.globalAlpha=.15;
  163. vars.ctx.fillStyle="#000";
  164. vars.ctx.fillRect(0, 0, canvas.width, canvas.height);
  165. drawFloor(vars);
  166. var point,x,y,z,a;
  167. for(var i=0;i<vars.points.length;++i){
  168. x=vars.points[i].x;
  169. y=vars.points[i].y;
  170. z=vars.points[i].z;
  171. point=project3D(x,y,z,vars);
  172. if(point.d != -1){
  173. vars.points[i].dist=point.d;
  174. size=1+vars.points[i].radius/(1+point.d);
  175. d=Math.abs(vars.points[i].y);
  176. a = .8 - Math.pow(d / (vars.vortexHeight/2), 1000) * .8;
  177. vars.ctx.globalAlpha=a>=0&&a<=1?a:0;
  178. vars.ctx.fillStyle=rgb(vars.points[i].color);
  179. if(point.x>-1&&point.x<vars.canvas.width&&point.y>-1&&point.y<vars.canvas.height)vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  180. }
  181. }
  182. vars.points.sort(sortFunction);
  183. }
  184. function spawnParticle(vars){
  185. var p,ls;
  186. pt={};
  187. p=Math.PI*2*Math.random();
  188. ls=Math.sqrt(Math.random()*vars.distributionRadius);
  189. pt.x=Math.sin(p)*ls;
  190. pt.y=-vars.vortexHeight/2;
  191. pt.vy=vars.initV/20+Math.random()*vars.initV;
  192. pt.z=Math.cos(p)*ls;
  193. pt.radius=200+800*Math.random();
  194. pt.color=pt.radius/1000+vars.frameNo/250;
  195. vars.points.push(pt);
  196. }
  197. function frame(vars) {
  198. if(vars === undefined){
  199. var vars={};
  200. vars.canvas = document.querySelector("canvas");
  201. vars.ctx = vars.canvas.getContext("2d");
  202. vars.canvas.width = document.body.clientWidth;
  203. vars.canvas.height = document.body.clientHeight;
  204. window.addEventListener("resize", function(){
  205. vars.canvas.width = document.body.clientWidth;
  206. vars.canvas.height = document.body.clientHeight;
  207. vars.cx=vars.canvas.width/2;
  208. vars.cy=vars.canvas.height/2;
  209. }, true);
  210. vars.frameNo=0;
  211. vars.camX = 0;
  212. vars.camY = 0;
  213. vars.camZ = -14;
  214. vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2;
  215. vars.yaw = 0;
  216. vars.cx=vars.canvas.width/2;
  217. vars.cy=vars.canvas.height/2;
  218. vars.bounding=10;
  219. vars.scale=500;
  220. vars.floor=26.5;
  221. vars.points=[];
  222. vars.initParticles=700;
  223. vars.initV=.01;
  224. vars.distributionRadius=800;
  225. vars.vortexHeight=25;
  226. }
  227. vars.frameNo++;
  228. requestAnimationFrame(function() {
  229. frame(vars);
  230. });
  231. process(vars);
  232. draw(vars);
  233. }
  234. frame();
  235. </script>
  236. </body>
  237. </html>