一个小程序,自己做的,实习的题目,呵呵,把代码粘出来做个记录吧。
这个程序是用javascript自定义的类来完成,主要实现的功能:画圆,并可以拖动。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>drawCircle</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>
<body>
<p> </p>
<p>条件</p>
<p>
半 径 :
<input type="text" name="circleR" id="circleR">
</p>
<div id="Layer1" style="position:absolute; width:200px; height:115px; z-index:1; left: 338px; top: 85px; cursor:pointer" onmousedown="startDrag1(this)" onmouseup="stopDrag1(this)" onmousemove="drag1(this)"></div>
<p>圆心横坐标:
<input type="text" name="circleX" id="circleX">
</p>
<p>圆心纵坐标:
<input type="text" name="circleY" id="circleY">
</p>
<p>
<input type="button" name="Button1" value="画圆" onClick="doDraw()">
</p>
<p> </p>
</body>
</html>
<script language="javascript" type="text/javascript">
var x0=0,y0=0,x1=0,y1=0;
var moveable=false;
function circle(r,xpos,ypos)
{
this.r = r;
this.xpos=xpos;
this.ypos=ypos;
this.drawCircle=function(obj){
var n = 0;
var result='';
for (var i=0;i<2*Math.PI;i+=0.01)
{
PointX = Math.sin(i) * this.r + this.xpos;
PointY = Math.cos(i) * this.r + this.ypos;
result+='<div id="Point' + n +
'" style="position: absolute; left: ' + PointX +
'px; top: ' + PointY +
'px; width: 2px; height: 2px; "><strong>.</strong></div>';
n++;
}
obj.style.left=xpos-r;
obj.style.top=ypos-r;
obj.style.width=2*r;
obj.style.height=2*r;
obj.innerHTML=result;
}
this.startDrag=function(win){
if(event.button==1){
x0 = event.clientX;
y0 = event.clientY;
x1 = parseInt(win.offsetLeft);
y1 = parseInt(win.offsetTop);
moveable = true;
}
}
this.drag=function(win)
{
if(moveable){
win.style.left = x1 + event.clientX - x0;
win.style.top = y1 + event.clientY - y0;
}
}
this.stopDrag=function(win)
{
if(moveable){
win.releaseCapture();
moveable = false;
}
}
}
function doDraw()
{
theCircle = new circle(Number(this.document.getElementById("circleR").value),Number(this.document.getElementById("circleX").value),Number(this.document.getElementById("circleY").value));
theCircle.drawCircle(this.document.getElementById("Layer1"));
}
//开始拖动;
function startDrag1(obj){
theCircle = new circle();
obj.setCapture();
theCircle.startDrag(obj);
}
//拖动;
function drag1(obj){
theCircle = new circle();
theCircle.drag(obj);
}
//停止拖动;
function stopDrag1(obj){
theCircle = new circle();
theCircle.stopDrag(obj);
}
</script>