<!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>