当前位置:   article > 正文

H5画布canvas特效(旋转跳跃不停歇)_h5 canvas 加载gif 后左右跳

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/80865
推荐阅读
  

闽ICP备14008679号