软件开发|编程技术|编程代码|编程入门先学什么—程序设计语言

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

HTML5,Logo,3D旋转

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

在线演示 源码下载

HTML代码

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

  1. pclass=link-overlay
  2. /p canvasid=canvasstyle=background-color:#ddd/canvas

JavaScript代码

JavaScript Code复制内容到剪贴板

  1. //bindtowindowonloadhandlerwindow.addEventListener('load',onloadHandler,false);
  2. /**
  3. *Windowonloadhandler*/
  4. functiononloadHandler(){
  5. varcanvas=document.getElementById('canvas');canvas.width=window.innerWidth;
  6. canvas.height=window.innerHeight;vark3dmain=newK3D.Controller(canvas,true);
  7. //generate3Dobjects
  8. varobj1=newK3D.K3DObject();
  9. with(obj1){
  10. drawmode=solid;shademode=lightsource;
  11. sortmode=unsorted;addphi=-0.5;
  12. abouty=-90;perslevel=1000;
  13. init([{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}],
  14. [],[{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]}]
  15. );}
  16. k3dmain.addK3DObject(obj1);
  17. varobj2=newK3D.K3DObject();with(obj2)
  18. {drawmode=solid;
  19. shademode=lightsource;sortmode=unsorted;
  20. addphi=-0.5;abouty=-90;
  21. perslevel=1000;init(
  22. [{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}],[],
  23. [{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]}]);
  24. }k3dmain.addK3DObject(obj2);
  25. varobj3=newK3D.K3DObject();
  26. with(obj3){
  27. drawmode=solid;shademode=lightsource;
  28. sortmode=unsorted;addphi=-0.5;
  29. abouty=-90;perslevel=1000;
  30. init([{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}],
  31. [],[{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]}]
  32. );}
  33. k3dmain.addK3DObject(obj3);
  34. varobj4=newK3D.K3DObject();with(obj4)
  35. {drawmode=solid;
  36. shademode=lightsource;sortmode=unsorted;
  37. addphi=-0.5;abouty=-90;
  38. perslevel=1000;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. [{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]}]);
  41. }k3dmain.addK3DObject(obj4);
  42. varobjBase=newK3D.K3DObject();
  43. with(objBase){
  44. drawmode=solid;shademode=lightsource;
  45. sortmode=unsorted;addphi=-0.5;
  46. abouty=-90;perslevel=1000;
  47. init([{x:0,y:0,z:-80},{x:-80,y:20,z:0},{x:0,y:0,z:80},{x:80,y:20,z:0}],
  48. [],[{color:[227,76,38],vertices:[0,2,1,0]},{color:[227,76,38],vertices:[0,3,2,0]}]
  49. );}
  50. k3dmain.addK3DObject(objBase);
  51. varobjHtml=newK3D.K3DObject();with(objHtml)
  52. {drawmode=solid;
  53. shademode=lightsource;//sortmode=unsorted;
  54. color=[64,64,64];doublesided=true;
  55. addphi=-0.5;abouty=100;
  56. scale=0.75;perslevel=1000;
  57. init([{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}],
  58. [],[{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]}]
  59. );}
  60. k3dmain.addK3DObject(objHtml);
  61. //addrenderloopcallbackvarctx=canvas.getContext('2d');
  62. varrotationOffset=0;varlen=(canvas.heightcanvas.width?canvas.height:canvas.width)*0.7;
  63. k3dmain.clearBackground=false;k3dmain.callback=function()
  64. {//manuallyclearbg-aswewanttorendersomeextragoodies
  65. ctx.clearRect(0,0,canvas.width,canvas.height);
  66. //drawbgeffectbeforeK3Ddoesits3Drenderingctx.save();
  67. ctx.translate(canvas.width/2,canvas.height/2);ctx.rotate(rotationOffset);
  68. //firstfillpass-outerrays
  69. varRAYCOUNT=24;ctx.fillStyle=#eee;
  70. ctx.beginPath();for(vari=0;iRAYCOUNT;i++)
  71. {//rotatecontext
  72. ctx.rotate(TWOPI/RAYCOUNT);ctx.moveTo(0,0);
  73. ctx.lineTo(-20,len);ctx.lineTo(20,len);
  74. }ctx.closePath();
  75. ctx.fill();//secondfillpass-innerrays
  76. ctx.fillStyle=#fff;ctx.beginPath();
  77. for(vari=0;iRAYCOUNT;i++){
  78. //rotatecontextctx.rotate(TWOPI/RAYCOUNT);
  79. ctx.moveTo(0,0);ctx.lineTo(-15,len);
  80. ctx.lineTo(15,len);}
  81. ctx.closePath();ctx.fill();
  82. ctx.restore();rotationOffset+=0.005;
  83. //applyuserinteractiontorotation
  84. for(vari=0,objs=k3dmain.objects;iobjs.length;i++){
  85. objs[i].ophi+=targetRotationX;}
  86. if(targetRotationX-0.5)targetRotationX-=0.05;
  87. elseif(targetRotationX-0.55)targetRotationX+=0.05;if(targetRotationX-0.55targetRotationX-0.5)targetRotationX=-0.5;
  88. };
  89. //startdemoloopk3dmain.paused=true;
  90. setInterval(function(){k3dmain.tick()},1000/60);}
  91. //niftydrag/toucheventcapturecodeborrowedfromMrDoobhttp://mrdoob.com/
  92. vartargetRotationX=0;vartargetRotationOnMouseDownX=0;
  93. varmouseX=0;varmouseXOnMouseDown=0;
  94. vartargetRotationY=0;vartargetRotationOnMouseDownY=0;
  95. varmouseY=0;varmouseYOnMouseDown=0;
  96. varwindowHalfX=window.innerWidth/2;
  97. varwindowHalfY=window.innerHeight/2;
  98. document.addEventListener('mousedown',onDocumentMouseDown,false);document.addEventListener('touchstart',onDocumentTouchStart,false);
  99. document.addEventListener('touchmove',onDocumentTouchMove,false);
  100. functiononDocumentMouseDown(event){
  101. event.preventDefault();
  102. document.addEventListener('mousemove',onDocumentMouseMove,false);document.addEventListener('mouseup',onDocumentMouseUp,false);
  103. document.addEventListener('mouseout',onDocumentMouseOut,false);
  104. mouseXOnMouseDown=event.clientX-windowHalfX;targetRotationOnMouseDownX=targetRotationX;
  105. mouseYOnMouseDown=event.clientY-windowHalfY;targetRotationOnMouseDownY=targetRotationY;
  106. }
  107. functiononDocumentMouseMove(event){
  108. mouseX=event.clientX-windowHalfX;targetRotationX=targetRotationOnMouseDownX+(mouseX-mouseXOnMouseDown)*0.02;
  109. mouseY=event.clientY-windowHalfY;targetRotationY=targetRotationOnMouseDownY+(mouseY-mouseYOnMouseDown)*0.02;
  110. }
  111. functiononDocumentMouseUp(event){
  112. document.removeEventListener('mousemove',onDocumentMouseMove,false);document.removeEventListener('mouseup',onDocumentMouseUp,false);
  113. document.removeEventListener('mouseout',onDocumentMouseOut,false);}
  114. functiononDocumentMouseOut(event)
  115. {document.removeEventListener('mousemove',onDocumentMouseMove,false);
  116. document.removeEventListener('mouseup',onDocumentMouseUp,false);document.removeEventListener('mouseout',onDocumentMouseOut,false);
  117. }
  118. functiononDocumentTouchStart(event){
  119. if(event.touches.length==1){
  120. event.preventDefault();
  121. mouseXOnMouseDown=event.touches[0].pageX-windowHalfX;targetRotationOnMouseDownX=targetRotationX;
  122. mouseYOnMouseDown=event.touches[0].pageY-windowHalfY;targetRotationOnMouseDownY=targetRotationY;
  123. }}
  124. functiononDocumentTouchMove(event)
  125. {if(event.touches.length==1)
  126. {event.preventDefault();
  127. mouseX=event.touches[0].pageX-windowHalfX;
  128. targetRotationX=targetRotationOnMouseDownX+(mouseX-mouseXOnMouseDown)*0.05;mouseY=event.touches[0].pageY-windowHalfY;
  129. targetRotationY=targetRotationOnMouseDownY+(mouseX-mouseYOnMouseDown)*0.05;}
  130. }

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