在控制台中输入
db.drawCircle([50,50],20,"black");
db.drawLine([5,5],[36,44],"red");
可以看到效果
<script> <BR> function DrawingBoard(width,height,size) <BR> { <BR> size=size||3 <BR> var container=document.createElement("div"); <BR> this.container=container; <br><br> container.runtimeStyle.width=(width)*size+"px"; <BR> container.runtimeStyle.height=(height)*size+"px"; <BR> container.runtimeStyle.margin="0px"; <BR> //container.style.border="solid 1px blue"; <BR> var count=0; <BR> for(var y=0;y<height;y++) <BR> { <BR> for(var x=0;x<width;x++) <BR> { <BR> var curr=document.createElement("div"); <BR> curr.runtimeStyle.height=size+"px"; <BR> curr.runtimeStyle.width=size+"px"; <BR> curr.runtimeStyle.display="inline"; <BR> curr.runtimeStyle.overflow="hidden"; <BR> curr.style.backgroundColor="green"; <BR> curr.src=""; <BR> container.appendChild(curr); <BR> } <BR> } <BR> //alert(curr.currentStyle.display); <BR> //document.body.appendChild(container); <br><br> this.drawLine=function(start,end,color) <BR> { <BR> var dx=start[0]-end[0]; <BR> var dy=start[1]-end[1]; <BR> var x,y; <br><br> if( Math.abs(dx) > Math.abs(dy) ) <BR> { <BR> for(var x=start[0];x!=end[0]+(end[0]-start[0])/Math.abs(end[0]-start[0]);x+=(end[0]-start[0])/Math.abs(end[0]-start[0]) ) <BR> { <BR> y=Math.round((x-start[0])/dx*dy+start[1]); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> } <BR> } <BR> else <BR> { <BR> for(var y=start[1];y!=end[1]+(end[1]-start[1])/Math.abs(end[1]-start[1]);y+=(end[1]-start[1])/Math.abs(end[1]-start[1]) ) <BR> { <BR> x=Math.round((y-start[1])/dy*dx+start[0]); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> } <BR> } <BR> } <BR> this.drawCircle=function(m,R,color) <BR> { <br><br> for(var r=0;r<=Math.floor(Math.sqrt(R*R-r*r));r++) <BR> { <br><br> x=m[0]+r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> x=m[0]-r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> x=m[0]+r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> x=m[0]-r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> y=m[1]+r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> y=m[1]-r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> y=m[1]+r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <BR> y=m[1]-r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r)); <BR> this.container.childNodes[this.trans([x,y])].style.backgroundColor=color; <br><br> } <br><br><BR> } <BR> this.appendto=function(parent) <BR> { <BR> parent.appendChild(this.container); <BR> } <br><br> this.drawPoint=function(p,color) <BR> { <BR> this.container.childNodes[this.trans(p)].style.backgroundColor=color; <BR> } <BR> this.trans=function(p) <BR> { <BR> return p[0]+p[1]*width; <BR> } <br><br> container=null; <BR> } <BR> function Console(width,height,command) <BR> { <BR> var container=document.createElement("div"); <BR> this.container=container; <br><br> container.runtimeStyle.width=(width); <BR> container.runtimeStyle.height=(height); <BR> container.runtimeStyle.margin="0px"; <BR> container.runtimeStyle.backgroundColor="black"; <BR> container.runtimeStyle.fontFamily="Terminal"; <BR> container.runtimeStyle.color="white"; <BR> container.runtimeStyle.fontSize="16px"; <BR> this.output=document.createElement("div"); <BR> container.appendChild(this.output); <BR> container.innerHTML+="js>" <BR> this.input=document.createElement("input"); <BR> container.appendChild(this.input); <BR> this.input.runtimeStyle.backgroundColor="black"; <BR> this.input.runtimeStyle.borderWidth="0px"; <BR> this.input.runtimeStyle.color="white"; <BR> this.input.runtimeStyle.fontFamily="Terminal"; <BR> this.input.runtimeStyle.width="90%" <BR> this.input.runtimeStyle.fontSize="16px" <BR> this.input.runtimeStyle.position="relative"; <BR> this.input.runtimeStyle.top="2px"; <BR> command=command||function(str) <BR> { <br><br> var e; <BR> try{ <BR> var r=eval(str); <BR> } catch(e) { <BR> return "Bad command"; <BR> } <BR> return r; <br><br> } <BR> this.run=function(str) <BR> { <br><br> this.input.parentNode.childNodes[0].innerHTML+=str+'<br/>' <BR> this.input.parentNode.childNodes[0].innerHTML+=(command(str)+"<br/>") <br><br> } <BR> this.input.command=function() <BR> { <BR> this.parentNode.childNodes[0].innerHTML+=this.value+'<br/>' <BR> this.parentNode.childNodes[0].innerHTML+=(command(this.value)+"<br/>") <BR> } <BR> this.input.onkeyup=new Function("e","e=e||event;if(e.keyCode!=13)return;this.command();this.value='';"); <BR> this.appendto=function(parent) <BR> { <BR> parent.appendChild(this.container); <BR> } <BR> container=null; <BR> } <br><br> var c=new Console("100%","50%"); <BR> c.appendto(document.body); <BR> c.run("window.db=new DrawingBoard(100,100);document.body.appendChild(db.container);"); <BR></script>