DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
利用 HTML5 繪製由點、線、面組成的 3D 圖形示例
編輯:HTML5教程     

玩 Canvas 玩了兩三個禮拜,平面的東西玩來玩去也就那樣,所以就開始折騰 3D 了。

因為 Canvas 畫布終究還是平面的,所以要做 3D,就得先抽象出一個 Z 軸,再把 3D 坐標轉換成 2D 坐標畫到畫布上,然後透過旋轉等變換效果來產生立體感。做 3D 一般都是「由點到線,再由線到面」。

【點】

關於點,我之前寫過一篇關於 3D 的博文《解析 3D 標籤雲,其實很簡單》。那篇博文雖然講的是用 div 實現的 3D 標籤雲,但追根究底,其 3D 原理是一樣的——都是最簡單的、由點構成的 3D,每個標籤就是一個點。也可以直接看這個 DEMO:

3D Ball

裡面總共有五百個點物件,每個點根據各自的 Z 軸來改變大小與透明度,再平均分布在球面上,就構成了點球體。

【線】

如果知道怎麼做點,那麼線就容易了,只要把點連起來即可。這個沒做 DEMO,不過確實不難——循環 moveTo,然後 lineTo,線就出來了。

【面】

這篇博文主要講「面」。二話不說,先上個 DEMO:

3D 立方體

做一個立方體,我用了三個物件:點物件、面物件,以及立方體本身的物件。

下面是點物件,x、y、z 是點的三維坐標,_get2d 方法把三維坐標轉換到二維層面,fallLength 是焦距。

var Vector = function(x,y,z){
this.x = x;
this.y = y;
this.z = z;
this._get2d = function(){
var scale = fallLength/(fallLength+this.z);
var x = centerX + this.x*scale;
var y = centerY + this.y*scale;
return {x:x , y:y};
}
}

然後是面物件。面物件的屬性也很容易理解:一個面就是一個正方形,v1、v2、v3、v4 是面的四個頂點;zIndex 這個屬性很重要,它代表這個面的層級——是在最外面還是在裡面,必須有它,這樣用 canvas 畫的時候才能讓這個面畫在最前面、不被其他面遮蓋。zIndex 的值就是四個頂點 z 軸坐標的平均值,也就是中心點的 z 軸坐標。顏色則是這個面的顏色。

var Face = function(vector1,vector2,vector3,vector4,color){
this.v1 = vector1;
this.v2 = vector2;
this.v3 = vector3;
this.v4 = vector4;
this.color = color;
this.zIndex = (this.v1.z + this.v2.z + this.v3.z + this.v4.z)/4;
this.draw = function(){
ctx.save();
ctx.beginPath();
ctx.moveTo(this.v1._get2d().x , this.v1._get2d().y);
ctx.lineTo(this.v2._get2d().x , this.v2._get2d().y);
ctx.lineTo(this.v3._get2d().x , this.v3._get2d().y);
ctx.lineTo(this.v4._get2d().x , this.v4._get2d().y);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
}

最後是立方體本身物件。因為立方體最後要旋轉,所以立方體物件裡不僅有面物件,還要有點物件——點旋轉後才會引起面的旋轉。length 是立方體的邊長,_initVector 初始化各頂點,_draw 方法把所有點形成面、放入陣列,然後對面進行排序(根據 zIndex),排序好後呼叫每個面的 draw 方法,立方體就出來了。

var Cube = function(length){
this.length = length;
this.faces = [];
this.vectors = [];
}
Cube.prototype = {
_initVector:function(){
this.vectors[0] = new Vector(-this.length/2 , -this.length/2 , this.length/2);
this.vectors[1] = new Vector(-this.length/2 , this.length/2 , this.length/2);
this.vectors[2] = new Vector(this.length/2 , -this.length/2 , this.length/2);
this.vectors[3] = new Vector(this.length/2 , this.length/2 , this.length/2);
this.vectors[4] = new Vector(this.length/2 , -this.length/2 , -this.length/2);
this.vectors[5] = new Vector(this.length/2 , this.length/2 , -this.length/2);
this.vectors[6] = new Vector(-this.length/2 , -this.length/2 , -this.length/2);
this.vectors[7] = new Vector(-this.length/2 , this.length/2 , -this.length/2);
},
_draw:function(){
this.faces[0] = new Face(this.vectors[0] , this.vectors[1] , this.vectors[3] , this.vectors[2] , "#6c6");
this.faces[1] = new Face(this.vectors[2] , this.vectors[3] , this.vectors[5] , this.vectors[4] , "#6cc");
this.faces[2] = new Face(this.vectors[4] , this.vectors[5] , this.vectors[7] , this.vectors[6] , "#cc6");
this.faces[3] = new Face(this.vectors[6] , this.vectors[7] , this.vectors[1] , this.vectors[0] , "#c6c");
this.faces[4] = new Face(this.vectors[1] , this.vectors[3] , this.vectors[5] , this.vectors[7] , "#666");
this.faces[5] = new Face(this.vectors[0] , this.vectors[2] , this.vectors[4] , this.vectors[6] , "#ccc");
this.faces.sort(function(a , b){
return b.zIndex - a.zIndex;
});
this.faces.forEach(function(){
this.draw();
})
}
}

立方體做好後,接下來就可以讓它動起來。根據滑鼠位置改變立方體轉動的角度,rotateX 和 rotateY 方法就是讓所有點繞 X 軸旋轉、繞 Y 軸旋轉。繞 X 軸和繞 Y 軸是最簡單的旋轉矩陣,想深入了解可以搜一下「計算機圖形學 3D 變換」。透過 rotateX 和 rotateY 兩個方法,可以讓每個點獲得下一幀的位置,在動畫循環中重繪,轉動的立方體就做出來了。

if("addEventListener" in window){
window.addEventListener("mousemove" , function(event){
var x = event.clientX - canvas.offsetLeft - centerX;
var y = event.clientY - canvas.offsetTop - centerY;
angleY = x*0.0001;
angleX = y*0.0001;
});
}
else {
window.attachEvent("onmousemove" , function(event){
var x = event.clientX - canvas.offsetLeft - centerX;
var y = event.clientY - canvas.offsetTop - centerY;
angleY = x*0.0001;
angleX = y*0.0001;
});
}
function rotateX(vectors){
var cos = Math.cos(angleX);
var sin = Math.sin(angleX);
vectors.forEach(function(){
var y1 = this.y * cos - this.z * sin;
var z1 = this.z * cos + this.y * sin;
this.y = y1;
this.z = z1;
});
}
function rotateY(vectors){
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
vectors.forEach(function(){
var x1 = this.x * cos - this.z * sin;
var z1 = this.z * cos + this.x * sin;
this.x = x1;
this.z = z1;
})
}
cube = new Cube(80);
cube._initVector();
function initAnimate(){
cube._draw();
animate();
}
function animate(){
ctx.clearRect(0,0,canvas.width,canvas.height)
rotateY(cube.vectors);
rotateX(cube.vectors);
cube._draw();
if("requestAnimationFrame" in window){
requestAnimationFrame(animate);
}
else if("webkitRequestAnimationFrame" in window){
webkitRequestAnimationFrame(animate);
}
else if("msRequestAnimationFrame" in window){
msRequestAnimationFrame(animate);
}
else if("mozRequestAnimationFrame" in window){
mozRequestAnimationFrame(animate);
}
else {
setTimeout(animate , 16);
}
}

完整程式碼我就不貼了,DEMO 裡透過控制台都能看到。我也沒引用其他框架,直接 copy 下來就能用。寫好轉動的立方體後,多個立方體一起轉動也能做出來。

DEMO:面——3D 立方體2、3D 立方體線(這個純粹是覺得沒有面更酷而已)。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved