聲明:本文爲原創文章,如需轉載,請註明來源WAxes,謝謝!html
玩Canvas玩了有兩三個禮拜了,平面的東西玩來玩去也就那樣,因此就開始折騰3D了。git
由於Canvas畫布終究仍是平面的,因此要有3D就得抽象出一個Z軸。而後再把3D座標轉換成2D座標,畫到畫布上,再經過旋轉等變換效果來產生3D感。作3D通常就是由點到線,而後由線到面。github
【點】web
點的話,以前我有寫過關於3D的博文 解析3D標籤雲,其實很簡單 ,這篇博文雖然講的是用div實現的3D標籤雲,可是追根到底產生的3D原理是同樣的,就是最簡單的由點構成的3D了。每個標籤就是一個點。也能夠直接看這個DEMO:canvas
3DBall 。數組
裏面的總共有五百個點對象,每一個點對象相應的根據他們的Z軸來改變他們的大小和透明度,再平均分佈在球面上,就構成了點球體了。框架
【線】dom
若是知道怎麼作點以後,線也就容易了,只要把點連起來就好了。這個沒作DEMO,不過也確實不難。就循環moveTo,而後lineTo,線就出來了。post
【面】動畫
這篇博文主要講面滴。
二話不說,先上個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}; } }
而後是面對象:
面對象的屬性頁很容易理解,一個面就是一個正方形 , v1v2v3v4是面的四個頂點,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 = "rgba("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+",0.2)"; 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軸旋轉。這個的原理我在以前那個博文上好像有說過。。。。若是想了解更多,能夠本身去百度一下計算機圖形學3D變換。繞X軸和繞Y軸是最簡單的旋轉矩陣了。固然,若是有興趣的還能夠去搜一下繞任意軸旋轉矩陣。。。這個有點複雜,我原本想用它來作個魔方,不過遇到一些問題,暫時還沒解決。好吧,扯遠了。經過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多立方體
源碼地址:https://github.com/whxaxes/canvas-test/blob/gh-pages/src/3D-demo/3Dcubes.html
就是繁瑣了一點,具體方法這裏就不說了,原本想作成魔方的,就是裏面的rotateP方法就是繞任意軸旋轉(矩陣請自行搜「繞任意軸旋轉矩陣」),不過仍是出了點bug,轉是能轉了,可是轉動後的座標有誤,因此沒作成,如今還在研究着。。。。