当前位置: 移动技术网 > IT编程>网页制作>Html5 > 利用HTML5绘制点线面组成的3D图形的示例

利用HTML5绘制点线面组成的3D图形的示例

2019年07月25日  | 移动技术网IT编程  | 我要评论

mird 124,滨州汽车站,4999小游戏大全

玩canvas玩了有两三个礼拜了,平面的东西玩来玩去也就那样,所以就开始折腾3d了。

  因为canvas画布终究还是平面的,所以要有3d就得抽象出一个z轴。然后再把3d坐标转换成2d坐标,画到画布上,再通过旋转等变换效果来产生3d感。做3d一般就是由点到线,然后由线到面。

  【点】

  点的话,之前我有写过关于3d的博文 ,这篇博文虽然讲的是用div实现的3d标签云,但是追根到底产生的3d原理是一样的,就是最简单的由点构成的3d了。每一个标签就是一个点。也可以直接看这个demo:
2015512164236104.png (344×329)

里面的总共有五百个点对象,每个点对象相应的根据他们的z轴来改变他们的大小和透明度,再平均分布在球面上,就构成了点球体了。

  【线】

  如果知道怎么做点之后,线也就容易了,只要把点连起来就行了。这个没做demo,不过也确实不难。就循环moveto,然后lineto,线就出来了。

  【面】

  这篇博文主要讲面滴。
二话不说,先上个demo吧 :
2015512164305697.png (170×168)

做一个立方体,我用了三个对象:点对象,面对象,以及立方体本身一个对象:

  下面这个是点对象,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两个方法可以让每个点获得下一帧的位置,在动画循环中重绘。这样,转动的立方体就做出来了。

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.         }   


全部代码我就不贴了,demo里通过控制台都可以看到。我也没引用其他什么框架之类的,直接copy下来就能用了。

  能写好转动的一个立方体后,多个立方体转动也可以做出来了。
2015512164340019.png (484×463)

戳demo:面:    (这个纯碎觉得没有面更酷而已)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网