用JavaScript绘图—JS2D函数集
<script Language="javascript"><br />
/****************** JS2D函数集 *******************<br />
<br />
作者:neweroica 2003-3-28<br />
<br />
CopyRight (C) 2003<br />
<br />
在引用或转载时请保留此版权信息,谢谢!!!<br />
<br />
本函数集可以单独存成一个js文件:"JS2D.js"<br />
<br />
***************************************************/<br />
<br />
/************* 画点 **************<br />
x,y 点所在的屏幕坐标(像素)<br />
color 颜色(字符串值)<br />
size 大小(像素)<br />
**********************************/<br />
function drawDot(x,y,color,size){<br />
document.write("<table border='0' cellspacing=0 cellpadding=0><tr><td style='position: absolute; left: "+(x)+"; top: "+(y)+";background-color: "+color+"' width="+size+" height="+size+"></td></tr></table>")<br />
}<br />
<br />
/************* 画直线 **************<br />
x1,y1 起点所在的屏幕坐标(像素)<br />
x2,y2 终点所在的屏幕坐标(像素)<br />
color 颜色(字符串值)<br />
size 大小(像素)<br />
style 样式<br />
=0 实线<br />
=1 虚线<br />
=2 虚实线<br />
**********************************/<br />
function drawLine(x1,y1,x2,y2,color,size,style){<br />
var i;<br />
var r=Math.floor(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));<br />
var theta=Math.atan((x2-x1)/(y2-y1));<br />
if(((y2-y1)<0&&(x2-x1)>0)||((y2-y1)<0&&(x2-x1)<0))<br />
theta=Math.PI+theta;<br />
var dx=Math.sin(theta);//alert(dx)<br />
var dy=Math.cos(theta);<br />
for(i=0;i<r;i++){<br />
switch(style){<br />
case 0:<br />
drawDot(x1+i*dx,y1+i*dy,color,size);<br />
break;<br />
case 1:<br />
i+=size*2;<br />
drawDot(x1+i*dx,y1+i*dy,color,size);<br />
break;<br />
case 2:<br />
if(Math.floor(i/4/size)%2==0){<br />
drawDot(x1+i*dx,y1+i*dy,color,size);<br />
}<br />
else{<br />
i+=size*2;<br />
drawDot(x1+i*dx,y1+i*dy,color,size);<br />
}<br />
break;<br />
default:<br />
drawDot(x1+i*dx,y1+i*dy,color,size);<br />
break;<br />
}<br />
}<br />
}<br />
<br />
/************* 画实心矩形 **************<br />
x1,y1 起点(矩形左上角)所在的屏幕坐标(像素)<br />
x2,y2 终点(矩形右下角)所在的屏幕坐标(像素)<br />
color 颜色(字符串值)<br />
**********************************/<br />
function drawFilledRect(x1,y1,x2,y2,color){<br />
document.write("<table border='0' cellspacing=0 cellpadding=0><tr><td style='position: absolute; left: "+(x1)+"; top: "+(y1)+";background-color: "+color+"' width="+(x2-x1)+" height="+(y2-y1)+"></td></tr></table>")<br />
}<br />
<br />
/************* 画矩形 **************<br />
x1,y1 起点(矩形左上角)所在的屏幕坐标(像素)<br />
x2,y2 终点(矩形右下角)所在的屏幕坐标(像素)<br />
color 颜色(字符串值)<br />
size 大小(像素)<br />
style 样式<br />
=0 实线<br />
=1 虚线<br />
=2 虚实线<br />
**********************************/<br />
function drawRect(x1,y1,x2,y2,color,size,style){<br />
drawLine(x1,y1,x2,y1,color,size,style);<br />
drawLine(x1,y2,x2,y2,color,size,style);<br />
drawLine(x1,y1,x1,y2,color,size,style);<br />
drawLine(x2,y1,x2,y2,color,size,style);<br />
}<br />
<br />
/************* 画椭圆 **************<br />
x,y 中心所在的屏幕坐标(像素)<br />
a,b 长轴和短轴的长度(像素)<br />
color 颜色(字符串值)<br />
size 大小(像素)<br />
precision 边缘精细度<br />
**********************************/<br />
function drawOval(x,y,a,b,color,size,precision){<br />
var i;<br />
var iMax=2*Math.PI;<br />
var step=2*Math.PI/(precision*Math.sqrt(a*b)*4.5);<br />
for(i=0;i<iMax;i+=step){<br />
drawDot(x+a*Math.cos(i),y+b*Math.sin(i),color,size);<br />
}<br />
}<br />
<br />
/************* 画多边形 **************<br />
x,y 中心所在的屏幕坐标(像素)<br />
r 多边形外接圆半径(像素)<br />
n 多边形的边数<br />
color 颜色(字符串值)<br />
size 大小(像素)<br />
style 样式<br />
=0 实线<br />
=1 虚线<br />
=2 虚实线<br />
**********************************/<br />
function drawPoly(x,y,r,n,color,size,style){<br />
var i;<br />
var theta=Math.PI;<br />
var x1=x,y1=y-r,x2,y2;<br />
for(i=0;i<n;i++){<br />
theta-=(2*Math.PI/n);<br />
x2=x+r*Math.sin(theta);<br />
y2=y+r*Math.cos(theta);<br />
drawLine(x1,y1,x2,y2,color,size,style);<br />
x1=x2;<br />
y1=y2;//alert(x1+" "+y1)<br />
}<br />
}<br />
</script><br />
<br />
<br />
<script><br />
//****************** JS2D函数集示例 *******************<br />
drawLine(20,20,300,20,"#0000cc",2,0);<br />
drawLine(20,40,300,40,"#0000cc",2,1);<br />
drawLine(20,60,300,60,"#0000cc",2,2);<br />
drawFilledRect(20,80,300,200,"009900");<br />
drawRect(20,220,220,320,"ff0000",2,0);<br />
drawRect(240,220,440,320,"ff0000",2,1);<br />
drawRect(460,220,660,320,"ff0000",2,2);<br />
drawOval(250,450,120,50,"006600",1,1);<br />
drawOval(250,650,120,120,"006600",2,0.5);<br />
drawPoly(200,900,100,3,"ff8800",2,0);<br />
drawPoly(400,900,100,4,"ff8800",2,1);<br />
drawPoly(600,900,100,5,"ff8800",2,2);<br />
drawPoly(200,1100,100,6,"ff8800",2,0);<br />
drawPoly(400,1100,100,7,"ff8800",2,1);<br />
drawPoly(600,1100,100,12,"ff8800",2,2);<br />
</script>