当前位置:   article > 正文

JS伪3D 图形透视效果_js3d斜向穿透排版

js3d斜向穿透排版

本文地址:http://blog.csdn.net/ei__nino/article/details/9243331

本来是想实现多个圆片的透视效果,对于运算都是测试得出的。不是严谨的数学计算。

使用简单的div布局,css设置的一些形式

有舞台深度stageDeep,圆片深度距离zDistance,和修正角度p可以设置调节图像的整体显示效果。

图形随鼠标运动而运动。

Google Chrom下表现最好。不支持FF

演示地址:http://www.einino.net/circle2.html

下面代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <style type="text/css">
  6. body{ height:500px; }
  7. .ineed{width:40px; position:absolute; left:10px;border:1px #666 solid; height:40px; border-radius:20px; margin:5px; background:#EEE; float:left;}
  8. .cross{border:1px #666 solid;position:absolute; }
  9. </style>
  10. </head>
  11. <body>
  12. </body>
  13. <script type="text/javascript" >
  14. //author EI Nino
  15. //E-Mail : Jinyachen@gmail.com
  16. //copyright 2013
  17. function randColor(){
  18. var v=[0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E"];
  19. return "#"+v[Math.ceil(Math.random()*15%15)]+v[Math.ceil(Math.random()*15%15)]+v[Math.ceil(Math.random()*15%15)]+v[Math.ceil(Math.random()*15%15)]+v[Math.ceil(Math.random()*15%15)]+v[Math.ceil(Math.random()*15%15)];
  20. }
  21. function debug(){
  22. if(arguments.length==0)
  23. return;
  24. var d;
  25. var p = document.createElement("p");
  26. var pp;
  27. for(var i in arguments){
  28. var a = document.createElement("a");
  29. a.style.margin="4px";
  30. a.style.fontFamily="Arial";
  31. a.style.color=randColor();
  32. a.innerHTML = i+"@"+arguments[i];
  33. p.appendChild(a);
  34. }
  35. if(!(d=document.getElementById("DEBUG")))
  36. {
  37. d = document.createElement("div");
  38. d.setAttribute("id","DEBUG");
  39. d.style.width="500px";
  40. d.style.height="300px";
  41. d.style.background="#EFE";
  42. d.style.position="fixed";
  43. d.style.right="0";
  44. d.style.top="0";
  45. d.style.overflow="scroll";
  46. var h2 = document.createElement("h2");
  47. h2.innerHTML="DEBUG";
  48. h2.style.color="#AAA";
  49. h2.style.fontFamily="Arial";
  50. d.appendChild(h2);
  51. document.getElementsByTagName("body")[0].appendChild(d);
  52. }
  53. if(!(pp=document.getElementById("debug_p")))
  54. {
  55. d.appendChild(p);
  56. }
  57. else{
  58. d.insertBefore(p,pp);
  59. pp.setAttribute("id","");
  60. }
  61. p.setAttribute("id","debug_p");
  62. }
  63. function AngletoPI(a){
  64. return a/180*Math.PI;
  65. }
  66. function PItoAngle(p){
  67. return p/Math.PI*180;
  68. }
  69. function AngletoLength(p,z){
  70. return Math.tan(p)*z;
  71. }
  72. function Length(x,y,z){
  73. return Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z,3));
  74. }
  75. //cross
  76. function setCross(){
  77. for(var i=1; i<60; i++)//Y
  78. {
  79. var div =document.createElement("div");
  80. div.innerHTML="";
  81. div.className ="cross";
  82. div.style.width="2px";
  83. div.style.height="2px";
  84. div.style.borderRadius = "2px";
  85. div.style.borderColor="#F00";
  86. div.style.zIndex=-200;
  87. var width = div.style.width.substring(0,div.style.width.indexOf("px"));
  88. var height = div.style.height.substring(0,div.style.height.indexOf("px"));
  89. div.style.left=String(clientX/2-width/2)+"px";
  90. div.style.top =String(i*10)+"px";
  91. document.getElementsByTagName("body")[0].appendChild(div);
  92. }
  93. for(var i=1; i<100; i++)//X
  94. {
  95. var div =document.createElement("div");
  96. div.innerHTML="";
  97. div.className ="cross";
  98. div.style.width="2px";
  99. div.style.height="2px";
  100. div.style.borderRadius = "2px";
  101. div.style.borderColor="#0F0";
  102. div.style.zIndex=-100;
  103. var width = div.style.width.substring(0,div.style.width.indexOf("px"));
  104. var height = div.style.height.substring(0,div.style.height.indexOf("px"));
  105. div.style.left=String(i*10)+"px";
  106. div.style.top =String(clientY/2-height/2)+"px";
  107. document.getElementsByTagName("body")[0].appendChild(div);
  108. }
  109. /*for(var i=1; i<90; i++)//Z
  110. {
  111. var div =document.createElement("div");
  112. div.innerHTML="";
  113. div.className ="cross";
  114. div.style.width="2px";
  115. div.style.height="2px";
  116. div.style.borderRadius = "2px";
  117. div.style.borderColor="#00F";
  118. div.style.zIndex=5;
  119. var width = div.style.width.substring(0,div.style.width.indexOf("px"));
  120. var height = div.style.height.substring(0,div.style.height.indexOf("px"));
  121. div.style.left=String(clientX-i*10)+"px";
  122. div.style.top =String(i*5)+"px";
  123. document.getElementsByTagName("body")[0].appendChild(div);
  124. }*/
  125. }
  126. //cross end
  127. function DrowDiv(width,height,zIndex,vertical,horizon,deep,obj){
  128. var div =document.createElement("div");
  129. div.innerHTML="";
  130. div.className ="ineed";
  131. width = width-AngletoLength(p,zIndex);
  132. height = height-AngletoLength(p,zIndex);
  133. if(width<0)
  134. {
  135. return;
  136. }
  137. div.style.width=width+"px";
  138. div.style.height=height+"px";
  139. div.style.borderRadius = String(width/2)+"px";
  140. div.style.opacity = 0.6;
  141. div.style.background="#DDD";//randColor();
  142. div.style.zIndex=-zIndex;
  143. div.setAttribute("x",width);
  144. div.setAttribute("y",height);
  145. div.setAttribute("z",div.style.zIndex);
  146. div.style.left=String(clientX/2-width/2-horizon/deep*zIndex)+"px";
  147. div.style.top =String(clientY/2-height/2-vertical/deep*zIndex)+"px";
  148. obj.appendChild(div);
  149. }
  150. function Drow3D(v,h,deep){
  151. if(arguments.length<3)
  152. deep = stageDeep;
  153. var obj =document.getElementById("circle_3D");
  154. obj.innerHTML="";
  155. for(var i=1; i<10; i++)
  156. {
  157. DrowDiv(150,150,i*zDistance,v,h,deep,obj);
  158. }
  159. debug(v,h);
  160. }
  161. function Drow3DByMouse(v,h,deep){
  162. if(arguments.length<3)
  163. deep = stageDeep;
  164. h=event.pageX-(clientX/2);
  165. v=event.pageY-(clientY/2);
  166. var obj =document.getElementById("circle_3D");
  167. obj.innerHTML="";
  168. for(var i=1; i<10; i++)
  169. {
  170. DrowDiv(150,150,i*zDistance,v,h,deep,obj);
  171. }
  172. debug("y="+v,"x="+h);
  173. }
  174. function createStage(){
  175. var c = document.createElement("div");
  176. c.setAttribute("id","circle_3D");
  177. document.getElementsByTagName("body")[0].appendChild(c);
  178. stage = document.getElementById("circle_3D");
  179. stage.style.width="1000px";
  180. stage.style.height="500px";
  181. stage.style.border="1px solid #666";
  182. stage.style.overflow="hidden";
  183. stage.style.cursor="crosshair";
  184. }
  185. var stage;
  186. createStage();
  187. var clientX = stage.style.width.substring(0,stage.style.width.indexOf("px"));
  188. var clientY = stage.style.height.substring(0,stage.style.height.indexOf("px"));
  189. setCross();
  190. var stageDeep=100;//舞台深度
  191. var zDistance = 40;//物体间的距离
  192. var p = AngletoPI(20);//角度
  193. //Drow3D(-10,50);
  194. document.addEventListener("mousemove",Drow3DByMouse);
  195. </script>
  196. </html>




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/834641
推荐阅读
相关标签
  

闽ICP备14008679号