以JavaScript繪圖—JS2D函數集
作者:Eve Cole
更新時間:2009-06-08 18:25:42
<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=yr,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>