玩Canvas玩了有兩三個(gè)禮拜了,平面的東西玩來玩去也就那樣,所以就開始折騰3D了。
因?yàn)镃anvas畫布終究還是平面的,所以要有3D就得抽象出一個(gè)Z軸。然后再把3D坐標(biāo)轉(zhuǎn)換成2D坐標(biāo),畫到畫布上,再通過旋轉(zhuǎn)等變換效果來產(chǎn)生3D感。做3D一般就是由點(diǎn)到線,然后由線到面。
【點(diǎn)】
點(diǎn)的話,之前我有寫過關(guān)于3D的博文 解析3D標(biāo)簽云,其實(shí)很簡(jiǎn)單 ,這篇博文雖然講的是用div實(shí)現(xiàn)的3D標(biāo)簽云,但是追根到底產(chǎn)生的3D原理是一樣的,就是最簡(jiǎn)單的由點(diǎn)構(gòu)成的3D了。每一個(gè)標(biāo)簽就是一個(gè)點(diǎn)。也可以直接看這個(gè)DEMO:
![](/d/20211016/0de683ed7b6f2928c755b8a8fa41312c.gif)
3DBall
里面的總共有五百個(gè)點(diǎn)對(duì)象,每個(gè)點(diǎn)對(duì)象相應(yīng)的根據(jù)他們的Z軸來改變他們的大小和透明度,再平均分布在球面上,就構(gòu)成了點(diǎn)球體了。
【線】
如果知道怎么做點(diǎn)之后,線也就容易了,只要把點(diǎn)連起來就行了。這個(gè)沒做DEMO,不過也確實(shí)不難。就循環(huán)moveTo,然后lineTo,線就出來了。
【面】
這篇博文主要講面滴。
二話不說,先上個(gè)DEMO吧 :
![](/d/20211016/d9d8ff3a49b108fae87fca75f9106580.gif)
3D立方體
做一個(gè)立方體,我用了三個(gè)對(duì)象:點(diǎn)對(duì)象,面對(duì)象,以及立方體本身一個(gè)對(duì)象:
下面這個(gè)是點(diǎn)對(duì)象,x,y,z是點(diǎn)的三維坐標(biāo),_get2d方法是把三維坐標(biāo)轉(zhuǎn)換到二維層面來。fallLength是焦距。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- 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};
- }
- }
然后是面對(duì)象:
面對(duì)象的屬性頁很容易理解,一個(gè)面就是一個(gè)正方形 , v1v2v3v4是面的四個(gè)頂點(diǎn),zIndex這個(gè)屬性很重要,是代表這個(gè)面的層級(jí),是在最外面還是在里面,這個(gè)必須要有,這樣當(dāng)用canvas畫的時(shí)候才能讓這個(gè)面畫在最前面,才不會(huì)被其他的面遮蓋。zIndex的值也很容易理解,就是頂點(diǎn)z軸坐標(biāo)的平均值,其實(shí)也就是中心點(diǎn)的z軸坐標(biāo)。顏色就是這個(gè)面的顏色啦。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- 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();
- }
- }
最后是立方體本身對(duì)象:
因?yàn)榱⒎襟w最后要旋轉(zhuǎn),所以,立方體對(duì)象里面不僅有面對(duì)象,還要有點(diǎn)對(duì)象,點(diǎn)旋轉(zhuǎn)后才會(huì)引起面的旋轉(zhuǎn)。length是立方體的邊長(zhǎng),_initVector是初始化立方體的各個(gè)頂點(diǎn),_draw方法就是把所有點(diǎn)形成面,將面放入數(shù)組,然后對(duì)面進(jìn)行排序(就是根據(jù)面里的zIndex排序),排序好后,調(diào)用每個(gè)面里的draw方法。立方體就出來了。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- 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();
- })
- }
- }
立方體做好了,接下來就可以讓它動(dòng)起來了。根據(jù)鼠標(biāo)位置改變立方體轉(zhuǎn)動(dòng)的角度。rotateX和rotateY方法就是讓所有點(diǎn)繞X軸旋轉(zhuǎn)以及繞Y軸旋轉(zhuǎn)。這個(gè)的原理我在之前那個(gè)博文上好像有說過。。。。如果想了解更多,可以自己去百度一下計(jì)算機(jī)圖形學(xué)3D變換。繞X軸和繞Y軸是最簡(jiǎn)單的旋轉(zhuǎn)矩陣了。當(dāng)然,如果有興趣的還可以去搜一下繞任意軸旋轉(zhuǎn)矩陣。。。這個(gè)有點(diǎn)復(fù)雜,我本來想用它來做個(gè)魔方,不過遇到一些問題,暫時(shí)還沒解決。好吧,扯遠(yuǎn)了。通過rotateX和rotateY兩個(gè)方法可以讓每個(gè)點(diǎn)獲得下一幀的位置,在動(dòng)畫循環(huán)中重繪。這樣,轉(zhuǎn)動(dòng)的立方體就做出來了。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- 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里通過控制臺(tái)都可以看到。我也沒引用其他什么框架之類的,直接copy下來就能用了。
能寫好轉(zhuǎn)動(dòng)的一個(gè)立方體后,多個(gè)立方體轉(zhuǎn)動(dòng)也可以做出來了。
![](/d/20211016/ec951802fe1dbfd8fe157d69cf34aca6.gif)
戳DEMO:面:3D立方體2 3D立方體線(這個(gè)純碎覺得沒有面更酷而已)