純js網(wǎng)頁畫板(Graphics)類簡介及實現(xiàn)代碼
更新時間:2012年12月24日 18:13:58 作者:
今天需要在網(wǎng)頁上畫一個圖譜,想到用JS,經(jīng)過學(xué)習(xí),和網(wǎng)上搜索,經(jīng)過整理優(yōu)化得到下面代碼,注意不是用HTML5的canvas,而是用的純js,需要了解的朋友可以參考下
今天需要在網(wǎng)頁上畫一個圖譜,想到用JS,經(jīng)過學(xué)習(xí),和網(wǎng)上搜索,經(jīng)過整理優(yōu)化得到下面代碼,注意不是用HTML5的canvas,而是用的純js
/* 以下畫點,畫線,畫圓的方法,都不是用HTML5的canvas,而是用的純js
用到了一些數(shù)學(xué)的三角函數(shù)方法
以下代碼是課堂隨機寫出,沒有做更多優(yōu)化
*/
/*
面向?qū)ο蠓庋b,添加繪制矩形
進一步優(yōu)化代碼
*/
var Graphics = function(divId, color){
this.divId = divId;
this.color = color; //'#000000'或'black'
this.drawPoint= function(x,y){
//畫點
var oDiv=document.createElement('div');
oDiv.style.position='absolute';
oDiv.style.height='2px';
oDiv.style.width='2px';
oDiv.style.backgroundColor=this.color;
oDiv.style.left=x+'px';
oDiv.style.top=y+'px';
//document.body.appendChild(oDiv);
return oDiv;//注意:返回的值是一個dom節(jié)點,但并未追加到文檔中
};
this.drawLine = function(x1,y1,x2,y2){
//畫一條線段的方法。(x1,y1),(x2,y2)分別是線段的起點終點
var x=x2-x1;//寬
var y=y2-y1;//高
var frag=document.createDocumentFragment();
if(Math.abs(y)>Math.abs(x)){//那個邊更長,用那邊來做畫點的依據(jù)(就是下面那個循環(huán)),如果不這樣,當(dāng)是一條垂直線或水平線的時候,會畫不出來
if(y>0)//正著畫線是這樣的
for(var i=0;i<y;i++){
var width=x/y*i //x/y是直角兩個邊長的比,根據(jù)這個比例,求出新坐標(biāo)的位置
{
frag.appendChild(drawPoint(width+x1,i+y1));
}
}
if(y<0){//有時候是倒著畫線的
for(var i=0;i>y;i--){
var width=x/y*i
{
frag.appendChild(drawPoint(width+x1,i+y1));
}
}
}
}//end if
else {
if(x>0)//正著畫線是這樣的
for(var i=0;i<x;i++){
var height=y/x*i
{
frag.appendChild(drawPoint(i+x1,height+y1));
}
}
if(x<0){//有時候是倒著畫線的
for(var i=0;i>x;i--){
var height=y/x*i
{
frag.appendChild(this.drawPoint(i+x1,height+y1));
}
}
}//end if
}
document.getElementById(this.divId).appendChild(frag);
};
this.drawCircle = function(r, x, y){
//畫個圓。x,原點橫坐標(biāo);y,原點縱坐標(biāo);r,半徑
var frag=document.createDocumentFragment();
for(var degree=0;degree<360;degree+=2){
var x1=r*Math.sin(degree*Math.PI/180);
var y1=r*Math.cos(degree*Math.PI/180);
frag.appendChild(drawPoint(x+x1,y+y1));
}
document.body.appendChild(frag);
};
this.dragCircle = function(x1,y1,x2,y2){
//拖出一個圓來
var r=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));//求出半徑的長 直角三角形中 斜邊的平方=兩個直邊的平方之和
var frag=document.createDocumentFragment();
for(var degree=0;degree<360;degree+=2){//每隔2度畫一個點
var x2=r*Math.sin(degree*Math.PI/180);
var y2=r*Math.cos(degree*Math.PI/180);
frag.appendChild(drawPoint(x1+x2,y1+y2));
}
document.getElementById(this.divId).appendChild(frag);
};
this.drawRect = function(startX, startY, lengthX, lengthY, newId, text){
//(startX, startY)起點坐標(biāo),lengthX,長 lengthY,寬 newId,新創(chuàng)建的div的Id text,div內(nèi)容
var myDiv=document.createElement('div');
if(newId){
myDiv.id=newId;
}
myDiv.style.width= lengthX + 'px';
myDiv.style.height= lengthY + 'px';
myDiv.style.backgroundColor = this.color;
myDiv.style.left=startX + 'px';
myDiv.style.top=startY + 'px';
myDiv.style.textAlign= 'center';
if(text){
myDiv.innerHTML = text;
}
document.getElementById(this.divId).appendChild(myDiv);
};
};
window.onload=function(){
var g = new Graphics('div1', 'red');
g.drawLine(500,30,0,30);
g.color = '#CDC9C9';
g.drawRect(10,10,200,200, '', '測試');
}
復(fù)制代碼 代碼如下:
/* 以下畫點,畫線,畫圓的方法,都不是用HTML5的canvas,而是用的純js
用到了一些數(shù)學(xué)的三角函數(shù)方法
以下代碼是課堂隨機寫出,沒有做更多優(yōu)化
*/
/*
面向?qū)ο蠓庋b,添加繪制矩形
進一步優(yōu)化代碼
*/
var Graphics = function(divId, color){
this.divId = divId;
this.color = color; //'#000000'或'black'
this.drawPoint= function(x,y){
//畫點
var oDiv=document.createElement('div');
oDiv.style.position='absolute';
oDiv.style.height='2px';
oDiv.style.width='2px';
oDiv.style.backgroundColor=this.color;
oDiv.style.left=x+'px';
oDiv.style.top=y+'px';
//document.body.appendChild(oDiv);
return oDiv;//注意:返回的值是一個dom節(jié)點,但并未追加到文檔中
};
this.drawLine = function(x1,y1,x2,y2){
//畫一條線段的方法。(x1,y1),(x2,y2)分別是線段的起點終點
var x=x2-x1;//寬
var y=y2-y1;//高
var frag=document.createDocumentFragment();
if(Math.abs(y)>Math.abs(x)){//那個邊更長,用那邊來做畫點的依據(jù)(就是下面那個循環(huán)),如果不這樣,當(dāng)是一條垂直線或水平線的時候,會畫不出來
if(y>0)//正著畫線是這樣的
for(var i=0;i<y;i++){
var width=x/y*i //x/y是直角兩個邊長的比,根據(jù)這個比例,求出新坐標(biāo)的位置
{
frag.appendChild(drawPoint(width+x1,i+y1));
}
}
if(y<0){//有時候是倒著畫線的
for(var i=0;i>y;i--){
var width=x/y*i
{
frag.appendChild(drawPoint(width+x1,i+y1));
}
}
}
}//end if
else {
if(x>0)//正著畫線是這樣的
for(var i=0;i<x;i++){
var height=y/x*i
{
frag.appendChild(drawPoint(i+x1,height+y1));
}
}
if(x<0){//有時候是倒著畫線的
for(var i=0;i>x;i--){
var height=y/x*i
{
frag.appendChild(this.drawPoint(i+x1,height+y1));
}
}
}//end if
}
document.getElementById(this.divId).appendChild(frag);
};
this.drawCircle = function(r, x, y){
//畫個圓。x,原點橫坐標(biāo);y,原點縱坐標(biāo);r,半徑
var frag=document.createDocumentFragment();
for(var degree=0;degree<360;degree+=2){
var x1=r*Math.sin(degree*Math.PI/180);
var y1=r*Math.cos(degree*Math.PI/180);
frag.appendChild(drawPoint(x+x1,y+y1));
}
document.body.appendChild(frag);
};
this.dragCircle = function(x1,y1,x2,y2){
//拖出一個圓來
var r=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));//求出半徑的長 直角三角形中 斜邊的平方=兩個直邊的平方之和
var frag=document.createDocumentFragment();
for(var degree=0;degree<360;degree+=2){//每隔2度畫一個點
var x2=r*Math.sin(degree*Math.PI/180);
var y2=r*Math.cos(degree*Math.PI/180);
frag.appendChild(drawPoint(x1+x2,y1+y2));
}
document.getElementById(this.divId).appendChild(frag);
};
this.drawRect = function(startX, startY, lengthX, lengthY, newId, text){
//(startX, startY)起點坐標(biāo),lengthX,長 lengthY,寬 newId,新創(chuàng)建的div的Id text,div內(nèi)容
var myDiv=document.createElement('div');
if(newId){
myDiv.id=newId;
}
myDiv.style.width= lengthX + 'px';
myDiv.style.height= lengthY + 'px';
myDiv.style.backgroundColor = this.color;
myDiv.style.left=startX + 'px';
myDiv.style.top=startY + 'px';
myDiv.style.textAlign= 'center';
if(text){
myDiv.innerHTML = text;
}
document.getElementById(this.divId).appendChild(myDiv);
};
};
window.onload=function(){
var g = new Graphics('div1', 'red');
g.drawLine(500,30,0,30);
g.color = '#CDC9C9';
g.drawRect(10,10,200,200, '', '測試');
}
相關(guān)文章
blob轉(zhuǎn)換成string格式同步調(diào)用問題解決分析
這篇文章主要為大家介紹了blob轉(zhuǎn)換成string格式同步調(diào)用問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
用js實現(xiàn)的自定義的對話框的實現(xiàn)代碼
javascript alert函數(shù)的替代方案,一個自定義的對話框的方法2010-03-03
JavaScript中擴展Array contains方法實例
這篇文章主要介紹了JavaScript中擴展Array contains方法實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03
JavaScript實現(xiàn)簡潔的俄羅斯方塊完整實例
這篇文章主要介紹了JavaScript實現(xiàn)簡潔的俄羅斯方塊,以完整實例形式分析了JavaScript實現(xiàn)俄羅斯方塊游戲的具體技巧,代碼備有詳盡的注釋便于理解,需要的朋友可以參考下2016-03-03

