Drawing with JavaScript—JS2D function set
Author:Eve Cole
Update Time:2009-06-08 18:25:42
<script Language="javascript"><br />
/****************** JS2D function set *******************<br />
<br />
Author: neweroica 2003-3-28<br />
<br />
CopyRight (C) 2003<br />
<br />
Please retain this copyright information when quoting or reprinting, thank you!!!<br />
<br />
This function set can be saved as a separate js file: "JS2D.js"<br />
<br />
*************************************************** */<br />
<br />
/**************** Draw some******************<br />
Screen coordinates (pixels) of the x,y point<br />
color color (string value)<br />
size size (pixels)<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 />
/****************** Draw a straight line******************<br />
x1,y1 screen coordinates (pixels) of the starting point<br />
x2,y2 screen coordinates (pixels) of the end point<br />
color color (string value)<br />
size size (pixels)<br />
style style<br />
=0 solid line<br />
=1 dashed line<br />
=2 dashed solid line<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 />
/****************** Draw a solid rectangle******************<br />
x1,y1 screen coordinates (pixels) of the starting point (upper left corner of the rectangle)<br />
x2,y2 screen coordinates (pixels) of the end point (lower right corner of the rectangle) <br />
color color (string value)<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 />
/****************** Draw a rectangle******************<br />
x1,y1 screen coordinates (pixels) of the starting point (upper left corner of the rectangle)<br />
x2,y2 screen coordinates (pixels) of the end point (lower right corner of the rectangle) <br />
color color (string value)<br />
size size (pixels)<br />
style style<br />
=0 solid line<br />
=1 dashed line<br />
=2 dashed solid line<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 />
/****************** Draw an ellipse******************<br />
x,y screen coordinates (pixels) where the center is located<br />
a,b length of major axis and minor axis (pixels)<br />
color color (string value)<br />
size size (pixels)<br />
precision edge fineness<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 />
/**************** Draw polygon******************<br />
x,y screen coordinates (pixels) where the center is located<br />
r Polygon circumscribed circle radius (pixels)<br />
n Number of sides of a polygon<br />
color color (string value)<br />
size size (pixels)<br />
style style<br />
=0 solid line<br />
=1 dashed line<br />
=2 dashed solid line<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 function set example *******************<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>