純js網頁畫板(Graphics)類簡介及實現代碼

來源:互聯網
上載者:User

今天需要在網頁上畫一個圖譜,想到用JS,經過學習,和網上搜尋,經過整理最佳化得到下面代碼,注意不是用HTML5的canvas,而是用的純js
複製代碼 代碼如下:
/* 以下畫點,畫線,畫圓的方法,都不是用HTML5的canvas,而是用的純js
用到了一些數學的三角函數方法
以下代碼是課堂隨機寫出,沒有做更多最佳化
*/
/*
物件導向封裝,添加繪製矩形
進一步最佳化代碼
*/
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節點,但並未追加到文檔中
};
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)){//那個邊更長,用那邊來做畫點的依據(就是下面那個迴圈),如果不這樣,當是一條垂直線或水平線的時候,會畫不出來
if(y>0)//正著畫線是這樣的
for(var i=0;i<y;i++){
var width=x/y*i //x/y是直角兩個邊長的比,根據這個比例,求出新座標的位置
{

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,原點橫座標;y,原點縱座標;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)起點座標,lengthX,長 lengthY,寬 newId,新建立的div的Id text,div內容
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, '', '測試');
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.