欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

多视角3D可旋转的HTML5 Logo动画_html5教程技巧

程序员文章站 2022-03-20 17:33:56
...
这是一款基于HTML5和CSS3的3D动画特效,与之前的3D特效不同的是,这款是完全用HTML5特性实现的,而并非用多张图片叠加起来。这款3D动画还有一个特点是可以无限的旋转,从而可以从多个视角来观察HTML5 Logo。

多视角3D可旋转的HTML5 Logo动画_html5教程技巧

在线演示 源码下载

HTML代码

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

JavaScript代码

JavaScript Code复制内容到剪贴板
  1. // bind to window onload handler
  2. window.addEventListener('load', onloadHandler, false);
  3. /**
  4. * Window onload handler
  5. */
  6. function onloadHandler()
  7. {
  8. var canvas = document.getElementById('canvas');
  9. canvas.width = window.innerWidth;
  10. canvas.height = window.innerHeight;
  11. var k3dmain = new K3D.Controller(canvas, true);
  12. // generate 3D objects
  13. var obj1 = new K3D.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. var obj2 = new K3D.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. var obj3 = new K3D.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. var obj4 = new K3D.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. var objBase = new K3D.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. var objHtml = new K3D.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. // add render loop callback
  113. var ctx = canvas.getContext('2d');
  114. var rotationOffset = 0;
  115. var len = (canvas.height > canvas.width ? canvas.height : canvas.width) * 0.7;
  116. k3dmain.clearBackground = false;
  117. k3dmain.callback = function()
  118. {
  119. // manually clear bg - as we want to render some extra goodies
  120. ctx.clearRect(0, 0, canvas.width, canvas.height);
  121. // draw bg effect before K3D does its 3D rendering
  122. ctx.save();
  123. ctx.translate(canvas.width/2, canvas.height/2);
  124. ctx.rotate(rotationOffset);
  125. // first fill pass - outer rays
  126. var RAYCOUNT = 24;
  127. ctx.fillStyle = "#eee";
  128. ctx.beginPath();
  129. for (var i=0; i
  130. {
  131. // rotate context
  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. // second fill pass - inner rays
  140. ctx.fillStyle = "#fff";
  141. ctx.beginPath();
  142. for (var i=0; i
  143. {
  144. // rotate context
  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. // apply user interaction to rotation
  155. for (var i=0, objs=k3dmain.objects; i
  156. {
  157. objs[i].ophi += targetRotationX;
  158. }
  159. if (targetRotationX > -0.5) targetRotationX -= 0.05;
  160. else if (targetRotationX
  161. if (targetRotationX > -0.55 && targetRotationX
  162. };
  163. // start demo loop
  164. k3dmain.paused = true;
  165. setInterval(function(){k3dmain.tick()}, 1000/60);
  166. }
  167. // nifty drag/touch event capture code borrowed from Mr Doob http://mrdoob.com/
  168. var targetRotationX = 0;
  169. var targetRotationOnMouseDownX = 0;
  170. var mouseX = 0;
  171. var mouseXOnMouseDown = 0;
  172. var targetRotationY = 0;
  173. var targetRotationOnMouseDownY = 0;
  174. var mouseY = 0;
  175. var mouseYOnMouseDown = 0;
  176. var windowHalfX = window.innerWidth / 2;
  177. var windowHalfY = window.innerHeight / 2;
  178. document.addEventListener('mousedown', onDocumentMouseDown, false);
  179. document.addEventListener('touchstart', onDocumentTouchStart, false);
  180. document.addEventListener('touchmove', onDocumentTouchMove, false);
  181. function onDocumentMouseDown( 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. function onDocumentMouseMove( 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. function onDocumentMouseUp( event )
  199. {
  200. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  201. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  202. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  203. }
  204. function onDocumentMouseOut( event )
  205. {
  206. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  207. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  208. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  209. }
  210. function onDocumentTouchStart( 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. function onDocumentTouchMove( 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. }

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