热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HTML53D衣服摇摆动画特效_html5教程技巧

这篇文章主要为大家分享了一款HTML53D衣服摇摆动画特效,一款基于HTML5Canvas的3D动画杰作,感兴趣的小伙伴们可以参考一下
这又是一款基于HTML5 Canvas的3D动画杰作,它是一个可以随风飘动的3D衣服摇摆动画特效,非常逼真。当我们将鼠标滑过衣服时,衣服将会出现摇摆的动画,点击鼠标时,衣服将会更加剧烈地摆动。

在线演示 源码下载

HTML代码

XML/HTML Code复制内容到剪贴板

  1. <p style="width:500px;margin:10px auto">
  2. <canvas id="cv" width="480" height="300">canvas>
  3. <p>"3D on 2D Canvas" demop>
  4. <p>move cursor to pan / click to swingp>
  5. p>

P3D库JS代码,主要用来处理3D效果的

Javascript Code复制内容到剪贴板

  1. window.P3D = {
  2. texture: null,
  3. g: null
  4. };
  5. P3D.clear = function(f, w, h) {
  6. var g = this.g;
  7. g.beginPath();
  8. g.fillStyle = f;
  9. g.fillRect(0, 0, w, h);
  10. }
  11. P3D.num_cmp = function(a,b){return a-b;}
  12. P3D.drawTriangle = function(poss, uvs, shade_clr) {
  13. var w = this.texture.width;
  14. var h = this.texture.height;
  15. var g = this.g;
  16. var vAd = [ poss[1].x - poss[0].x , poss[1].y - poss[0].y ];
  17. var vBd = [ poss[2].x - poss[0].x , poss[2].y - poss[0].y ];
  18. var vA = [ uvs[1].u - uvs[0].u , uvs[1].v - uvs[0].v ];
  19. var vB = [ uvs[2].u - uvs[0].u , uvs[2].v - uvs[0].v ];
  20. vA[0] *= w;
  21. vA[1] *= h;
  22. vB[0] *= w;
  23. vB[1] *= h;
  24. var m = new M22();
  25. m._11 = vA[0];
  26. m._12 = vA[1];
  27. m._21 = vB[0];
  28. m._22 = vB[1];
  29. var im = m.getInvert();
  30. if (!im) return false;
  31. var a = im._11 * vAd[0] + im._12 * vBd[0];
  32. var b = im._21 * vAd[0] + im._22 * vBd[0];
  33. var c = im._11 * vAd[1] + im._12 * vBd[1];
  34. var d = im._21 * vAd[1] + im._22 * vBd[1];
  35. var wu = uvs[0].u * w;
  36. var hv = uvs[0].v * h;
  37. var du = wu * a + hv * b;
  38. var dv = wu * c + hv * d;
  39. g.save();
  40. g.beginPath();
  41. g.moveTo(poss[0].x, poss[0].y);
  42. g.lineTo(poss[1].x, poss[1].y);
  43. g.lineTo(poss[2].x, poss[2].y);
  44. g.clip();
  45. g.transform(a, c, b, d, poss[0].x - du, poss[0].y - dv);
  46. // bounds
  47. var bx = [wu, wu+vA[0], wu+vB[0]];
  48. var by = [hv, hv+vA[1], hv+vB[1]];
  49. bx.sort(P3D.num_cmp);
  50. by.sort(P3D.num_cmp);
  51. var bw = bx[2] - bx[0];
  52. var bh = by[2] - by[0];
  53. if ((bx[0]+bw) <= (w-1)) bw++;
  54. if ((by[0]+bh) <= (h-1)) bh++;
  55. if (bx[0] >= 1) {bx[0]--; bw++;}
  56. if (by[0] >= 1) {by[0]--; bh++;}
  57. g.drawImage(this.texture, bx[0], by[0], bw, bh, bx[0], by[0], bw, bh);
  58. if (shade_clr) {
  59. g.fillStyle = shade_clr;
  60. g.fillRect(bx[0], by[0], bw, bh);
  61. }
  62. g.restore();
  63. return true;
  64. }
  65. P3D.drawTestByIndexBuffer = function(pos_buf, ix_buf, culling) {
  66. var g = this.g;
  67. if ((ix_buf.length%3) != 0)
  68. throw "invalid index buffer length!";
  69. var len = ix_buf.length/3;
  70. var i, ibase, vbase;
  71. var poss = [{},{},{}];
  72. g.strokeWidth = 1;
  73. for (i = 0, ibase = 0;i
  74. {
  75. vbase = ix_buf[ibase++] <<2;
  76. poss[0].x = pos_buf[vbase++];
  77. poss[0].y = pos_buf[vbase ];
  78. vbase = ix_buf[ibase++] <<2;
  79. poss[1].x = pos_buf[vbase++];
  80. poss[1].y = pos_buf[vbase ];
  81. vbase = ix_buf[ibase++] <<2;
  82. poss[2].x = pos_buf[vbase++];
  83. poss[2].y = pos_buf[vbase ];
  84. // z component of cross product <0 ?
  85. var Ax = poss[1].x - poss[0].x;
  86. var Ay = poss[1].y - poss[0].y;
  87. var Cx = poss[2].x - poss[1].x;
  88. var Cy = poss[2].y - poss[1].y;
  89. var cull = ( (((Ax * Cy) - (Ay * Cx))*culling) <0);
  90. g.beginPath();
  91. g.strokeStyle = cull ? "#592" : "#0f0";
  92. g.moveTo(poss[0].x, poss[0].y);
  93. g.lineTo(poss[1].x, poss[1].y);
  94. g.lineTo(poss[2].x, poss[2].y);
  95. g.lineTo(poss[0].x, poss[0].y);
  96. g.stroke();
  97. }
  98. }
  99. P3D.drawByIndexBuffer = function(pos_buf, ix_buf, tx_buf, culling, z_clip) {
  100. var w, h;
  101. var color_polygon = !this.texture;
  102. if (this.texture) {
  103. w = this.texture.width;
  104. h = this.texture.height;
  105. }
  106. var g = this.g;
  107. var m = new M22();
  108. if (!culling) culling = 0;
  109. if ((ix_buf.length%3) != 0)
  110. throw "invalid index buffer length!";
  111. var i, ibase, vbase, tbase, poss = [{},{},{}];
  112. var len = ix_buf.length/3;
  113. var uv_0u, uv_0v, uv_1u, uv_1v, uv_2u, uv_2v;
  114. for (i = 0, ibase = 0;i
  115. {
  116. tbase = ix_buf[ibase++] <<1
  117. vbase = tbase <<1;
  118. poss[0].x = pos_buf[vbase++]; uv_0u = tx_buf[tbase++];
  119. poss[0].y = pos_buf[vbase++]; uv_0v = tx_buf[tbase];
  120. if (z_clip && (pos_buf[vbase] <0 || pos_buf[vbase] > 1)) {ibase += 2; continue;}
  121. tbase = ix_buf[ibase++] <<1
  122. vbase = tbase <<1;
  123. poss[1].x = pos_buf[vbase++]; uv_1u = tx_buf[tbase++];
  124. poss[1].y = pos_buf[vbase++]; uv_1v = tx_buf[tbase];
  125. if (z_clip && (pos_buf[vbase] <0 || pos_buf[vbase] > 1)) {++ibase; continue;}
  126. tbase = ix_buf[ibase++] <<1
  127. vbase = tbase <<1;
  128. poss[2].x = pos_buf[vbase++]; uv_2u = tx_buf[tbase++];
  129. poss[2].y = pos_buf[vbase++]; uv_2v = tx_buf[tbase];
  130. if (z_clip && (pos_buf[vbase] <0 || pos_buf[vbase] > 1)) {continue;}
  131. var vAd = [ poss[1].x - poss[0].x , poss[1].y - poss[0].y ];
  132. var vBd = [ poss[2].x - poss[0].x , poss[2].y - poss[0].y ];
  133. var vCd = [ poss[2].x - poss[1].x , poss[2].y - poss[1].y ];
  134. // z component of cross product <0 ?
  135. if( (((vAd[0] * vCd[1]) - (vAd[1] * vCd[0]))*culling) <0)
  136. continue;
  137. if (color_polygon) {
  138. g.fillStyle = uv_0u;
  139. g.beginPath();
  140. g.moveTo(poss[0].x, poss[0].y);
  141. g.lineTo(poss[1].x, poss[1].y);
  142. g.lineTo(poss[2].x, poss[2].y);
  143. g.fill();
  144. continue;
  145. }
  146. var vA = [ uv_1u - uv_0u , uv_1v - uv_0v ];
  147. var vB = [ uv_2u - uv_0u , uv_2v - uv_0v ];
  148. vA[0] *= w;
  149. vA[1] *= h;
  150. vB[0] *= w;
  151. vB[1] *= h;
  152. m._11 = vA[0];
  153. m._12 = vA[1];
  154. m._21 = vB[0];
  155. m._22 = vB[1];
  156. var im = m.getInvert();
  157. if (!im) { continue;}
  158. var a = im._11 * vAd[0] + im._12 * vBd[0];
  159. var b = im._21 * vAd[0] + im._22 * vBd[0];
  160. var c = im._11 * vAd[1] + im._12 * vBd[1];
  161. var d = im._21 * vAd[1] + im._22 * vBd[1];
  162. var wu = uv_0u * w;
  163. var hv = uv_0v * h;
  164. var du = wu * a + hv * b;
  165. var dv = wu * c + hv * d;
  166. g.save();
  167. g.beginPath();
  168. g.moveTo(poss[0].x, poss[0].y);
  169. g.lineTo(poss[1].x, poss[1].y);
  170. g.lineTo(poss[2].x, poss[2].y);
  171. g.clip();
  172. g.transform(a, c, b, d, poss[0].x - du, poss[0].y - dv);
  173. // bounds
  174. var bx = [wu, wu+vA[0], wu+vB[0]];
  175. var by = [hv, hv+vA[1], hv+vB[1]];
  176. bx.sort(P3D.num_cmp);
  177. by.sort(P3D.num_cmp);
  178. var bw = bx[2] - bx[0];
  179. var bh = by[2] - by[0];
  180. if ((bx[0]+bw) <= (w-1)) bw++;
  181. if ((by[0]+bh) <= (h-1)) bh++;
  182. if (bx[0] >= 1) {bx[0]--; bw++;}
  183. if (by[0] >= 1) {by[0]--; bh++;}
  184. g.drawImage(this.texture, bx[0], by[0], bw, bh, bx[0], by[0], bw, bh);
  185. /*
  186. if (shade_clr) {
  187. g.fillStyle = shade_clr;
  188. g.fillRect(bx[0], by[0], bw, bh);
  189. }
  190. */
  191. g.restore();
  192. }
  193. }
  194. function Vec3(_x, _y, _z)
  195. {
  196. this.x = _x || 0;
  197. this.y = _y || 0;
  198. this.z = _z || 0;
  199. }
  200. Vec3.prototype = {
  201. zero: function() {
  202. this.x = this.y = this.z = 0;
  203. },
  204. sub: function(v) {
  205. this.x -= v.x;
  206. this.y -= v.y;
  207. this.z -= v.z;
  208. return this;
  209. },
  210. add: function(v) {
  211. this.x += v.x;
  212. this.y += v.y;
  213. this.z += v.z;
  214. return this;
  215. },
  216. copyFrom: function(v) {
  217. this.x = v.x;
  218. this.y = v.y;
  219. this.z = v.z;
  220. return this;
  221. },
  222. norm:function() {
  223. return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
  224. },
  225. normalize: function() {
  226. var nrm = Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
  227. if (nrm != 0)
  228. {
  229. this.x /= nrm;
  230. this.y /= nrm;
  231. this.z /= nrm;
  232. }
  233. return this;
  234. },
  235. smul: function(k) {
  236. this.x *= k;
  237. this.y *= k;
  238. this.z *= k;
  239. return this;
  240. },
  241. dpWith: function(v) {
  242. return this.x*v.x + this.y*v.y + this.z*v.z;
  243. },
  244. cp: function(v, w) {
  245. this.x = (w.y * v.z) - (w.z * v.y);
  246. this.y = (w.z * v.x) - (w.x * v.z);
  247. this.z = (w.x * v.y) - (w.y * v.x);
  248. return this;
  249. },
  250. toString: function() {
  251. return this.x + ", " + this.y + "," + this.z;
  252. }
  253. }
  254. function M44(cpy)
  255. {
  256. if (cpy)
  257. this.copyFrom(cpy);
  258. else {
  259. this.ident();
  260. }
  261. }
  262. M44.prototype = {
  263. ident: function() {
  264. this._12 = this._13 = this._14 = 0;
  265. this._21 = this._23 = this._24 = 0;
  266. this._31 = this._32 = this._34 = 0;
  267. this._41 = this._42 = this._43 = 0;
  268. this._11 = this._22 = this._33 = this._44 = 1;
  269. return this;
  270. },
  271. copyFrom: function(m) {
  272. this._11 = m._11;
  273. this._12 = m._12;
  274. this._13 = m._13;
  275. this._14 = m._14;
  276. this._21 = m._21;
  277. this._22 = m._22;
  278. this._23 = m._23;
  279. this._24 = m._24;
  280. this._31 = m._31;
  281. this._32 = m._32;
  282. this._33 = m._33;
  283. this._34 = m._34;
  284. this._41 = m._41;
  285. this._42 = m._42;
  286. this._43 = m._43;
  287. this._44 = m._44;
  288. return this;
  289. },
  290. transVec3: function(out, x, y, z) {
  291. out[0] = x * this._11 + y * this._21 + z * this._31 + this._41;
  292. out[1] = x * this._12 + y * this._22 + z * this._32 + this._42;
  293. out[2] = x * this._13 + y * this._23 + z * this._33 + this._43;
  294. out[3] = x * this._14 + y * this._24 + z * this._34 + this._44;
  295. },
  296. transVec3Rot: function(out, x, y, z) {
  297. out[0] = x * this._11 + y * this._21 + z * this._31;
  298. out[1] = x * this._12 + y * this._22 + z * this._32;
  299. out[2] = x * this._13 + y * this._23 + z * this._33;
  300. },
  301. perspectiveLH: function(vw, vh, z_near, z_far) {
  302. this._11 = 2.0*z_near/vw;
  303. this._12 = 0;
  304. this._13 = 0;
  305. this._14 = 0;
  306. this._21 = 0;
  307. this._22 = 2*z_near/vh;
  308. this._23 = 0;
  309. this._24 = 0;
  310. this._31 = 0;
  311. this._32 = 0;
  312. this._33 = z_far/(z_far-z_near);
  313. this._34 = 1;
  314. this._41 = 0;
  315. this._42 = 0;
  316. this._43 = z_near*z_far/(z_near-z_far);
  317. this._44 = 0;
  318. return this;
  319. },
  320. lookAtLH: function(aUp, aFrom, aAt) {
  321. var aX = new Vec3();
  322. var aY = new Vec3();
  323. var aZ = new Vec3(aAt.x, aAt.y, aAt.z);
  324. aZ.sub(aFrom).normalize();
  325. aX.cp(aUp, aZ).normalize();
  326. aY.cp(aZ, aX);
  327. this._11 = aX.x; this._12 = aY.x; this._13 = aZ.x; this._14 = 0;
  328. this._21 = aX.y; this._22 = aY.y; this._23 = aZ.y; this._24 = 0;
  329. this._31 = aX.z; this._32 = aY.z; this._33 = aZ.z; this._34 = 0;
  330. this._41 = -aFrom.dpWith(aX);
  331. this._42 = -aFrom.dpWith(aY);
  332. this._43 = -aFrom.dpWith(aZ);
  333. this._44 = 1;
  334. return this;
  335. },
  336. mul: function(A, B) {
  337. this._11 = A._11*B._11 + A._12*B._21 + A._13*B._31 + A._14*B._41;
  338. this._12 = A._11*B._12 + A._12*B._22 + A._13*B._32 + A._14*B._42;
  339. this._13 = A._11*B._13 + A._12*B._23 + A._13*B._33 + A._14*B._43;
  340. this._14 = A._11*B._14 + A._12*B._24 + A._13*B._34 + A._14*B._44;
  341. this._21 = A._21*B._11 + A._22*B._21 + A._23*B._31 + A._24*B._41;
  342. this._22 = A._21*B._12 + A._22*B._22 + A._23*B._32 + A._24*B._42;
  343. this._23 = A._21*B._13 + A._22*B._23 + A._23*B._33 + A._24*B._43;
  344. this._24 = A._21*B._14 + A._22*B._24 + A._23*B._34 + A._24*B._44;
  345. this._31 = A._31*B._11 + A._32*B._21 + A._33*B._31 + A._34*B._41;
  346. this._32 = A._31*B._12 + A._32*B._22 + A._33*B._32 + A._34*B._42;
  347. this._33 = A._31*B._13 + A._32*B._23 + A._33*B._33 + A._34*B._43;
  348. this._34 = A._31*B._14 + A._32*B._24 + A._33*B._34 + A._34*B._44;
  349. this._41 = A._41*B._11 + A._42*B._21 + A._43*B._31 + A._44*B._41;
  350. this._42 = A._41*B._12 + A._42*B._22 + A._43*B._32 + A._44*B._42;
  351. this._43 = A._41*B._13 + A._42*B._23 + A._43*B._33 + A._44*B._43;
  352. this._44 = A._41*B._14 + A._42*B._24 + A._43*B._34 + A._44*B._44;
  353. return this;
  354. },
  355. translate: function(x, y, z) {
  356. this._11 = 1; this._12 = 0; this._13 = 0; this._14 = 0;
  357. this._21 = 0; this._22 = 1; this._23 = 0; this._24 = 0;
  358. this._31 = 0; this._32 = 0; this._33 = 1; this._34 = 0;
  359. this._41 = x; this._42 = y; this._43 = z; this._44 = 1;
  360. return this;
  361. },
  362. transpose33: function() {
  363. var t;
  364. t = this._12;
  365. this._12 = this._21;
  366. this._21 = t;
  367. t = this._13;
  368. this._13 = this._31;
  369. this._31 = t;
  370. t = this._23;
  371. this._23 = this._32;
  372. this._32 = t;
  373. return this;
  374. },
  375. // OpenGL style rotation
  376. glRotate: function(angle, x, y, z) {
  377. var s = Math.sin( angle );
  378. var c = Math.cos( angle );
  379. var xx = x * x;
  380. var yy = y * y;
  381. var zz = z * z;
  382. var xy = x * y;
  383. var yz = y * z;
  384. var zx = z * x;
  385. var xs = x * s;
  386. var ys = y * s;
  387. var zs = z * s;
  388. var one_c = 1.0 - c;
  389. /*
  390. this._11 = (one_c * xx) + c;
  391. this._21 = (one_c * xy) - zs;
  392. this._31 = (one_c * zx) + ys;
  393. this._41 = 0;
  394. this._12 = (one_c * xy) + zs;
  395. this._22 = (one_c * yy) + c;
  396. this._32 = (one_c * yz) - xs;
  397. this._42 = 0;
  398. this._13 = (one_c * zx) - ys;
  399. this._23 = (one_c * yz) + xs;
  400. this._33 = (one_c * zz) + c;
  401. this._43 = 0;
推荐阅读
author-avatar
钢铁猪991884679
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有