Js模拟三角函数曲线

来源:互联网 发布:行动派琦琦 知乎 编辑:程序博客网 时间:2024/04/28 22:00

http://bbs.blueidea.com/viewthread.php?tid=2893888

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <meta name="description" content="javascript特效,正弦函数,余弦函数" /> 
  4. <meta name="copyright" content="http://hi.baidu.com/lpj1985" /> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  7. <title>JS模拟三角函数</title> 
  8. <script type="text/javascript"> 
  9. function $(id){ 
  10.   return document.getElementById(id);} 
  11. function gt(obj,tag){ 
  12.   return obj.getElementsByTagName(tag);} 
  13. </script> 
  14. <style type="text/css"> 
  15. body,html{margin:0; background:#666} 
  16. #container,#container1{position:absolute; width:600px; height:60px; left:50%; top:50%; margin:-30px 0 0 -300px; background:#bbb; border:1px #fff solid;} 
  17. #container1{margin:-90px 0 0 -300px; border-bottom:none;} 
  18. #container div.sin,#container div.cos{position:absolute; width:2px; height:2px; background:#900; overflow:hidden;} 
  19. #container div.cos{background:#036} 
  20. #opt{width:600px; height:20px; position:absolute; left:50%; top:50%; margin:60px 0 0 -300px; background:#ccc;} 
  21. #opt span{font-family:Arial; font-size:12px; display:block; float:right; background:#888; height:20px; width:40px; line-height:20px; overflow:hidden;margin-left:1em; text-align:center; cursor:pointer;} 
  22. #opt .sin,#opt .cos{color:#900; float:left; margin-left:0; margin-right:1em;} 
  23. #opt .cos{color:#036;} 
  24. </style> 
  25. </head> 
  26. <body onselectstart="return false"> 
  27. <div id="container1"></div> 
  28. <div id="container"></div> 
  29. <div id="opt"> 
  30.     <span class="sin" onclick="shCur('sin')">sinX</span> 
  31.     <span class="cos" onclick="shCur('cos')">cosX</span> 
  32.     <span onclick="clearTimeout(timer)">Stop</span> 
  33.     <span onclick="mCur()">Play</span> 
  34.     <span onclick="spl('b')">PL+</span> 
  35.     <span onclick="spl('s')">PL-</span> 
  36.     <span onclick="szf('b')">ZF+</span> 
  37.     <span onclick="szf('s')">ZF-</span> 
  38. </div> 
  39. <script type="text/javascript"> 
  40. var zf=50pl=0.05;x=0
  41. //绘制曲线 
  42. function drawCur(zf,pl){ 
  43. $('container').innerHTML=""
  44. rX=0
  45. rY=0
  46. for(i=1;i<=200;i++){ 
  47.     var dsin=document.createElement("div"); 
  48.     dsin.setAttribute("id","sin"+i); 
  49.     dsin.className="sin"
  50.     dsin.style.left=rX+"px"; 
  51.     dsin.style.top=Math.sin((i+x)*pl)*zf+"px"; 
  52.     var dcos=document.createElement("div"); 
  53.     dcos.setAttribute("id","cos"+i); 
  54.     dcos.className="cos"
  55.     dcos.style.left=rX+"px"; 
  56.     dcos.style.top=Math.cos((i+x)*pl)*zf+"px"; 
  57.     rX+=3; 
  58.     rY+=pl; 
  59.     $('container').appendChild(dsin); 
  60.     $('container').appendChild(dcos);} 
  61.     } 
  62. drawCur(zf,pl); 
  63. //隐藏曲线 
  64. function shCur(cur){ 
  65. var cdiv=gt($('container'),"div"); 
  66. for(i=0;i<cdiv.length;i++){ 
  67.    if(cdiv[i].id.substring(0,3)==cur){ 
  68.       cdiv[i].style.display=(cdiv[i].style.display=="block")?"none":"block";} 
  69.    } 
  70. //设置振幅、频率 
  71. function szf(flag){ 
  72. if(flag=="b")zf+=5; 
  73. if(flag=="s")zf-=5
  74. drawCur(zf,pl);} 
  75. function spl(flag){ 
  76. if(flag=="b")pl+=0.05; 
  77. if(flag=="s")pl-=0.05; 
  78. drawCur(zf,pl);} 
  79. //让曲线动起来 
  80. //不要变化太快,否则会吃尽内存的 
  81. var timer; 
  82. function mCur(){ 
  83. var cdiv=gt($('container'),"div"); 
  84. for(i=0;i<cdiv.length;i++){ 
  85.    cid=parseInt(cdiv[i].id.substring(3,cdiv[i].id.length)); 
  86.    if(cdiv[i].id.substring(0,3)=="sin"){ 
  87.       cdiv[i].style.top=Math.sin((cid+x)*pl)*zf+"px";} 
  88.    if(cdiv[i].id.substring(0,3)=="cos"){ 
  89.       cdiv[i].style.top=Math.cos((cid+x)*pl)*zf+"px";} 
  90.    } 
  91. x++; 
  92. timer=setTimeout(mCur,200) 
  93. mCur(); 
  94. var opt=gt($('opt'),"span"); 
  95. for(i=0;i<opt.length;i++){ 
  96.    opt[i].onmouseover=function(){this.style.background="#aaa";} 
  97.    opt[i].onmouseout=function(){this.style.background="#888";} 
  98.    } 
  99. </script> 
  100. </body> 
  101. </html> 
原创粉丝点击