Javascript 绘图 例子

1。适用于ie,firefox等浏览器的解决方案<例子>: --index1.html

 

<html>
<head>
<script Language="javascript">
function drawDot(x,y,color,size)
{
  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>");
}
function drawLine(x1,y1,x2,y2,color,size,style)
{
  var i;
  var r=Math.floor(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
  var theta=Math.atan((x2-x1)/(y2-y1));
  if(((y2-y1)<0&&(x2-x1)>0)||((y2-y1)<0&&(x2-x1)<0))
  {
   theta = Math.PI + theta;
  }
  var dx=Math.sin(theta);
  var dy=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;
    }
  }
}

function drawFilledRect(x1,y1,x2,y2,color)
{
  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>");
}
function drawRect(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);
}
function drawOval(x,y,a,b,color,size,precision)
{
  var i;
  var iMax=2*Math.PI;
  var step=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);
  }
}
function drawPoly(x,y,r,n,color,size,style)
{
  var i;
  var theta=Math.PI;
  var x1=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;
  }
}
</script>
</head>
<body>
<script Language="javascript">
drawLine(20,20,300,20,"#0000cc",2,0);
drawLine(20,40,300,40,"#0000cc",2,1);
drawLine(20,60,300,60,"#0000cc",2,2);
drawFilledRect(20,80,300,200,"009900");
drawRect(20,220,220,320,"ff0000",2,0);
drawRect(240,220,440,320,"ff0000",2,1);
drawRect(460,220,660,320,"ff0000",2,2);
drawOval(250,450,120,50,"006600",1,1);
drawOval(250,650,120,120,"006600",2,0.5);
drawPoly(200,900,100,3,"ff8800",2,0);
drawPoly(400,900,100,4,"ff8800",2,1);
drawPoly(600,900,100,5,"ff8800",2,2);
drawPoly(200,1100,100,6,"ff8800",2,0);
drawPoly(400,1100,100,7,"ff8800",2,1);
drawPoly(600,1100,100,12,"ff8800",2,2);
</script>
</body>
</html> 

2。利用vml的绘图<例子>,适用于ie --index2.html

 


  <html   xmlns:v="urn:schemas-microsoft-com:vml">  
  <META   content="text/html;   charset=gb2312"   http-equiv=content-type>  
  <SCRIPT   LANGUAGE=javascript>  
  <!--  
  var   Ox="";
  var   Oy="";
  var   LongCase=250;  
  var   LongX=250;
  var   LongY=250;//mondify  
  var   caseX=25;//
  var   caseY=25;// 
  var   ArrayX=new   Array();  
  var   ArrayY=new   Array();  
  ArrayX[0]=0;  
  ArrayY[0]=0;  
   
  ArrayX[1]=33;  
  ArrayY[1]=33;  
   
  ArrayX[2]=40;  
  ArrayY[2]=20;  
   
  ArrayX[3]=50;  
  ArrayY[3]=40;  
   
  ArrayX[4]=60;  
  ArrayY[4]=10;  
   
  ArrayX[5]=65;  
  ArrayY[5]=20;  
   
  ArrayX[6]=80;  
  ArrayY[6]=80;  
   
  ArrayX[7]=85;  
  ArrayY[7]=85;  
   
  ArrayX[8]=99;  
  ArrayY[8]=99;  
  function   mouse()  
  {  
  window.status=event.y+","+event.x     ;  
  }  
  function   mousex()  
  {  
  alert(window.event.offsetY);  
  }  
  function   sColor(TakeMode)  
  {  
  var   sel=window.showModalDialog("selcolor.html",   "",   "dialogWidth:18.5em;   dialogHeight:17.5em;   status:0");  
   
  if   (sel!=null)  
  {  
  //alert(sel);  
  for   (var   i=0;i<document.all(TakeMode).length;i++)  
  {  
  document.all(TakeMode)[i].strokecolor=sel;  
  }  
  }  
  }  
  //-->  
  </SCRIPT>  
      <head>  
      <object   id='VMLRender'   classid='CLSID:10072CEC-8CC1-11D1-986E-00A0C955B42E'></object>  
       
        <style>  
              v/:*   {   behavior:   url(#VMLRender);   }  
           
          .Scale{font-family   :   Comic   Sans   Ms;font-size   :   9pt;font-weight   :   bold;color:white;}  
            .Scale2{font-family   :   Comic   Sans   Ms;font-size   :   7pt;font-weight   :   bold;color:white;}  
          </style>  
                <SCRIPT   ID=clientEventHandlersJS   LANGUAGE=javascript>  
  <!--  
   
  function   document_oncontextmenu()   {  
  window.event.returnValue=0;  
  }  
   
  //-->  
  </SCRIPT>  
   
  <SCRIPT   LANGUAGE=javascript   FOR=document   EVENT=oncontextmenu>  
  <!--  
    document_oncontextmenu()  
  //-->  
  </SCRIPT>  
  </head>  
      <body   οnmοusemοve=mouse()     bgcolor=Black>  
  <table   style="position:absolute;top:0pt;left:0pt;HEIGHT:330pt;WIDTH:310pt;"   >  
  <tr><td></td></tr></table>  
  <!--<TEXTAREA   rows=40   cols=40   id=textarea1   name=textarea1>   background="main.bmp"  
  25,45</TEXTAREA>-->  
  <SCRIPT   LANGUAGE=javascript>  
  <!--  
  var   DocumentX="";  
  for   (var   nowX=caseX;nowX<LongX;nowX=nowX+caseX)//mondify  
  {  
  DocumentX=DocumentX+"<v:line   id=Firds   name=Firds   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;'   from="+nowX+"pt,0pt     to="+nowX+"pt,"+LongY+"pt   strokecolor=#696969     strokeweight=1pt></v:line>";  
  }  
  for   (var   nowY=caseY;nowY<LongY;nowY=nowY+caseY)  
  {  
  DocumentX=DocumentX+"<v:line   id=Firds   name=Firds   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;'   from=0pt,"+nowY+"pt     to="+LongX+"pt,"+nowY+"pt   strokecolor=#696969   strokeweight=1pt></v:line>";  
  }  
  //textarea1.value=DocumentX;  
  document.write(DocumentX);  
  document.write("<v:line   id=FirdsY   name=FirdsY   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;'   from=0pt,-10pt     to=0pt,"+LongCase+"pt   strokecolor=white   strokeweight=0.5pt></v:line>");  
  document.write("<v:line   id=FirdsX   name=FirdsX   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;'   from=0pt,"+LongCase+"pt     to="+(LongCase+30)+"pt,"+LongCase+"pt   strokecolor=white   strokeweight=0.5pt></v:line>");  
  document.write   ("<v:polyline   style=position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt   points   ='0pt,-10pt   0,-20pt   3pt,-10pt'   fillcolor   =white   strokecolor=white   strokeweight   =0pt></v:polyline>");  
  document.write   ("<v:polyline   style=position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt   points   ='280pt,250pt   270pt,250pt   270pt,247pt'   fillcolor   =white   strokecolor=white   strokeweight   =0pt></v:polyline>");  
   
  var   bnTd=0;  
  for   (var   tdX=100;tdX>=0;tdX=tdX-1)  
  {  
  bnTd=bnTd+1;  
  if   (Math.ceil(tdX/2)*2==Math.ceil(tdX))  
  {  
  if   (Math.ceil(Math.ceil(tdX/5)*5)==Math.ceil(tdX))  
  {  
  document.write("<div   align=right   class=Scale   style='position:absolute;left:0pt;top:"+Math.ceil(Math.ceil(tdX*(250/100))+45)+"pt;width:45pt'>");  
  document.write   (100-tdX);  
  document.write   ("&nbsp;</div>");  
  document.write("<v:line   id=tdX   name=tdX   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from=0pt,"+tdX*(250/100)+"pt     to=10pt,"+tdX*(250/100)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
  }  
  else  
  document.write("<v:line   id=tdX   name=tdX   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from=0pt,"+tdX*(250/100)+"pt     to=5pt,"+tdX*(250/100)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
   
  }  
  else  
  {  
  if   (Math.ceil(Math.ceil(tdX/5)*5)==Math.ceil(tdX))  
  {  
  document.write("<v:line   id=tdX   name=tdX   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from=0pt,"+tdX*(250/100)+"pt     to=10pt,"+tdX*(250/100)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
  }  
  else  
  {  
  document.write("<v:line   id=tdX   name=tdX   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from=0pt,"+tdX*(250/100)+"pt     to=3pt,"+tdX*(250/100)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
  }  
  }  
  }  
  bnTd=0;  
  for   (var   tdY=100;tdY>=0;tdY=tdY-1)  
  {  
  bnTd=bnTd+1;  
  if (Math.ceil(tdY/2)*2==Math.ceil(tdY))  
  {  
   if (Math.ceil(Math.ceil(tdY/5)*5)==Math.ceil(tdY))  
   {  
    if (tdY!=0)  
    {  
    document.write("<div   align=right   class=Scale   style='position:absolute;left:"+Math.ceil(Math.ceil(tdY*(LongCase/100))+10)+"pt;top:"+(LongCase+55)+"pt;width:45pt'>");  
    document.write   (tdY);  
    document.write   ("&nbsp;</div>");  
    document.write("<v:line   id=tdY   name=tdY   style='position:absolute;top:"+(LongCase+40)+"pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from="+tdY*(LongCase/100)+"pt,0pt     to="+tdY*(LongCase/100)+"pt,10pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
    }  
   }  
   else
   {
    document.write("<v:line   id=tdY   name=tdY   style='position:absolute;top:"+(LongCase+45)+"pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from="+tdY*(LongCase/100)+"pt,0pt     to="+tdY*(LongCase/100)+"pt,5pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
   }
   
  }  
  else  
  {  
   if(Math.ceil(Math.ceil(tdY/5)*5)==Math.ceil(tdY))  
   {  
   document.write("<v:line   id=tdY   name=tdY   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from="+tdY*(250/100)+"pt,250pt     to="+tdY*(250/100)+"pt,"+(250-10)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
   }  
   else  
   {  
   document.write("<v:line   id=tdY   name=tdY   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:2'   from="+tdY*(250/100)+"pt,250pt     to="+tdY*(250/100)+"pt,"+(250-3)+"pt   strokecolor=white   strokeweight=0.25pt></v:line>");  
   }  
  }  
  }  
   
  //drawLine  
  function   drawLine(LineX1,LineY1,LineX2,LineY2)  
  {  
   //translate   X.  
   var   sarX1=250*LineX1/100;  
   var   sarX2=250*LineX2/100;  
   //translate   Y.  
   var   sarY1=(100-LineY1)*250/100;  
   var   sarY2=(100-LineY2)*250/100;  
   //draw   Line.  
   document.write("<v:line   id=line   name=line   style='position:absolute;top:50pt;left:45pt;HEIGHT:200pt;WIDTH:200pt;z-index:5'   from="+sarX1+"pt,"+sarY1+"pt     to="+sarX2+"pt,"+sarY2+"pt   strokecolor=#ffd700   strokeweight=0.25pt></v:line>");  
  }  
   
  //drawLine  
   
  var   tempX=0;  
  var   tempY=0;  
   
  function   beginDraw()  
  {  
   
   for(var   i=0;i<=ArrayX.length-1;i++)  
   {  
  
    if   (i==0)  
    {  
     tempX=ArrayX[0];  
     tempY=ArrayY[0];  
    }  
    else  
    {  
     tempX=ArrayX[i-1];  
     tempY=ArrayY[i-1];  
     drawLine(tempX,tempY,ArrayX[i],ArrayY[i])  
    }  
   }  
   
  }  
  beginDraw();  
  //-->  
  </SCRIPT>  
  <div   style="   BORDER-BOTTOM:  buttonshadow   1px solid;  
                           BORDER-LEFT:  buttonhighlight   1px solid;  
                           BORDER-RIGHT: buttonshadow   1px solid;  
                           BORDER-TOP: buttonhighlight   1px solid;
                           position: absolute;BORDER-LEFT-COLOR:oldlace;  
                           BORDER-TOP-COLOR:  oldlace;position:absolute;
                           top:310pt;left:15pt;z-index:3">  
   
  <div  style="position:absolute;top:18pt;left:0pt;z-index:4;"  class=Scale2   align=right> </div>   
  </body>  
  </html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值