溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HTML5中怎么通過繪制點線面組成一個3D圖形

發(fā)布時間:2021-06-29 16:33:46 來源:億速云 閱讀:134 作者:Leah 欄目:web開發(fā)

這篇文章將為大家詳細講解有關HTML5中怎么通過繪制點線面組成一個3D圖形,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

3D立方體

做一個立方體,我用了三個對象:點對象,面對象,以及立方體本身一個對象:

  下面這個是點對象,x,y,z是點的三維坐標,_get2d方法是把三維坐標轉換到二維層面來。fallLength是焦距。

XML/HTML Code復制內容到剪貼板

  1. var Vector = function(x,y,z){   

  2.             this.x = x;   

  3.             this.y = y;   

  4.             this.z = z;   

  5.             this._get2d = function(){   

  6.                 var scale = fallLength/(fallLength+this.z);   

  7.                 var x = centerX + this.x*scale;   

  8.                 var y = centerY + this.y*scale;   

  9.                 return {x:x , y:y};   

  10.             }   

  11.         }  


  然后是面對象:

  面對象的屬性頁很容易理解,一個面就是一個正方形 , v1v2v3v4是面的四個頂點,zIndex這個屬性很重要,是代表這個面的層級,是在最外面還是在里面,這個必須要有,這樣當用canvas畫的時候才能讓這個面畫在最前面,才不會被其他的面遮蓋。zIndex的值也很容易理解,就是頂點z軸坐標的平均值,其實也就是中心點的z軸坐標。顏色就是這個面的顏色啦。

XML/HTML Code復制內容到剪貼板

  1. var Face = function(vector1,vector2,vector3,vector4,color){   

  2.             this.v1 = vector1;   

  3.             this.v2 = vector2;   

  4.             this.v3 = vector3;   

  5.             this.v4 = vector4;   

  6.             this.color = color;   

  7.             this.zIndex = (this.v1.z + this.v2.z + this.v3.z + this.v4.z)/4;   

  8.             this.draw = function(){   

  9.                 ctx.save();   

  10.                 ctx.beginPath();   

  11.                 ctx.moveTo(this.v1._get2d().x , this.v1._get2d().y);   

  12.                 ctx.lineTo(this.v2._get2d().x , this.v2._get2d().y);   

  13.                 ctx.lineTo(this.v3._get2d().x , this.v3._get2d().y);   

  14.                 ctx.lineTo(this.v4._get2d().x , this.v4._get2d().y);   

  15.                 ctx.closePath();   

  16.                 // ctx.fillStyle = "rgba("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+",0.2)";   

  17.                 ctx.fillStyle = this.color;   

  18.                 ctx.fill();   

  19.             }   

  20.         }  


  最后是立方體本身對象:

  因為立方體最后要旋轉,所以,立方體對象里面不僅有面對象,還要有點對象,點旋轉后才會引起面的旋轉。length是立方體的邊長,_initVector是初始化立方體的各個頂點,_draw方法就是把所有點形成面,將面放入數組,然后對面進行排序(就是根據面里的zIndex排序),排序好后,調用每個面里的draw方法。立方體就出來了。

XML/HTML Code復制內容到剪貼板

  1. var Cube = function(length){   

  2.             this.length = length;   

  3.             this.faces = [];   

  4.             this.vectors = [];   

  5.         }   

  6.         Cube.prototype = {   

  7.             _initVector:function(){   

  8.                 this.vectors[0] = new Vector(-this.length/2 , -this.length/2 , this.length/2);   

  9.                 this.vectors[1] = new Vector(-this.length/2 , this.length/2 , this.length/2);    

  10.                 this.vectors[2] = new Vector(this.length/2 , -this.length/2 , this.length/2);    

  11.                 this.vectors[3] = new Vector(this.length/2 , this.length/2 , this.length/2);    

  12.                 this.vectors[4] = new Vector(this.length/2 , -this.length/2 , -this.length/2);   

  13.                 this.vectors[5] = new Vector(this.length/2 , this.length/2 , -this.length/2);   

  14.                 this.vectors[6] = new Vector(-this.length/2 , -this.length/2 , -this.length/2);   

  15.                 this.vectors[7] = new Vector(-this.length/2 , this.length/2 , -this.length/2);   

  16.             },   

  17.             _draw:function(){   

  18.                 this.faces[0] = new Face(this.vectors[0] , this.vectors[1] , this.vectors[3] , this.vectors[2] , "#6c6");   

  19.                 this.faces[1] = new Face(this.vectors[2] , this.vectors[3] , this.vectors[5] , this.vectors[4] , "#6cc");   

  20.                 this.faces[2] = new Face(this.vectors[4] , this.vectors[5] , this.vectors[7] , this.vectors[6] , "#cc6");   

  21.                 this.faces[3] = new Face(this.vectors[6] , this.vectors[7] , this.vectors[1] , this.vectors[0] , "#c6c");   

  22.                 this.faces[4] = new Face(this.vectors[1] , this.vectors[3] , this.vectors[5] , this.vectors[7] , "#666");   

  23.                 this.faces[5] = new Face(this.vectors[0] , this.vectors[2] , this.vectors[4] , this.vectors[6] , "#ccc");   

  24.   

  25.                 this.faces.sort(function(a , b){   

  26.                     return b.zIndex - a.zIndex;   

  27.                 });   

  28.                 this.faces.foreach(function(){   

  29.                     this.draw();   

  30.                 })   

  31.             }   

  32.         }  


  立方體做好了,接下來就可以讓它動起來了。根據鼠標位置改變立方體轉動的角度。rotateX和rotateY方法就是讓所有點繞X軸旋轉以及繞Y軸旋轉。這個的原理我在之前那個博文上好像有說過。。。。如果想了解更多,可以自己去百度一下計算機圖形學3D變換。繞X軸和繞Y軸是最簡單的旋轉矩陣了。當然,如果有興趣的還可以去搜一下繞任意軸旋轉矩陣。。。這個有點復雜,我本來想用它來做個魔方,不過遇到一些問題,暫時還沒解決。好吧,扯遠了。通過rotateX和rotateY兩個方法可以讓每個點獲得下一幀的位置,在動畫循環(huán)中重繪。這樣,轉動的立方體就做出來了。

XML/HTML Code復制內容到剪貼板

  1. if("addEventListener" in window){   

  2.             window.addEventListener("mousemove" , function(event){   

  3.                 var x = event.clientX - canvas.offsetLeft - centerX;   

  4.                 var y = event.clientY - canvas.offsetTop - centerY;   

  5.                 angleY = x*0.0001;   

  6.                 angleX = y*0.0001;   

  7.             });   

  8.         }   

  9.         else {   

  10.             window.attachEvent("onmousemove" , function(event){   

  11.                 var x = event.clientX - canvas.offsetLeft - centerX;   

  12.                 var y = event.clientY - canvas.offsetTop - centerY;   

  13.                 angleY = x*0.0001;   

  14.                 angleX = y*0.0001;   

  15.             });   

  16.         }   

  17.            

  18.   

  19.         function rotateX(vectors){   

  20.             var cos = Math.cos(angleX);   

  21.             var sin = Math.sin(angleX);   

  22.             vectors.foreach(function(){   

  23.                 var y1 = this.y * cos - this.z * sin;   

  24.                 var z1 = this.z * cos + this.y * sin;   

  25.                 this.y = y1;   

  26.                 this.z = z1;   

  27.             });   

  28.         }   

  29.   

  30.         function rotateY(vectors){   

  31.             var cos = Math.cos(angleY);   

  32.             var sin = Math.sin(angleY);   

  33.             vectors.foreach(function(){   

  34.                 var x1 = this.x * cos - this.z * sin;   

  35.                 var z1 = this.z * cos + this.x * sin;   

  36.                 this.x = x1;   

  37.                 this.z = z1;   

  38.             })   

  39.         }   

  40.   

  41.            

  42.   

  43.         cube = new Cube(80);   

  44.         cube._initVector();   

  45.         function initAnimate(){   

  46.             cube._draw();   

  47.   

  48.             animate();   

  49.         }   

  50.   

  51.         function animate(){   

  52.             ctx.clearRect(0,0,canvas.width,canvas.height)   

  53.                

  54.             rotateY(cube.vectors);   

  55.             rotateX(cube.vectors);   

  56.             cube._draw();   

  57.             if("requestAnimationFrame" in window){   

  58.                 requestAnimationFrame(animate);   

  59.             }   

  60.             else if("webkitRequestAnimationFrame" in window){   

  61.                 webkitRequestAnimationFrame(animate);   

  62.             }   

  63.             else if("msRequestAnimationFrame" in window){   

  64.                 msRequestAnimationFrame(animate);   

  65.             }   

  66.             else if("mozRequestAnimationFrame" in window){   

  67.                 mozRequestAnimationFrame(animate);   

  68.             }   

  69.             else {   

  70.                 setTimeout(animate , 16);   

  71.             }   

  72.         }   

關于HTML5中怎么通過繪制點線面組成一個3D圖形就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI