HTML5 3D衣服摇摆动画特效_html5教程技巧

WBOY
Release: 2016-05-16 15:45:42
Original
1902 people have browsed it

这又是一款基于HTML5 Canvas的3D动画杰作,它是一个可以随风飘动的3D衣服摇摆动画特效,非常逼真。当我们将鼠标滑过衣服时,衣服将会出现摇摆的动画,点击鼠标时,衣服将会更加剧烈地摆动。

在线演示源码下载

HTML代码

XML/HTML Code 复制内容到剪贴板
  1. divstyle="width:500px;margin:10px auto">
  2. canvasid="cv"width="480"height="300">canvas>
  3. p>"3D on 2D Canvas" demop>
  4. p>move cursor to pan / click to swingp>
  5. div>

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

JavaScript Code 复制内容到剪贴板
  1. window.P3D = {
  2. texture:null,
  3. g:null
  4. };
  5. P3D.clear =function(f, w, h) {
  6. varg =this.g;
  7. g.beginPath();
  8. g.fillStyle = f;
  9. g.fillRect(0, 0, w, h);
  10. }
  11. P3D.num_cmp =function(a,b){returna-b;}
  12. P3D.drawTriangle =function(poss, uvs, shade_clr) {
  13. varw =this.texture.width;
  14. varh =this.texture.height;
  15. varg =this.g;
  16. varvAd = [ poss[1].x - poss[0].x , poss[1].y - poss[0].y ];
  17. varvBd = [ poss[2].x - poss[0].x , poss[2].y - poss[0].y ];
  18. varvA = [ uvs[1].u - uvs[0].u , uvs[1].v - uvs[0].v ];
  19. varvB = [ 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. varm =newM22();
  25. m._11 = vA[0];
  26. m._12 = vA[1];
  27. m._21 = vB[0];
  28. m._22 = vB[1];
  29. varim = m.getInvert();
  30. if(!im)returnfalse;
  31. vara = im._11 * vAd[0] + im._12 * vBd[0];
  32. varb = im._21 * vAd[0] + im._22 * vBd[0];
  33. varc = im._11 * vAd[1] + im._12 * vBd[1];
  34. vard = im._21 * vAd[1] + im._22 * vBd[1];
  35. varwu = uvs[0].u * w;
  36. varhv = uvs[0].v * h;
  37. vardu = wu * a + hv * b;
  38. vardv = 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. varbx = [wu, wu+vA[0], wu+vB[0]];
  48. varby = [hv, hv+vA[1], hv+vB[1]];
  49. bx.sort(P3D.num_cmp);
  50. by.sort(P3D.num_cmp);
  51. varbw = bx[2] - bx[0];
  52. varbh = by[2] - by[0];
  53. if((bx[0]+bw)
  54. if((by[0]+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. returntrue;
  64. }
  65. P3D.drawTestByIndexBuffer =function(pos_buf, ix_buf, culling) {
  66. varg =this.g;
  67. if((ix_buf.length%3) != 0)
  68. throw"invalid index buffer length!";
  69. varlen = ix_buf.length/3;
  70. vari, ibase, vbase;
  71. varposs = [{},{},{}];
  72. g.strokeWidth = 1;
  73. for(i = 0, ibase = 0;i
  74. {
  75. vbase = ix_buf[ibase++]
  76. poss[0].x = pos_buf[vbase++];
  77. poss[0].y = pos_buf[vbase ];
  78. vbase = ix_buf[ibase++]
  79. poss[1].x = pos_buf[vbase++];
  80. poss[1].y = pos_buf[vbase ];
  81. vbase = ix_buf[ibase++]
  82. poss[2].x = pos_buf[vbase++];
  83. poss[2].y = pos_buf[vbase ];
  84. // z component of cross product
  85. varAx = poss[1].x - poss[0].x;
  86. varAy = poss[1].y - poss[0].y;
  87. varCx = poss[2].x - poss[1].x;
  88. varCy = poss[2].y - poss[1].y;
  89. varcull = ( (((Ax * Cy) - (Ay * Cx))*culling)
  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. varw, h;
  101. varcolor_polygon = !this.texture;
  102. if(this.texture) {
  103. w =this.texture.width;
  104. h =this.texture.height;
  105. }
  106. varg =this.g;
  107. varm =newM22();
  108. if(!culling) culling = 0;
  109. if((ix_buf.length%3) != 0)
  110. throw"invalid index buffer length!";
  111. vari, ibase, vbase, tbase, poss = [{},{},{}];
  112. varlen = ix_buf.length/3;
  113. varuv_0u, uv_0v, uv_1u, uv_1v, uv_2u, uv_2v;
  114. for(i = 0, ibase = 0;i
  115. {
  116. tbase = ix_buf[ibase++]
  117. vbase = tbase
  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] 1)) {ibase += 2;continue;}
  121. tbase = ix_buf[ibase++]
  122. vbase = tbase
  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] 1)) {++ibase;continue;}
  126. tbase = ix_buf[ibase++]
  127. vbase = tbase
  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] 1)) {continue;}
  131. varvAd = [ poss[1].x - poss[0].x , poss[1].y - poss[0].y ];
  132. varvBd = [ poss[2].x - poss[0].x , poss[2].y - poss[0].y ];
  133. varvCd = [ poss[2].x - poss[1].x , poss[2].y - poss[1].y ];
  134. // z component of cross product
  135. if( (((vAd[0] * vCd[1]) - (vAd[1] * vCd[0]))*culling)
  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. varvA = [ uv_1u - uv_0u , uv_1v - uv_0v ];
  147. varvB = [ 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. varim = m.getInvert();
  157. if(!im) {continue;}
  158. vara = im._11 * vAd[0] + im._12 * vBd[0];
  159. varb = im._21 * vAd[0] + im._22 * vBd[0];
  160. varc = im._11 * vAd[1] + im._12 * vBd[1];
  161. vard = im._21 * vAd[1] + im._22 * vBd[1];
  162. varwu = uv_0u * w;
  163. varhv = uv_0v * h;
  164. vardu = wu * a + hv * b;
  165. vardv = 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. varbx = [wu, wu+vA[0], wu+vB[0]];
  175. varby = [hv, hv+vA[1], hv+vB[1]];
  176. bx.sort(P3D.num_cmp);
  177. by.sort(P3D.num_cmp);
  178. varbw = bx[2] - bx[0];
  179. varbh = by[2] - by[0];
  180. if((bx[0]+bw)
  181. if((by[0]+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. functionVec3(_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. returnthis;
  209. },
  210. add:function(v) {
  211. this.x += v.x;
  212. this.y += v.y;
  213. this.z += v.z;
  214. returnthis;
  215. },
  216. copyFrom:function(v) {
  217. this.x = v.x;
  218. this.y = v.y;
  219. this.z = v.z;
  220. returnthis;
  221. },
  222. norm:function() {
  223. returnMath.sqrt(this.x*this.x +this.y*this.y +this.z*this.z);
  224. },
  225. normalize:function() {
  226. varnrm = 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. returnthis;
  234. },
  235. smul:function(k) {
  236. this.x *= k;
  237. this.y *= k;
  238. this.z *= k;
  239. returnthis;
  240. },
  241. dpWith:function(v) {
  242. returnthis.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. returnthis;
  249. },
  250. toString:function() {
  251. returnthis.x +", "+this.y +","+this.z;
  252. }
  253. }
  254. functionM44(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. returnthis;
  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. returnthis;
  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. returnthis;
  319. },
  320. lookAtLH:function(aUp, aFrom, aAt) {
  321. varaX =newVec3();
  322. varaY =newVec3();
  323. varaZ =newVec3(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. returnthis;
  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. returnthis;
  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. returnthis;
  361. },
  362. transpose33:function() {
  363. vart;
  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. returnthis;
  374. },
  375. // OpenGL style rotation
  376. glRotate:function(angle, x, y, z) {
  377. vars = Math.sin( angle );
  378. varc = Math.cos( angle );
  379. varxx = x * x;
  380. varyy = y * y;
  381. varzz = z * z;
  382. varxy = x * y;
  383. varyz = y * z;
  384. varzx = z * x;
  385. varxs = x * s;
  386. varys = y * s;
  387. varzs = z * s;
  388. varone_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;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!