[HTML5] 3D model-hundreds of lines of code for rotating three-dimensional cube instances and hundreds of lines of html5

Source: Internet
Author: User
Tags addchild

[HTML5] 3D model-hundreds of lines of code for rotating three-dimensional cube instances and hundreds of lines of html5

Recently, after studying how to create a cube, I suddenly wanted to use HMTL5 to write a cube model. Because the cube is a 3D cube, I tried to write a simple 3D model with HTML5.

The following is a preview.

Production Process

First you need to download Html5 open source library lufylegend-1.4.0

Cube is divided into six sides, each of which consists of nine small rectangles. Now I encapsulate each small rectangle as a class,

Because we are building a 3D cube, to draw each small rectangle, we need to know the four fixed points of the small rectangle, and these four fixed points will change according to the Rotation Angle of the space, therefore, to calculate the four fixed-point coordinates, you need to know the angle from which the cube rotates around the X axis and the Z axis.

Therefore, create a rectangle class as follows:

function Rect(pointA,pointB,pointC,pointD,angleX,angleZ,color){      base(this,LSprite,[]);      this.pointZ=[(pointA[0]+pointB[0]+pointC[0]+pointD[0])/4,(pointA[1]+pointB[1]+pointC[1]+pointD[1])/4,(pointA[2]+pointB[2]+pointC[2]+pointD[2])/4];      this.z = this.pointZ[2];      this.pointA=pointA,this.pointB=pointB,this.pointC=pointC,this.pointD=pointD,this.angleX=angleX,this.angleZ=angleZ,this.color=color;  }    Rect.prototype.setAngle = function(a,b){      this.angleX = a;      this.angleZ = b;      this.z=this.getPoint(this.pointZ)[2];  };  

PointA, pointB, pointC, and pointD are the four vertices of the small rectangle. angleX and angleZ are the rotation angles of the X axis and the Z axis respectively, and color is the color of the small rectangle.

The Cube is divided into six sides. Let's first look at the front side. If the center of the cube is the center of the 3D coordinate system, the coordinates corresponding to each point of the nine small rectangles are shown in.

Therefore, the nine small rectangles above can be created by the following code.

for(var x=0;x<3;x++){      for(var y=0;y<3;y++){          z = 3;          var rect = new Rect([-3*step + x*2*step,-3*step + y*2*step,-3*step + z*2*step],[-step + x*2*step,-3*step + y*2*step,-3*step + z*2*step],[-step + x*2*step,-step + y*2*step,-3*step + z*2*step],[-3*step + x*2*step,-step + y*2*step,-3*step + z*2*step],0,0,"#FF0000");          backLayer.addChild(rect);      }  }  

The backLayer is an LSprite class, and the step is the length of half a small rectangle. In the same way, the other five faces can be obtained.

The six faces are created. Before drawing the six faces, you must first calculate the coordinates of each point based on the rotation angle. See the following figure.

Based on the preceding figure, use the following formula to obtain the converted fixed point coordinates.

Rect.prototype.getPoint = function(p){      var u2,v2,w2,u=p[0],v=p[1],w=p[2];      u2 = u * Math.cos(this.angleX) - v * Math.sin(this.angleX);      v2 = u * Math.sin(this.angleX) + v * Math.cos(this.angleX);      w2 = w;      u = u2; v = v2; w = w2;      u2 = u;      v2 = v * Math.cos(this.angleZ) - w * Math.sin(this.angleZ);      w2 = v * Math.sin(this.angleZ) + w * Math.cos(this.angleZ);      u = u2; v = v2; w = w2;      return [u2,v2,w2];  };  

Finally, draw the rectangle based on the four fixed-point coordinates of the small rectangle,

Rect.prototype.draw = function(layer){      this.graphics.clear();      this.graphics.drawVertices(1,"#000000",[this.getPoint(this.pointA),this.getPoint(this.pointB),this.getPoint(this.pointC),this.getPoint(this.pointD)],true,this.color);  };  

DrawVertices is a method of the LGraphics class in the lufylegend. js library. It can draw a polygon based on the input fixed-point coordinate array.

Finally, the complete code is provided, with few lines of JavaScript code.

1. index.html

<! DOCTYPE html> 

Ii. Rect class

function Rect(pointA,pointB,pointC,pointD,angleX,angleZ,color){      base(this,LSprite,[]);      this.pointZ=[(pointA[0]+pointB[0]+pointC[0]+pointD[0])/4,(pointA[1]+pointB[1]+pointC[1]+pointD[1])/4,(pointA[2]+pointB[2]+pointC[2]+pointD[2])/4];      this.z = this.pointZ[2];      this.pointA=pointA,this.pointB=pointB,this.pointC=pointC,this.pointD=pointD,this.angleX=angleX,this.angleZ=angleZ,this.color=color;  }  Rect.prototype.draw = function(layer){      this.graphics.clear();      this.graphics.drawVertices(1,"#000000",[this.getPoint(this.pointA),this.getPoint(this.pointB),this.getPoint(this.pointC),this.getPoint(this.pointD)],true,this.color);  };  Rect.prototype.setAngle = function(a,b){      this.angleX = a;      this.angleZ = b;      this.z=this.getPoint(this.pointZ)[2];  };  Rect.prototype.getPoint = function(p){      var u2,v2,w2,u=p[0],v=p[1],w=p[2];      u2 = u * Math.cos(this.angleX) - v * Math.sin(this.angleX);      v2 = u * Math.sin(this.angleX) + v * Math.cos(this.angleX);      w2 = w;      u = u2; v = v2; w = w2;      u2 = u;      v2 = v * Math.cos(this.angleZ) - w * Math.sin(this.angleZ);      w2 = v * Math.sin(this.angleZ) + w * Math.cos(this.angleZ);      u = u2; v = v2; w = w2;      return [u2,v2,w2];  };  

3. Main. js

Init (50, "mylegend", 400,400, main); var a = 0, B = 0, backLayer, step = 20, key = null; function main () {backLayer = new LSprite (); addChild (backLayer); backLayer. x = 120, backLayer. y = 120; // After for (var x = 0; x <3; x ++) {for (var y = 0; y <3; y ++) {z = 0; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], 0, 0, "# FF4500"); backLayer. addChild (rect) ;}}// front for (var x = 0; x <3; x ++) {for (var y = 0; y <3; y ++) {z = 3; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], 0, 0, "# FF0000"); backLayer. addChild (rect) ;}}// for (var x = 0; x <3; x ++) {for (var z = 0; z <3; z ++) {y = 0; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], 0, "# FFFFFF"); backLayer. addChild (rect) ;}}// for (var x = 0; x <3; x ++) {for (var z = 0; z <3; z ++) {y = 3; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], 0, "# FFFF00"); backLayer. addChild (rect) ;}}// left for (var y = 0; y <3; y ++) {for (var z = 0; z <3; z ++) {x = 0; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-3 * step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], 008000, "#"); backLayer. addChild (rect) ;}}// right for (var y = 0; y <3; y ++) {for (var z = 0; z <3; z ++) {x = 3; var rect = new Rect ([-3 * step + x * 2 * step,-3 * step + y * 2 * step, -3 * step + z * 2 * step], [-3 * step + x * 2 * step,-3 * step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -step + z * 2 * step], [-3 * step + x * 2 * step,-step + y * 2 * step, -3 * step + z * 2 * step], 0, 0, "# 0000FF"); backLayer. addChild (rect) ;}} backLayer. addEventListener (LEvent. ENTER_FRAME, onframe);} function onframe () {a + = 0.1, B + = 0.1; backLayer. childList = backLayer. childList. sort (function (a, B) {return. z-B. z;}); for (key in backLayer. childList) {backLayer. childList [key]. setAngle (a, B); backLayer. childList [key]. draw (backLayer );}}

This is just a very simple 3D model. I hope it will be helpful for everyone's learning, and I hope everyone can support the house of helping customers a lot.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.