一个用JS作的简单的画图程序,可改画线粗细和顡色,觉得挺有意思的,所以放上来,以供相互学习

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<style type="text/css">
.style1 {
    font-size: x-small;
}
</style>
</head>

<script type="text/javascript">
var w=1;
var h=1;
var col='black';
var drawElement=0;//表示画哪种元素
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var preStringLength=0;
var lastStringLength=0;
function add1()
{
w+=1;
h+=1;

}
function cut1()
{
if(w>1&&h>1)
{
w-=1;
h-=1;
}
}
function catchMouse()
{


var x=event.clientX-50;
var y=event.clientY-52;

makedot(x,y);
window.setTimeOut('catchMouse()',3);

}
function makedot(x,y){ 

var sp=document.getElementById("sspp");
sp.insertAdjacentHTML("BeforeEnd","<hr style='position:absolute;left:"+x+"px;top:"+y+"px;width:"+w+"px;height:"+h+"px;color:"+col+";'>");
 
}
function changeColor(a)
{
col=a.value;
}

function circle(xx1,yy1,xx2,yy2){ 
 var x,y,r;
 x=xx1;
 y=yy1;
 r=Math.sqrt(Math.abs(xx1-xx2)*Math.abs(xx1-xx2)+Math.abs(yy1-yy2)*Math.abs(yy1-yy2));
 var dotx,doty,radio;
 var Pi=Math.PI;
 makedot(x,y);
 for(var i=0;i<360;i+=0.5){
  radio=i*Pi/180;
  dotx=r*Math.cos(radio)+x;
  doty=r*Math.sin(radio)+y;
  makedot(dotx,doty);
 }
}

function rect(x1,y1,x2,y2){ //(x,y)Ͻ,w,h
 var x,y,w,h;
 x=x1;
 y=y1;
 w=Math.abs(x2-x1);
 h=Math.abs(y2-y1);
 for(var i=0;i<w;i++){
  makedot(x+i,y);
  makedot(x+i,y+h);
 }
 for(var i=0;i<h;i++){
  makedot(x,y+i);
  makedot(x+w,y+i);
 }
}


function line(x1,y1,x2,y2){
 var slope=(y2-y1)/(x2-x1); 
 var diff=x2-x1;
 if(x1<x2){
  for(var i=0;i<diff;i++){
   makedot(x1+i,y1+slope*i);
  }
 }else if(x1>x2){
  for(var i=0;i>diff;i--){
   makedot(x1+i,y1+slope*i);
  }
 }else{ 
  var temp=y2-y1;
  if(temp>0){
   for(var i=0;i<temp;i++){
    makedot(x1,y1+i);
   }
  }else{
   for(var i=0;i>temp;i--){
    makedot(x1,y1+i);
   }
  }
 }
}


function draw(a)
{
drawElement=a;
}
function MouseD()
{
switch(drawElement)
{
 case 0: document.οnmοusemοve=catchMouse;break;
 case 1:
 case 2:
 case 3:
 case 4:
 case 5:x1=event.clientX-50;y1=event.clientY-52;
 default:break;
}
var sp=document.getElementById("sspp");
preStringLength=sp.innerHTML.length;
}
function MouseU()
{
x2=event.clientX-50;y2=event.clientY-52;
switch(drawElement)
{
 case 0: document.οnmοusemοve=null;break;
 case 1: line(x1,y1,x2,y2);
   break;
 case 2: circle(x1,y1,x2,y2);break;
 case 3: rect(x1,y1,x2,y2);break;
 case 4:break;
 case 5:break;
 default:break;
}
var sp=document.getElementById("sspp");
lastStringLength=sp.innerHTML.length;

}
var spStr;
function drawCancel()
{
var sp=document.getElementById("sspp");
if(preStringLength<lastStringLength)
spStr=sp.innerHTML.substring(preStringLength+1,lastStringLength);
sp.innerHTML=sp.innerHTML.substring(0,preStringLength);
}
function drawRedo()
{
var sp=document.getElementById("sspp");
sp.insertAdjacentHTML("BeforeEnd",spStr);
}
</script>

<body>

<span id="sspp"  onMouseDown="MouseD()" onMouseUp="MouseU()"  οnmοuseοver="this.focus();" style="position:absolute;top:50px;left:50px;width:500px;height:500px;background-color:#eeeeee ">

</span>
<form action="" style="position:absolute;top:550px;left:50px; ">
<input type="button" onClick="drawCancel()" name="cancel" value="undo">
<input type="button" onClick="drawRedo()" name="cancel" value="redo">
<input type="button" onClick="add1()" name="add" value="+">
<input type="button" onClick="cut1()"  name="cut" value="-">
<input type="button" onClick="draw(0)"  name="point" value="点">
<input type="button" onClick="draw(1)"  name="line" value="线">
<input type="button" onClick="draw(2)"  name="circle" value="圆">

<input type="button" onClick="draw(3)"  name="rect" value="方形">


<select id="col" onChange="changeColor(this)">
<option value="black">black</option>
<option value="blue">blue</option>
<option value="green">green</option>
<option value="orange">orange</option>
<option value="red">red</option>
<option value="pink">pink</option>
</select>
</form>

</body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值