多视角3D可旋转的HTML5 Logo动画

事业上得寸进尺,生活中不论短长,何愁事业无成。人生最精彩的不是实现梦想的瞬间,而是坚持梦想的过程。

这是一款基于HTML5和CSS3的3D动画特效,与之前的3D特效不同的是,这款是完全用HTML5特性实现的,而并非用多张图片叠加起来。这款3D动画还有一个特点是可以无限的旋转,从而可以从多个视角来观察HTML5 Logo。

在线演示 源码下载

HTML代码

XML/HTML Code复制内容到剪贴板
  1. <divclass="link-overlay">
  2. </div>
  3. <canvasid="canvas"style="background-color:#ddd"></canvas>

JavaScript代码

JavaScript Code复制内容到剪贴板
  1. //bindtowindowonloadhandler
  2. window.addEventListener('load',onloadHandler,false);
  3. /**
  4. *Windowonloadhandler
  5. */
  6. functiononloadHandler()
  7. {
  8. varcanvas=document.getElementById('canvas');
  9. canvas.width=window.innerWidth;
  10. canvas.height=window.innerHeight;
  11. vark3dmain=newK3D.Controller(canvas,true);
  12. //generate3Dobjects
  13. varobj1=newK3D.K3DObject();
  14. with(obj1)
  15. {
  16. drawmode="solid";
  17. shademode="lightsource";
  18. sortmode="unsorted";
  19. addphi=-0.5;
  20. abouty=-90;
  21. perslevel=1000;
  22. init(
  23. [{x:-80,y:180,z:0},{x:0,y:180,z:-80},{x:0,y:0,z:-80},{x:-80,y:20,z:0},{x:-50,y:150,z:-30},{x:0,y:150,z:-80},{x:0,y:130,z:-80},{x:-30,y:130,z:-50},{x:-28,y:110,z:-52},{x:0,y:110,z:-80},{x:0,y:90,z:-80},{x:-45,y:90,z:-35},{x:-44,y:80,z:-36},{x:-25,y:80,z:-55},{x:-22,y:66,z:-58},{x:0,y:60,z:-80},{x:0,y:40,z:-80},{x:-40,y:50,z:-40}],
  24. [],
  25. [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[235,235,235],vertices:[4,5,6,7,8,9,10,11,4]},{color:[235,235,235],vertices:[12,13,14,15,16,17,12]}]
  26. );
  27. }
  28. k3dmain.addK3DObject(obj1);
  29. varobj2=newK3D.K3DObject();
  30. with(obj2)
  31. {
  32. drawmode="solid";
  33. shademode="lightsource";
  34. sortmode="unsorted";
  35. addphi=-0.5;
  36. abouty=-90;
  37. perslevel=1000;
  38. init(
  39. [{x:0,y:180,z:-80},{x:80,y:180,z:0},{x:80,y:20,z:0},{x:0,y:0,z:-80},{x:0,y:165,z:-80},{x:68,y:165,z:-12},{x:55,y:35,z:-25},{x:0,y:20,z:-80},{x:0,y:150,z:-80},{x:50,y:150,z:-30},{x:47,y:130,z:-33},{x:0,y:130,z:-80},{x:0,y:110,z:-80},{x:45,y:110,z:-35},{x:40,y:50,z:-40},{x:0,y:40,z:-80},{x:0,y:60,z:-80},{x:20,y:66,z:-60},{x:23,y:90,z:-57},{x:0,y:90,z:-80}],
  40. [],
  41. [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[240,101,41],vertices:[4,5,6,7,4]},{color:[235,235,235],vertices:[8,9,10,11,8]},{color:[235,235,235],vertices:[12,13,14,15,16,17,18,19,12]}]
  42. );
  43. }
  44. k3dmain.addK3DObject(obj2);
  45. varobj3=newK3D.K3DObject();
  46. with(obj3)
  47. {
  48. drawmode="solid";
  49. shademode="lightsource";
  50. sortmode="unsorted";
  51. addphi=-0.5;
  52. abouty=-90;
  53. perslevel=1000;
  54. init(
  55. [{x:80,y:180,z:0},{x:0,y:180,z:80},{x:0,y:0,z:80},{x:80,y:20,z:0},{x:50,y:150,z:30},{x:0,y:150,z:80},{x:0,y:130,z:80},{x:30,y:130,z:50},{x:28,y:110,z:52},{x:0,y:110,z:80},{x:0,y:90,z:80},{x:45,y:90,z:35},{x:44,y:80,z:36},{x:25,y:80,z:55},{x:22,y:66,z:58},{x:0,y:60,z:80},{x:0,y:40,z:80},{x:40,y:50,z:40}],
  56. [],
  57. [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[235,235,235],vertices:[4,5,6,7,8,9,10,11,4]},{color:[235,235,235],vertices:[12,13,14,15,16,17,12]}]
  58. );
  59. }
  60. k3dmain.addK3DObject(obj3);
  61. varobj4=newK3D.K3DObject();
  62. with(obj4)
  63. {
  64. drawmode="solid";
  65. shademode="lightsource";
  66. sortmode="unsorted";
  67. addphi=-0.5;
  68. abouty=-90;
  69. perslevel=1000;
  70. init(
  71. [{x:0,y:180,z:80},{x:-80,y:180,z:0},{x:-80,y:20,z:0},{x:0,y:0,z:80},{x:0,y:165,z:80},{x:-68,y:165,z:12},{x:-55,y:35,z:25},{x:0,y:20,z:80},{x:0,y:150,z:80},{x:-50,y:150,z:30},{x:-47,y:130,z:33},{x:0,y:130,z:80},{x:0,y:110,z:80},{x:-45,y:110,z:35},{x:-40,y:50,z:40},{x:0,y:40,z:80},{x:0,y:60,z:80},{x:-20,y:66,z:60},{x:-23,y:90,z:57},{x:0,y:90,z:80}],
  72. [],
  73. [{color:[227,76,38],vertices:[0,1,2,3,0]},{color:[240,101,41],vertices:[4,5,6,7,4]},{color:[235,235,235],vertices:[8,9,10,11,8]},{color:[235,235,235],vertices:[12,13,14,15,16,17,18,19,12]}]
  74. );
  75. }
  76. k3dmain.addK3DObject(obj4);
  77. varobjBase=newK3D.K3DObject();
  78. with(objBase)
  79. {
  80. drawmode="solid";
  81. shademode="lightsource";
  82. sortmode="unsorted";
  83. addphi=-0.5;
  84. abouty=-90;
  85. perslevel=1000;
  86. init(
  87. [{x:0,y:0,z:-80},{x:-80,y:20,z:0},{x:0,y:0,z:80},{x:80,y:20,z:0}],
  88. [],
  89. [{color:[227,76,38],vertices:[0,2,1,0]},{color:[227,76,38],vertices:[0,3,2,0]}]
  90. );
  91. }
  92. k3dmain.addK3DObject(objBase);
  93. varobjHtml=newK3D.K3DObject();
  94. with(objHtml)
  95. {
  96. drawmode="solid";
  97. shademode="lightsource";
  98. //sortmode="unsorted";
  99. color=[64,64,64];
  100. doublesided=true;
  101. addphi=-0.5;
  102. abouty=100;
  103. scale=0.75;
  104. perslevel=1000;
  105. init(
  106. [{x:-80,y:40,z:0},{x:-70,y:40,z:0},{x:-70,y:30,z:0},{x:-60,y:30,z:0},{x:-60,y:40,z:0},{x:-50,y:40,z:0},{x:-50,y:10,z:0},{x:-60,y:10,z:0},{x:-60,y:20,z:0},{x:-70,y:20,z:0},{x:-70,y:10,z:0},{x:-80,y:10,z:0},{x:-40,y:40,z:0},{x:-10,y:40,z:0},{x:-10,y:30,z:0},{x:-20,y:30,z:0},{x:-20,y:10,z:0},{x:-30,y:10,z:0},{x:-30,y:30,z:0},{x:-40,y:30,z:0},{x:0,y:40,z:0},{x:10,y:40,z:0},{x:20,y:30,z:0},{x:30,y:40,z:0},{x:40,y:40,z:0},{x:40,y:10,z:0},{x:30,y:10,z:0},{x:30,y:30,z:0},{x:20,y:20,z:0},{x:10,y:30,z:0},{x:10,y:10,z:0},{x:0,y:10,z:0},{x:50,y:40,z:0},{x:60,y:40,z:0},{x:60,y:20,z:0},{x:80,y:20,z:0},{x:80,y:10,z:0},{x:50,y:10,z:0}],
  107. [],
  108. [{vertices:[0,1,2,3,4,5,6,7,8,9,10,11,0]},{vertices:[12,13,14,15,16,17,18,19,12]},{vertices:[20,21,22,23,24,25,26,27,28,29,30,31,20]},{vertices:[32,33,34,35,36,37,32]}]
  109. );
  110. }
  111. k3dmain.addK3DObject(objHtml);
  112. //addrenderloopcallback
  113. varctx=canvas.getContext('2d');
  114. varrotationOffset=0;
  115. varlen=(canvas.height>canvas.width?canvas.height:canvas.width)*0.7;
  116. k3dmain.clearBackground=false;
  117. k3dmain.callback=function()
  118. {
  119. //manuallyclearbg-aswewanttorendersomeextragoodies
  120. ctx.clearRect(0,0,canvas.width,canvas.height);
  121. //drawbgeffectbeforeK3Ddoesits3Drendering
  122. ctx.save();
  123. ctx.translate(canvas.width/2,canvas.height/2);
  124. ctx.rotate(rotationOffset);
  125. //firstfillpass-outerrays
  126. varRAYCOUNT=24;
  127. ctx.fillStyle="#eee";
  128. ctx.beginPath();
  129. for(vari=0;i<RAYCOUNT;i++)
  130. {
  131. //rotatecontext
  132. ctx.rotate(TWOPI/RAYCOUNT);
  133. ctx.moveTo(0,0);
  134. ctx.lineTo(-20,len);
  135. ctx.lineTo(20,len);
  136. }
  137. ctx.closePath();
  138. ctx.fill();
  139. //secondfillpass-innerrays
  140. ctx.fillStyle="#fff";
  141. ctx.beginPath();
  142. for(vari=0;i<RAYCOUNT;i++)
  143. {
  144. //rotatecontext
  145. ctx.rotate(TWOPI/RAYCOUNT);
  146. ctx.moveTo(0,0);
  147. ctx.lineTo(-15,len);
  148. ctx.lineTo(15,len);
  149. }
  150. ctx.closePath();
  151. ctx.fill();
  152. ctx.restore();
  153. rotationOffset+=0.005;
  154. //applyuserinteractiontorotation
  155. for(vari=0,objs=k3dmain.objects;i<objs.length;i++)
  156. {
  157. objs[i].ophi+=targetRotationX;
  158. }
  159. if(targetRotationX>-0.5)targetRotationX-=0.05;
  160. elseif(targetRotationX<-0.55)targetRotationX+=0.05;
  161. if(targetRotationX>-0.55&&targetRotationX<-0.5)targetRotationX=-0.5;
  162. };
  163. //startdemoloop
  164. k3dmain.paused=true;
  165. setInterval(function(){k3dmain.tick()},1000/60);
  166. }
  167. //niftydrag/toucheventcapturecodeborrowedfromMrDoobhttp://mrdoob.com/
  168. vartargetRotationX=0;
  169. vartargetRotationOnMouseDownX=0;
  170. varmouseX=0;
  171. varmouseXOnMouseDown=0;
  172. vartargetRotationY=0;
  173. vartargetRotationOnMouseDownY=0;
  174. varmouseY=0;
  175. varmouseYOnMouseDown=0;
  176. varwindowHalfX=window.innerWidth/2;
  177. varwindowHalfY=window.innerHeight/2;
  178. document.addEventListener('mousedown',onDocumentMouseDown,false);
  179. document.addEventListener('touchstart',onDocumentTouchStart,false);
  180. document.addEventListener('touchmove',onDocumentTouchMove,false);
  181. functiononDocumentMouseDown(event){
  182. event.preventDefault();
  183. document.addEventListener('mousemove',onDocumentMouseMove,false);
  184. document.addEventListener('mouseup',onDocumentMouseUp,false);
  185. document.addEventListener('mouseout',onDocumentMouseOut,false);
  186. mouseXOnMouseDown=event.clientX-windowHalfX;
  187. targetRotationOnMouseDownX=targetRotationX;
  188. mouseYOnMouseDown=event.clientY-windowHalfY;
  189. targetRotationOnMouseDownY=targetRotationY;
  190. }
  191. functiononDocumentMouseMove(event)
  192. {
  193. mouseX=event.clientX-windowHalfX;
  194. targetRotationX=targetRotationOnMouseDownX+(mouseX-mouseXOnMouseDown)*0.02;
  195. mouseY=event.clientY-windowHalfY;
  196. targetRotationY=targetRotationOnMouseDownY+(mouseY-mouseYOnMouseDown)*0.02;
  197. }
  198. functiononDocumentMouseUp(event)
  199. {
  200. document.removeEventListener('mousemove',onDocumentMouseMove,false);
  201. document.removeEventListener('mouseup',onDocumentMouseUp,false);
  202. document.removeEventListener('mouseout',onDocumentMouseOut,false);
  203. }
  204. functiononDocumentMouseOut(event)
  205. {
  206. document.removeEventListener('mousemove',onDocumentMouseMove,false);
  207. document.removeEventListener('mouseup',onDocumentMouseUp,false);
  208. document.removeEventListener('mouseout',onDocumentMouseOut,false);
  209. }
  210. functiononDocumentTouchStart(event)
  211. {
  212. if(event.touches.length==1)
  213. {
  214. event.preventDefault();
  215. mouseXOnMouseDown=event.touches[0].pageX-windowHalfX;
  216. targetRotationOnMouseDownX=targetRotationX;
  217. mouseYOnMouseDown=event.touches[0].pageY-windowHalfY;
  218. targetRotationOnMouseDownY=targetRotationY;
  219. }
  220. }
  221. functiononDocumentTouchMove(event)
  222. {
  223. if(event.touches.length==1)
  224. {
  225. event.preventDefault();
  226. mouseX=event.touches[0].pageX-windowHalfX;
  227. targetRotationX=targetRotationOnMouseDownX+(mouseX-mouseXOnMouseDown)*0.05;
  228. mouseY=event.touches[0].pageY-windowHalfY;
  229. targetRotationY=targetRotationOnMouseDownY+(mouseX-mouseYOnMouseDown)*0.05;
  230. }
  231. }

以上就是本文的全部内容,希望对大家的学习有所帮助。