最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用HTML5繪制點(diǎn)線面組成的3D圖形的示例

  發(fā)布時(shí)間:2015-05-12 16:37:10   作者:佚名   我要評(píng)論
這篇文章主要介紹了利用HTML5繪制點(diǎn)線面組成的3D圖形的示例,主要還是利用了HTML5中的Canvas API,需要的朋友可以參考下

玩Canvas玩了有兩三個(gè)禮拜了,平面的東西玩來(lái)玩去也就那樣,所以就開(kāi)始折騰3D了。

  因?yàn)镃anvas畫(huà)布終究還是平面的,所以要有3D就得抽象出一個(gè)Z軸。然后再把3D坐標(biāo)轉(zhuǎn)換成2D坐標(biāo),畫(huà)到畫(huà)布上,再通過(guò)旋轉(zhuǎn)等變換效果來(lái)產(chǎn)生3D感。做3D一般就是由點(diǎn)到線,然后由線到面。

  【點(diǎn)】

  點(diǎn)的話,之前我有寫(xiě)過(guò)關(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:
2015512164236104.png (344×329)

3DBall
里面的總共有五百個(gè)點(diǎn)對(duì)象,每個(gè)點(diǎn)對(duì)象相應(yīng)的根據(jù)他們的Z軸來(lái)改變他們的大小和透明度,再平均分布在球面上,就構(gòu)成了點(diǎn)球體了。

  【線】

  如果知道怎么做點(diǎn)之后,線也就容易了,只要把點(diǎn)連起來(lái)就行了。這個(gè)沒(méi)做DEMO,不過(guò)也確實(shí)不難。就循環(huán)moveTo,然后lineTo,線就出來(lái)了。

  【面】

  這篇博文主要講面滴。
二話不說(shuō),先上個(gè)DEMO吧 :
2015512164305697.png (170×168)

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)換到二維層面來(lái)。fallLength是焦距。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  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.         }  


  然后是面對(duì)象:

  面對(duì)象的屬性頁(yè)很容易理解,一個(gè)面就是一個(gè)正方形 , v1v2v3v4是面的四個(gè)頂點(diǎn),zIndex這個(gè)屬性很重要,是代表這個(gè)面的層級(jí),是在最外面還是在里面,這個(gè)必須要有,這樣當(dāng)用canvas畫(huà)的時(shí)候才能讓這個(gè)面畫(huà)在最前面,才不會(huì)被其他的面遮蓋。zIndex的值也很容易理解,就是頂點(diǎn)z軸坐標(biāo)的平均值,其實(shí)也就是中心點(diǎn)的z軸坐標(biāo)。顏色就是這個(gè)面的顏色啦。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  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.         }  


  最后是立方體本身對(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方法。立方體就出來(lái)了。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  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.         }  


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

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  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.         }   


全部代碼我就不貼了,DEMO里通過(guò)控制臺(tái)都可以看到。我也沒(méi)引用其他什么框架之類的,直接copy下來(lái)就能用了。

  能寫(xiě)好轉(zhuǎn)動(dòng)的一個(gè)立方體后,多個(gè)立方體轉(zhuǎn)動(dòng)也可以做出來(lái)了。
2015512164340019.png (484×463)

戳DEMO:面:3D立方體2    3D立方體線(這個(gè)純碎覺(jué)得沒(méi)有面更酷而已)

相關(guān)文章

  • Html5+JS實(shí)現(xiàn)手機(jī)搖一搖功能

    這篇文章主要介紹了Html5+JS實(shí)現(xiàn)手機(jī)搖一搖功能,本文使用HTML5的DeviceOrientation實(shí)現(xiàn)監(jiān)聽(tīng)手機(jī)方向傳感器數(shù)據(jù),實(shí)現(xiàn)搖一搖功能雛形,需要的朋友可以參考下
    2015-04-24
  • HTML5 Canvas的事件處理介紹

    這篇文章主要介紹了HTML5 Canvas的事件處理介紹,本文講解了Canvas的限制、給Canvas元素綁定事件、isPointInPath方法、循環(huán)重繪和事件冒泡等內(nèi)容,需要的朋友可以參考下
    2015-04-24
  • HTML5 audio標(biāo)簽使用js進(jìn)行播放控制實(shí)例

    這篇文章主要介紹了HTML5 audio標(biāo)簽使用js進(jìn)行播放控制實(shí)例,本文直接給出代碼實(shí)例,演示了獲取播放時(shí)間、播放、暫停、靜音等控制方法,需要的朋友可以參考下
    2015-04-24

最新評(píng)論

通道| 浦江县| 穆棱市| 广元市| 寿光市| 沅江市| 富宁县| 抚顺市| 新营市| 万州区| 五常市| 隆化县| 南溪县| 巴塘县| 双桥区| 新泰市| 怀宁县| 绥化市| 宁武县| 长海县| 渝中区| 乌鲁木齐市| 武山县| 弋阳县| 沁源县| 湖口县| 吐鲁番市| 勐海县| 诏安县| 安新县| 潮安县| 台东市| 获嘉县| 鲁甸县| 寿阳县| 开封县| 荔浦县| 略阳县| 绥棱县| 日土县| 海原县|