純JS畫點、畫線、畫圓的方法


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
div{ overflow:hidden;}
</style>
<script type="text/javascript">
/* 珠峰培訓  2011年12月9日課堂示例
以下畫點,畫線,畫圓的方法,都不是用HTML5的canvas,而是用的純js
用到了一些數學的三角函數方法
以下代碼是課堂隨機寫出,沒有做更多優化
*/

function point(x,y){//畫點
var oDiv=document.createElement('div');
oDiv.style.position='absolute';
oDiv.style.height='2px';
oDiv.style.width='2px';
oDiv.style.backgroundColor='red';
oDiv.style.left=x+'px';
oDiv.style.top=y+'px';
//document.body.appendChild(oDiv);
return oDiv;//注意:返回的值是一個dom節點,但並未追加到文檔中
}
function drawLine(x1,y1,x2,y2){//畫一條直線的方法
var x=x2-x1;//寬
var y=y2-y1;//高
var frag=document.createDocumentFragment();
if(Math.abs(y)>Math.abs(x)){//那個邊更長,用那邊來做畫點的依據(就是下面那個循環),如果不這樣,
if(y>0)//正着畫線是這樣的

    1. for(var i=0;i<y;i++){
    2. var width=x/y*i  //x/y是直角兩個邊長的比,根據這個比例,求出新坐標的位置
    3. {
    4. frag.appendChild(point(width+x1,i+y1));
    5. }
    6. }
    7. if(y<0){//有時候是倒着畫線的
    8. for(var i=0;i>y;i--){
    9. var width=x/y*i
    10. {
    11. frag.appendChild(   point(width+x1,i+y1));
    12. }
    13. }
    14. }
    15. }//end if
    16. else {
    17. if(x>0)//正着畫線是這樣的
    18. for(var i=0;i<x;i++){
    19. var height=y/x*i
    20. {
    21. frag.appendChild(point(i+x1,height+y1));
    22. }
    23. }
    24. if(x<0){//有時候是倒着畫線的
    25. for(var i=0;i>x;i--){
    26. var height=y/x*i
    27. {
    28. frag.appendChild(   point(i+x1,height+y1));
    29. }
    30. }
    31. }//end if
    32. }
    33. //document.body.appendChild(frag);
    34. document.getElementById('div1').appendChild(frag);
    35. //var oDiv=document.createElement('div')
    36. //oDiv.appendChild(frag);
    37. //document.body.appendChild(oDiv);
    38. }
    39. function drawCircle(){//畫個圓
    40. var r=200;
    41. var x1=300;
    42. var y1=300;
    43. var frag=document.createDocumentFragment();
    44. for(var degree=0;degree<360;degree+=2){
    45. var x2=r*Math.sin(degree*Math.PI/180);
    46. var y2=r*Math.cos(degree*Math.PI/180);
    47. frag.appendChild(point(x1+x2,y1+y2));
    48. }
    49. document.body.appendChild(frag);
    50. }
    51. function dragCircle(x1,y1,x2,y2){//拖出一個圓來
    52. var r=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));//求出半徑的長 直角三角形中 斜邊的平方=兩個直邊的平方之和
    53. var frag=document.createDocumentFragment();
    54. for(var degree=0;degree<360;degree+=2){//每隔2度畫一個點
    55. var x2=r*Math.sin(degree*Math.PI/180);
    56. var y2=r*Math.cos(degree*Math.PI/180);
    57. frag.appendChild(point(x1+x2,y1+y2));
    58. }
    59. document.getElementById('div1').appendChild(frag);
    60. }
    61. window.onload=function(){
    62. drawCircle()
    63. drawLine(500,30,0,30);
    64. drawLine(300,20,300,500);
    65. drawLine(50,20,700,500);
    66. var x1=0;
    67. var y1=0;
    68. //以下是處理拖拽 拖拽的時候,出現一條直線和一個圓
    69. //注意:由於這些操作都是由DOM來完成的,所以性能開銷比較大,尤其是在IE里,明顯的會卡一些。
    70. function down(e){
    71. var e=e||window.event;
    72. x1=e.clientX;
    73. y1=e.clientY;
    74. document.onmousemove=move;
    75. document.onmouseup=up;
    76. }
    77. function move(e){
    78. document.getElementById('div1').innerHTML='';
    79. var e=e||window.event;
    80. var x2=e.clientX;
    81. var y2=e.clientY;
    82. drawLine(x1,y1,x2,y2);//用這個方法就可以在瀏覽器上拖出一條直線來
    83. dragCircle(x1,y1,x2,y2);//用這個方法就可以在瀏覽器上拖出一個圓來
    84. }
    85. function up(){
    86. document.onmousemove=null;
    87. document.onmouseup=null;
    88. }
    89. document.onmousedown=down;
    90. }
    91. </script>
    92. </head>
    93. <body>
    94. <div id="div1">在瀏覽器上拖動鼠標試試</div>
    95. </body>
    96. </html>
    97. 更多內容請點擊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM