用javascript實(shí)現(xiàn)畫板的代碼
更新時間:2007年09月05日 22:26:45 作者:
在控制臺中輸入
db.drawCircle([50,50],20,"black");
db.drawLine([5,5],[36,44],"red");
可以看到效果
復(fù)制代碼 代碼如下:
<body style="margin:0px;">
</body>
<script>
function DrawingBoard(width,height,size)
{
size=size||3
var container=document.createElement("div");
this.container=container;
container.runtimeStyle.width=(width)*size+"px";
container.runtimeStyle.height=(height)*size+"px";
container.runtimeStyle.margin="0px";
//container.style.border="solid 1px blue";
var count=0;
for(var y=0;y<height;y++)
{
for(var x=0;x<width;x++)
{
var curr=document.createElement("div");
curr.runtimeStyle.height=size+"px";
curr.runtimeStyle.width=size+"px";
curr.runtimeStyle.display="inline";
curr.runtimeStyle.overflow="hidden";
curr.style.backgroundColor="green";
curr.src="";
container.appendChild(curr);
}
}
//alert(curr.currentStyle.display);
//document.body.appendChild(container);
this.drawLine=function(start,end,color)
{
var dx=start[0]-end[0];
var dy=start[1]-end[1];
var x,y;
if( Math.abs(dx) > Math.abs(dy) )
{
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]) )
{
y=Math.round((x-start[0])/dx*dy+start[1]);
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
}
}
else
{
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]) )
{
x=Math.round((y-start[1])/dy*dx+start[0]);
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
}
}
}
this.drawCircle=function(m,R,color)
{
for(var r=0;r<=Math.floor(Math.sqrt(R*R-r*r));r++)
{
x=m[0]+r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
x=m[0]-r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
x=m[0]+r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
x=m[0]-r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
y=m[1]+r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
y=m[1]-r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
y=m[1]+r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
y=m[1]-r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r));
this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;
}
}
this.appendto=function(parent)
{
parent.appendChild(this.container);
}
this.drawPoint=function(p,color)
{
this.container.childNodes[this.trans(p)].style.backgroundColor=color;
}
this.trans=function(p)
{
return p[0]+p[1]*width;
}
container=null;
}
function Console(width,height,command)
{
var container=document.createElement("div");
this.container=container;
container.runtimeStyle.width=(width);
container.runtimeStyle.height=(height);
container.runtimeStyle.margin="0px";
container.runtimeStyle.backgroundColor="black";
container.runtimeStyle.fontFamily="Terminal";
container.runtimeStyle.color="white";
container.runtimeStyle.fontSize="16px";
this.output=document.createElement("div");
container.appendChild(this.output);
container.innerHTML+="js>"
this.input=document.createElement("input");
container.appendChild(this.input);
this.input.runtimeStyle.backgroundColor="black";
this.input.runtimeStyle.borderWidth="0px";
this.input.runtimeStyle.color="white";
this.input.runtimeStyle.fontFamily="Terminal";
this.input.runtimeStyle.width="90%"
this.input.runtimeStyle.fontSize="16px"
this.input.runtimeStyle.position="relative";
this.input.runtimeStyle.top="2px";
command=command||function(str)
{
var e;
try{
var r=eval(str);
} catch(e) {
return "Bad command";
}
return r;
}
this.run=function(str)
{
this.input.parentNode.childNodes[0].innerHTML+=str+'<br/>'
this.input.parentNode.childNodes[0].innerHTML+=(command(str)+"<br/>")
}
this.input.command=function()
{
this.parentNode.childNodes[0].innerHTML+=this.value+'<br/>'
this.parentNode.childNodes[0].innerHTML+=(command(this.value)+"<br/>")
}
this.input.onkeyup=new Function("e","e=e||event;if(e.keyCode!=13)return;this.command();this.value='';");
this.appendto=function(parent)
{
parent.appendChild(this.container);
}
container=null;
}
var c=new Console("100%","50%");
c.appendto(document.body);
c.run("window.db=new DrawingBoard(100,100);document.body.appendChild(db.container);");
</script>
您可能感興趣的文章:
- js+canvas實(shí)現(xiàn)畫板功能
- JS實(shí)現(xiàn)canvas簡單小畫板功能
- javascript+Canvas實(shí)現(xiàn)畫板功能
- javascript實(shí)現(xiàn)畫板功能
- javascript簡易畫板開發(fā)
- Javascript HTML5 Canvas實(shí)現(xiàn)的一個畫板
- JS基于ocanvas插件實(shí)現(xiàn)的簡單畫板效果代碼(附demo源碼下載)
- html5+javascript制作簡易畫板附圖
- 純js網(wǎng)頁畫板(Graphics)類簡介及實(shí)現(xiàn)代碼
- JS canvas實(shí)現(xiàn)畫板和簽字板功能
相關(guān)文章
JavaScript實(shí)現(xiàn)簡單動態(tài)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡單動態(tài)進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
JavaScript中的apply()方法和call()方法使用介紹
我們發(fā)現(xiàn)apply()和call()的真正用武之地是能夠擴(kuò)充函數(shù)賴以運(yùn)行的作用域,如果我們想用傳統(tǒng)的方法實(shí)現(xiàn)2012-07-07
使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果
這篇文章主要介紹了使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果的相關(guān)資料,需要的朋友可以參考下2017-01-01
淺談JS數(shù)組內(nèi)置遍歷方法有哪些和區(qū)別
本文主要介紹了淺談JS數(shù)組內(nèi)置遍歷方法有哪些和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

