js画图

来源:互联网 发布:文泰刻绘软件 编辑:程序博客网 时间:2024/05/12 03:54

 

<scriptLanguage="javascript">

/******************JS2D函数集  *******************<br />

 在引用或转载时请保留此版权信息,谢谢!!!<br/>

<br />

 本函数集可以单独存成一个js文件:"JS2D.js"<br/>

<br />

***************************************************/

/************* 画点**************<br />

 x,y     点所在的屏幕坐标(像素)<br />

 color   颜色(字符串值)<br />

 size    大小(像素)<br />

**********************************/

functiondrawDot(x,y,color,size)

{

  document.write("<table border='0' cellspacing=0cellpadding=0><tr><td style='position: absolute; left:"+x+";

top:"+y+    ";background-color:"+color+"' width="+size+"height="+size+"></td></tr></table>");

}

/************* 画直线**************<br />

 x1,y1   起点所在的屏幕坐标(像素)<br />

 x2,y2   终点所在的屏幕坐标(像素)<br />

 color   颜色(字符串值)<br />

 size    大小(像素)<br />

 style   样式<br />

         =0    实线<br />

         =1    虚线<br />

         =2    虚实线<br />

**********************************/

functiondrawLine(x1,y1,x2,y2,color,size,style)

{

  vari;

  varr=Math.floor(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));

  vartheta=Math.atan((x2-x1)/(y2-y1));

 if((y2-y1)<0&&(x2-x1)<0)//第三象限

   theta=Math.PI+theta;

 else if((y2-y1)>0&&(x2-x1)<0)//第二象限

  theta=2*Math.PI+theta;

 else if((y2-y1)<0&&(x2-x1)>0)//第四象限

  theta=Math.PI+theta;

  vardx=Math.sin(theta);//alert(dx)<br />

  vardy=Math.cos(theta);

 for(i=0;i<r;i++)

  {

   switch(style)

 {

     case 0:

       drawDot(x1+i*dx,y1+i*dy,color,size);

       break;

     case 1:

       i+=size*2;

       drawDot(x1+i*dx,y1+i*dy,color,size);

       break;

     case 2:

       if(Math.floor(i/4/size)%2==0)

  {

         drawDot(x1+i*dx,y1+i*dy,color,size);

       }

       else

  {

           i+=size*2;

           drawDot(x1+i*dx,y1+i*dy,color,size);

       }

       break;

     default:

       drawDot(x1+i*dx,y1+i*dy,color,size);

       break;

   }

  }

}

/************* 画实心矩形**************<br />

 x1,y1   起点(矩形左上角)所在的屏幕坐标(像素)<br />

 x2,y2   终点(矩形右下角)所在的屏幕坐标(像素)<br />

 color   颜色(字符串值)<br />

**********************************/

functiondrawFilledRect(x1,y1,x2,y2,color)

{

 document.write("<table border='0' cellspacing=0cellpadding=0><tr><td style='position: absolute; left:"+(x1)+"; top: "+

(y1)+";background-color:"+color+"' width="+(x2-x1)+"height="+(y2-y1)+"></td></tr></table>")

}

/************* 画矩形**************<br />

 x1,y1   起点(矩形左上角)所在的屏幕坐标(像素)<br />

 x2,y2   终点(矩形右下角)所在的屏幕坐标(像素)<br />

 color   颜色(字符串值)<br />

 size    大小(像素)<br />

 style   样式<br />

         =0    实线<br />

         =1    虚线<br />

         =2    虚实线<br />

**********************************/

functiondrawRect(x1,y1,x2,y2,color,size,style)

{

 drawLine(x1,y1,x2,y1,color,size,style);

 drawLine(x1,y2,x2,y2,color,size,style);

 drawLine(x1,y1,x1,y2,color,size,style);

 drawLine(x2,y1,x2,y2,color,size,style);

}

/************* 画椭圆**************<br />

 x,y         中心所在的屏幕坐标(像素)<br/>

 a,b         长轴和短轴的长度(像素)<br/>

 color       颜色(字符串值)<br/>

 size        大小(像素)<br/>

 precision   边缘精细度<br />

**********************************/

functiondrawOval(x,y,a,b,color,size,precision)

{

  vari;

  variMax=2*Math.PI;

  varstep=2*Math.PI/(precision*Math.sqrt(a*b)*4.5);

 for(i=0;i<iMax;i+=step)

  {

   drawDot(x+a*Math.cos(i),y+b*Math.sin(i),color,size);

  }

}

/************* 画多边形**************<br />

 x,y     中心所在的屏幕坐标(像素)<br />

 r       多边形外接圆半径(像素)<br/>

 n       多边形的边数<br/>

 color   颜色(字符串值)<br />

 size    大小(像素)<br />

 style   样式<br />

         =0    实线<br />

         =1    虚线<br />

         =2    虚实线<br />

**********************************/

functiondrawPoly(x,y,r,n,color,size,style)

{

  vari;

  vartheta=Math.PI;

  varx1=x,y1=y-r,x2,y2;

 for(i=0;i<n;i++)

  {

   theta-=(2*Math.PI/n);

   x2=x+r*Math.sin(theta);

   y2=y+r*Math.cos(theta);

   drawLine(x1,y1,x2,y2,color,size,style);

   x1=x2;

   y1=y2;//alert(x1+" "+y1)<br />

  }

}

</script>

 

 

 

原创粉丝点击