久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

【HTML5】3D模型--百行代碼實現旋轉立體魔方實例

本篇文章主要介紹【HTML5】3D模型--百行代碼實現旋轉立體魔方實例,具有一定的參考價值,有需要的可以了解一下。

最近研究魔方的玩法,就突然想用HMTL5寫一個魔方的模型,由于魔方是一個3D的立方體,這次就試著用HTML5寫了一個簡單的3D模型。

下面是預覽畫面。

制作流程

首先你需要下載Html5開源庫件lufylegend-1.4.0

魔方分為6個面,每個面由9個小矩形組成,現在我把每個小矩形當做一個類封裝起來,

因為現在建立的是一個3D魔方,所以要畫出每個小矩形,需要知道小矩形的4個定點,而這4個定點會根據空間的旋轉角度而變換,所以為了計算出這4個定點坐標,需要知道魔方繞x軸和z軸旋轉的角度。

所以,建立矩形類如下

function Rect(pointA,pointB,pointC,pointD,angleX,angleZ,color){  
    base(this,LSprite,[]);  
    this.pointZ=[(pointA[0]+pointB[0]+pointC[0]+pointD[0])/4,(pointA[1]+pointB[1]+pointC[1]+pointD[1])/4,(pointA[2]+pointB[2]+pointC[2]+pointD[2])/4];  
    this.z = this.pointZ[2];  
    this.pointA=pointA,this.pointB=pointB,this.pointC=pointC,this.pointD=pointD,this.angleX=angleX,this.angleZ=angleZ,this.color=color;  
}  
  
Rect.prototype.setAngle = function(a,b){  
    this.angleX = a;  
    this.angleZ = b;  
    this.z=this.getPoint(this.pointZ)[2];  
};  

pointA,pointB,pointC,pointD是小矩形的四個頂點,angleX,angleZ分別是x軸和z軸旋轉的角度,color是小矩形的顏色。

魔方分為6個面,先看一下最前面的一面,如果以立方體的中心作為3D坐標系的中心,那么9個小矩形的各個定點所對應的坐標如下圖所示

所以,前面這個面的9個小矩形可以由下面的代碼來建立

for(var x=0;x<3;x++){  
    for(var y=0;y<3;y++){  
        z = 3;  
        var rect = new Rect([-3*step + x*2*step,-3*step + y*2*step,-3*step + z*2*step],[-step + x*2*step,-3*step + y*2*step,-3*step + z*2*step],[-step + x*2*step,-step + y*2*step,-3*step + z*2*step],[-3*step + x*2*step,-step + y*2*step,-3*step + z*2*step],0,0,"#FF0000");  
        backLayer.addChild(rect);  
    }  
}  

其中backLayer是一個LSprite類,step是半個小矩形的長,同樣的道理,可以也得到其他5個面。

6個面都建立了,在繪制這6個面之前,首先要根據旋轉的角度來計算各個定點的坐標,看下面的圖

根據上面的圖,用下面的公式即可得到變換后的定點坐標

Rect.prototype.getPoint = function(p){  
    var u2,v2,w2,u=p[0],v=p[1],w=p[2];  
    u2 = u * Math.cos(this.angleX) - v * Math.sin(this.angleX);  
    v2 = u * Math.sin(this.angleX) + v * Math.cos(this.angleX);  
    w2 = w;  
    u = u2; v = v2; w = w2;  
    u2 = u;  
    v2 = v * Math.cos(this.angleZ) - w * Math.sin(this.angleZ);  
    w2 = v * Math.sin(this.angleZ) + w * Math.cos(this.angleZ);  
    u = u2; v = v2; w = w2;  
    return [u2,v2,w2];  
};  

最后根據小矩形的四個定點坐標,來繪制這個矩形,

Rect.prototype.draw = function(layer){  
    this.graphics.clear();  
    this.graphics.drawVertices(1,"#000000",[this.getPoint(this.pointA),this.getPoint(this.pointB),this.getPoint(this.pointC),this.getPoint(this.pointD)],true,this.color);  
};  

其中drawVertices是lufylegend.js庫件中LGraphics類的一個方法,它可以根據傳入的定點坐標數組來繪制一個多邊形。

最后,給出完整代碼,代碼很少,JS代碼一共91行。

一,index.html

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="UTF-8">  
<title>3D魔方</title>  
</head>  
<body>  
<div id="mylegend">loading……</div>  
<script type="text/javascript" src="../lufylegend-1.4.0.min.js"></script>   
<script type="text/javascript" src="./Main.js"></script>   
<script type="text/javascript" src="./Rect.js"></script>   
</body>  
</html>  

二,Rect類

function Rect(pointA,pointB,pointC,pointD,angleX,angleZ,color){  
    base(this,LSprite,[]);  
    this.pointZ=[(pointA[0]+pointB[0]+pointC[0]+pointD[0])/4,(pointA[1]+pointB[1]+pointC[1]+pointD[1])/4,(pointA[2]+pointB[2]+pointC[2]+pointD[2])/4];  
    this.z = this.pointZ[2];  
    this.pointA=pointA,this.pointB=pointB,this.pointC=pointC,this.pointD=pointD,this.angleX=angleX,this.angleZ=angleZ,this.color=color;  
}  
Rect.prototype.draw = function(layer){  
    this.graphics.clear();  
    this.graphics.drawVertices(1,"#000000",[this.getPoint(this.pointA),this.getPoint(this.pointB),this.getPoint(this.pointC),this.getPoint(this.pointD)],true,this.color);  
};  
Rect.prototype.setAngle = function(a,b){  
    this.angleX = a;  
    this.angleZ = b;  
    this.z=this.getPoint(this.pointZ)[2];  
};  
Rect.prototype.getPoint = function(p){  
    var u2,v2,w2,u=p[0],v=p[1],w=p[2];  
    u2 = u * Math.cos(this.angleX) - v * Math.sin(this.angleX);  
    v2 = u * Math.sin(this.angleX) + v * Math.cos(this.angleX);  
    w2 = w;  
    u = u2; v = v2; w = w2;  
    u2 = u;  
    v2 = v * Math.cos(this.angleZ) - w * Math.sin(this.angleZ);  
    w2 = v * Math.sin(this.angleZ) + w * Math.cos(this.angleZ);  
    u = u2; v = v2; w = w2;  
    return [u2,v2,w2];  
};  
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了有關HTML5頁面在iPhoneX適配問題,需要的朋友可以參考下
本篇文章主要介紹了html5中canvas圖表實現柱狀圖的示例,本文使用canvas來實現一個圖表,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
Adobe公司出品的多媒體處理軟件產品線較多,涵蓋了音視頻編輯、圖像處理、平面設計、影視后期等領域。這篇文章主要介紹了Adobe Html5 Extension開發初體驗圖文教程,非常不錯,需要的朋
這篇文章主要介紹了基于HTML5的WebGL經典3D虛擬機房漫游動畫,需要的朋友可以參考下
這篇文章主要介紹了html5實現移動端適配完美寫法,需要的朋友可以參考下
本篇文章主要介紹了HTML5響應式(自適應)網頁設計的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
主站蜘蛛池模板: 国产蜜臀97一区二区三区 | 久久91精品久久久久久9鸭 | 中文字幕视频在线免费 | 亚州av| 一区二区国产在线观看 | 国产精品资源在线观看 | 极品国产视频 | 在线观看国产精品视频 | 欧美视频二区 | 欧美aaaaa| 国产精品久久久久久久久久久久久久 | 一级毛片在线播放 | 99久久国产综合精品麻豆 | 羞羞视频网站免费观看 | 亚洲免费片 | 午夜视频一区二区三区 | 中国黄色毛片视频 | 国产精品自产拍在线观看蜜 | 第四色播日韩第一页 | 色综合成人网 | 欧美性另类 | 天天干成人网 | av在线电影网 | 午夜电影一区二区 | 亚洲精品一区二区 | 欧美一级毛片在线播放 | 日韩福利一区 | 欧美一区二区在线观看 | 欧美成人一级 | 久久久久久久综合 | 精品一区二区三区在线观看国产 | 国产精品久久久久久中文字 | 免费在线观看av的网站 | 亚洲精品一区二区另类图片 | 欧美激情精品久久久久 | 亚洲网一区 | 色资源在线 | 国产欧美在线一区二区 | 亚洲国产精品久久人人爱 | 国产视频精品在线观看 | 欧美中文字幕在线 |