2013-02-22 117 views
1

我使用畫布3D繪製一個三維圖形,其中我可以繪製諸如(1,5,4),(-8,6,-2)等點。所以我能夠繪製所有正,負x,y和z軸。使用箭頭鍵還可以獲得旋轉效果。 旋轉指令: z軸從屏幕中心向外延伸。關於軸問題的旋轉畫布

要繞X軸旋轉,請按向上/向下箭頭鍵。 要圍繞y軸旋轉,請按左/右箭頭鍵。 要圍繞z軸旋轉,請按Ctrl +左/ Ctrl +向下箭頭鍵。

我可以通過指定我提供的文本字段中的點來繪製點。 現在的問題是,例如,如果我繪製(5,5,2)它會正確繪製。但如果我先旋轉X軸然後Y軸然後點將被正確繪製。如果我先旋轉Y軸然後再旋轉X軸,問題就出現了。這個點會被錯誤地繪製出來。 簡單的方法來找到我遇到的問題: 這可以很容易地找出,如果你反覆繪製相同的點。點應繪製在同一點上方,以便只有單一點是可見的。但在我的情況下點(對於ex(5,5,2)在畫布中的不同位置繪製,而旋轉。此問題僅適用於如果我先旋轉y軸然後再旋轉x軸或如果我先旋轉z軸然後再旋轉y軸。那麼,什麼是我在做coding.I錯誤是新來這個帆布3D和java script.So請幫助。

<html> 

<head> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<title>Canvas Surface Rotation</title> 

<style> 

    body { 

    text-align: center; 

    } 



    canvas { 

    border: 1px solid black; 

    } 

</style> 

<script> 

var p1; 
var p2; 
var p3; 
var p4; 
var p5; 
var p6; 
var xangle=0; 
var yangle=0; 
var zangle=0; 
    var constants = { 

    canvasWidth: 600, // In pixels. 

    canvasHeight: 600, // In pixels. 

    leftArrow: 37, 

    upArrow: 38, 

    rightArrow: 39, 

    downArrow: 40, 

    xMin: -10, // These four max/min values define a square on the xy-plane that the surface will be plotted over. 

    xMax: 10, 

    yMin: -10, 

    yMax: 10, 

    xDelta: 0.06, // Make smaller for more surface points. 

    yDelta: 0.06, // Make smaller for more surface points. 

    colorMap: ["#000080"], // There are eleven possible "vertical" color values for the surface, based on the last row of http://www.cs.siena.edu/~lederman/truck/AdvanceDesignTrucks/html_color_chart.gif 

    pointWidth: 2, // The size of a rendered surface point (i.e., rectangle width and height) in pixels. 

    dTheta: 0.05, // The angle delta, in radians, by which to rotate the surface per key press. 

    surfaceScale: 24 // An empirically derived constant that makes the surface a good size for the given canvas size. 

    }; 



    // These are constants too but I've removed them from the above constants literal to ease typing and improve clarity. 

    var X = 0; 

    var Y = 1; 

    var Z = 2; 



    // ----------------------------------------------------------------------------------------------------- 



    var controlKeyPressed = false; // Shared between processKeyDown() and processKeyUp(). 

    var surface = new Surface(); // A set of points (in vector format) representing the surface. 



    // ----------------------------------------------------------------------------------------------------- 



    function point(x, y, z) 

    /* 

    Given a (x, y, z) surface point, returns the 3 x 1 vector form of the point. 

    */ 

    {  

    return [x, y, z]; // Return a 3 x 1 vector representing a traditional (x, y, z) surface point. This vector form eases matrix multiplication. 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    function Surface() 

    /* 

    A surface is a list of (x, y, z) points, in 3 x 1 vector format. This is a constructor function. 

    */ 

    { 

    this.points = []; 
    // An array of surface points in vector format. That is, each element of this array is a 3 x 1 array, as in [ [x1, y1, z1], [x2, y2, z2], [x3, y3, z3], ... ] 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.equation = function(x, y) 

    /* 

    Given the point (x, y), returns the associated z-coordinate based on the provided surface equation, of the form z = f(x, y). 

    */ 

    { 

    var d = Math.sqrt(x*x + y*y); // The distance d of the xy-point from the z-axis. 



    return 4*(Math.sin(d)/d); // Return the z-coordinate for the point (x, y, z). 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.generate = function() 

    /* 

    Creates a list of (x, y, z) points (in 3 x 1 vector format) representing the surface. 

    */ 

    { 

    var i = 0; 



    for (var x = constants.xMin; x <= constants.xMax; x += constants.xDelta) 

    { 

     for (var y = constants.yMin; y <= constants.yMax; y += constants.yDelta) 

     { 

     this.points[i] = point(x, y, this.equation(x, y)); // Store a surface point (in vector format) into the list of surface points.    

     ++i; 

     } 

    } 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.color = function() 

    /* 

    The color of a surface point is a function of its z-coordinate height. 

    */ 

    { 

    var z; // The z-coordinate for a given surface point (x, y, z). 



    this.zMin = this.zMax = this.points[0][Z]; // A starting value. Note that zMin and zMax are custom properties that could possibly be useful if this code is extended later. 

    for (var i = 0; i < this.points.length; i++) 

    {    

     z = this.points[i][Z]; 

     if (z < this.zMin) { this.zMin = z; } 

     if (z > this.zMax) { this.zMax = z; } 

    } 



    var zDelta = Math.abs(this.zMax - this.zMin)/constants.colorMap.length; 



    for (var i = 0; i < this.points.length; i++) 

    { 

     this.points[i].color = constants.colorMap[ Math.floor((this.points[i][Z]-this.zMin)/zDelta) ]; 

    } 



    /* Note that the prior FOR loop is functionally equivalent to the follow (much less elegant) loop:  

    for (var i = 0; i < this.points.length; i++) 

    { 

     if (this.points[i][Z] <= this.zMin + zDelta) {this.points[i].color = "#060";} 

     else if (this.points[i][Z] <= this.zMin + 2*zDelta) {this.points[i].color = "#090";} 

     else if (this.points[i][Z] <= this.zMin + 3*zDelta) {this.points[i].color = "#0C0";} 

     else if (this.points[i][Z] <= this.zMin + 4*zDelta) {this.points[i].color = "#0F0";} 

     else if (this.points[i][Z] <= this.zMin + 5*zDelta) {this.points[i].color = "#9F0";} 

     else if (this.points[i][Z] <= this.zMin + 6*zDelta) {this.points[i].color = "#9C0";} 

     else if (this.points[i][Z] <= this.zMin + 7*zDelta) {this.points[i].color = "#990";} 

     else if (this.points[i][Z] <= this.zMin + 8*zDelta) {this.points[i].color = "#960";} 

     else if (this.points[i][Z] <= this.zMin + 9*zDelta) {this.points[i].color = "#930";} 

     else if (this.points[i][Z] <= this.zMin + 10*zDelta) {this.points[i].color = "#900";}   

     else {this.points[i].color = "#C00";} 

    } 

    */ 

    } 



    // ----------------------------------------------------------------------------------------------------- 

    function update(){ 
document.querySelector("#xa").innerHTML = xangle; 
document.querySelector("#ya").innerHTML = yangle; 
document.querySelector("#za").innerHTML = zangle; 
} 

    function appendCanvasElement() 

    /* 

    Creates and then appends the "myCanvas" canvas element to the DOM. 

    */ 

    { 

    var canvasElement = document.createElement('canvas'); 



    canvasElement.width = constants.canvasWidth; 

    canvasElement.height = constants.canvasHeight; 

    canvasElement.id = "myCanvas"; 



    canvasElement.getContext('2d').translate(constants.canvasWidth/2, constants.canvasHeight/2); // Translate the surface's origin to the center of the canvas. 



    document.body.appendChild(canvasElement); // Make the canvas element a child of the body element. 

    } 



    //------------------------------------------------------------------------------------------------------ 

    Surface.prototype.sortByZIndex = function(A, B) 

    { 

    return A[Z] - B[Z]; // Determines if point A is behind, in front of, or at the same level as point B (with respect to the z-axis). 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.draw = function() 

    { 

    var myCanvas = document.getElementById("myCanvas"); // Required for Firefox. 

    var ctx = myCanvas.getContext("2d"); 
    var res; 
    var xm; 


    // this.points = surface.points.sort(surface.sortByZIndex); // Sort the set of points based on relative z-axis position. If the points are visibly small, you can sort of get away with removing this step. 


    for (var i = 0; i < this.points.length; i++) 

    { 

     ctx.fillStyle = this.points[i].color; 

     ctx.fillRect(this.points[i][X] * constants.surfaceScale, this.points[i][Y] * constants.surfaceScale, constants.pointWidth, constants.pointWidth); 


    }  

var c=document.getElementById("myCanvas"); 
var ctx=c.getContext("2d"); 
ctx.font="12px Arial"; 
ctx.fillStyle = "#000000"; 
ctx.fillText("X",this.points[p1][X] * constants.surfaceScale, this.points[p1][Y] * constants.surfaceScale); 
var c=document.getElementById("myCanvas"); 
var ctx1=c.getContext("2d"); 
ctx1.font="12px Arial"; 
ctx1.fillText("Y",this.points[p2][X] * constants.surfaceScale, this.points[p2][Y] * constants.surfaceScale); 
var c=document.getElementById("myCanvas"); 
var ctx1=c.getContext("2d"); 
ctx1.font="12px Arial"; 
ctx1.fillText("Z",this.points[p3][X] * constants.surfaceScale, this.points[p3][Y] * constants.surfaceScale); 

var c=document.getElementById("myCanvas"); 
var ctx1=c.getContext("2d"); 
ctx1.font="12px Arial"; 
ctx1.fillText("-Y",this.points[p4][X] * constants.surfaceScale, this.points[p4][Y] * constants.surfaceScale); 

var c=document.getElementById("myCanvas"); 
var ctx1=c.getContext("2d"); 
ctx1.font="12px Arial"; 
ctx1.fillText("-Z",this.points[p5][X] * constants.surfaceScale, this.points[p5][Y] * constants.surfaceScale); 

var c=document.getElementById("myCanvas"); 
var ctx1=c.getContext("2d"); 
ctx1.font="12px Arial"; 
ctx1.fillText("-X",this.points[p6][X] * constants.surfaceScale, this.points[p6][Y] * constants.surfaceScale); 


    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.multi = function(R) 

    /* 

    Assumes that R is a 3 x 3 matrix and that this.points (i.e., P) is a 3 x n matrix. This method performs P = R * P. 

    */ 

    { 

    var Px = 0, Py = 0, Pz = 0; // Variables to hold temporary results. 

    var P = this.points; // P is a pointer to the set of surface points (i.e., the set of 3 x 1 vectors). 

    var sum; // The sum for each row/column matrix product. 



    for (var V = 0; V < P.length; V++) // For all 3 x 1 vectors in the point list. 

    { 

     Px = P[V][X], Py = P[V][Y], Pz = P[V][Z]; 

     for (var Rrow = 0; Rrow < 3; Rrow++) // For each row in the R matrix. 

     { 

     sum = (R[Rrow][X] * Px) + (R[Rrow][Y] * Py) + (R[Rrow][Z] * Pz); 

     P[V][Rrow] = sum; 

     } 

    }  

    } 
Surface.prototype.multipt = function(R) 

    /* 

    Assumes that R is a 3 x 3 matrix and that this.points (i.e., P) is a 3 x n matrix. This method performs P = R * P. 

    */ 

    { 

    var Px = 0, Py = 0, Pz = 0; // Variables to hold temporary results. 

    var P = this.points; // P is a pointer to the set of surface points (i.e., the set of 3 x 1 vectors). 

    var sum; // The sum for each row/column matrix product. 



    for (var V = P.length-1; V < P.length; V++) // For all 3 x 1 vectors in the point list. 

    { 

     Px = P[V][X], Py = P[V][Y], Pz = P[V][Z]; 

     for (var Rrow = 0; Rrow < 3; Rrow++) // For each row in the R matrix. 

     { 

     sum = (R[Rrow][X] * Px) + (R[Rrow][Y] * Py) + (R[Rrow][Z] * Pz); 

     P[V][Rrow] = sum; 

     } 

    }  

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.erase = function() 

    { 

    var myCanvas = document.getElementById("myCanvas"); // Required for Firefox. 

    var ctx = myCanvas.getContext("2d"); 



    ctx.clearRect(-constants.canvasWidth/2, -constants.canvasHeight/2, myCanvas.width, myCanvas.height); 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.xRotate = function(sign) 

    /* 

    Assumes "sign" is either 1 or -1, which is used to rotate the surface "clockwise" or "counterclockwise". 

    */ 

    { 

    var Rx = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; // Create an initialized 3 x 3 rotation matrix. 



    Rx[0][0] = 1; 

    Rx[0][1] = 0; // Redundant but helps with clarity. 

    Rx[0][2] = 0; 

    Rx[1][0] = 0; 

    Rx[1][1] = Math.cos(sign*constants.dTheta); 

    Rx[1][2] = -Math.sin(sign*constants.dTheta); 

    Rx[2][0] = 0; 

    Rx[2][1] = Math.sin(sign*constants.dTheta); 

    Rx[2][2] = Math.cos(sign*constants.dTheta); 



    this.multi(Rx); // If P is the set of surface points, then this method performs the matrix multiplcation: Rx * P 

    this.erase(); // Note that one could use two canvases to speed things up, which also eliminates the need to erase. 

    this.draw(); 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.yRotate = function(sign) 

    /* 

    Assumes "sign" is either 1 or -1, which is used to rotate the surface "clockwise" or "counterclockwise". 

    */  

    { 

    var Ry = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; // Create an initialized 3 x 3 rotation matrix. 



    Ry[0][0] = Math.cos(sign*constants.dTheta); 

    Ry[0][1] = 0; // Redundant but helps with clarity. 

    Ry[0][2] = Math.sin(sign*constants.dTheta); 

    Ry[1][0] = 0; 

    Ry[1][1] = 1; 

    Ry[1][2] = 0; 

    Ry[2][0] = -Math.sin(sign*constants.dTheta); 

    Ry[2][1] = 0; 

    Ry[2][2] = Math.cos(sign*constants.dTheta); 



    this.multi(Ry); // If P is the set of surface points, then this method performs the matrix multiplcation: Rx * P 

    this.erase(); // Note that one could use two canvases to speed things up, which also eliminates the need to erase. 

    this.draw(); 

    } 



    // ----------------------------------------------------------------------------------------------------- 



    Surface.prototype.zRotate = function(sign) 

    /* 

    Assumes "sign" is either 1 or -1, which is used to rotate the surface "clockwise" or "counterclockwise". 

    */  

    { 

    var Rz = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; // Create an initialized 3 x 3 rotation matrix. 



    Rz[0][0] = Math.cos(sign*constants.dTheta); 

    Rz[0][1] = -Math.sin(sign*constants.dTheta);   

    Rz[0][2] = 0; // Redundant but helps with clarity. 

    Rz[1][0] = Math.sin(sign*constants.dTheta); 

    Rz[1][1] = Math.cos(sign*constants.dTheta); 

    Rz[1][2] = 0; 

    Rz[2][0] = 0 

    Rz[2][1] = 0; 

    Rz[2][2] = 1; 



    this.multi(Rz); // If P is the set of surface points, then this method performs the matrix multiplcation: Rx * P 

    this.erase(); // Note that one could use two canvases to speed things up, which also eliminates the need to erase. 

    this.draw(); 

    } 


Surface.prototype.xRotatept = function() 

    { 

    var Rx = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; 



    Rx[0][0] = 1; 

    Rx[0][1] = 0; 

    Rx[0][2] = 0; 

    Rx[1][0] = 0; 

    Rx[1][1] = Math.cos(xangle); 

    Rx[1][2] = -Math.sin(xangle); 

    Rx[2][0] = 0; 

    Rx[2][1] = Math.sin(xangle); 

    Rx[2][2] = Math.cos(xangle); 


    this.multipt(Rx); 

    this.erase(); 

    this.draw(); 

    } 




    Surface.prototype.yRotatept = function() 


    { 

    var Ry = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; 



    Ry[0][0] = Math.cos(yangle); 

    Ry[0][1] = 0; 

    Ry[0][2] = Math.sin(yangle); 

    Ry[1][0] = 0; 

    Ry[1][1] = 1; 

    Ry[1][2] = 0; 

    Ry[2][0] = -Math.sin(yangle); 

    Ry[2][1] = 0; 

    Ry[2][2] = Math.cos(yangle); 



    this.multipt(Ry); 

    this.erase(); 

    this.draw(); 

    } 




    Surface.prototype.zRotatept = function() 



    { 

    var Rz = [ [0, 0, 0], 

       [0, 0, 0], 

       [0, 0, 0] ]; 



    Rz[0][0] = Math.cos(zangle); 

    Rz[0][1] = -Math.sin(zangle);   

    Rz[0][2] = 0; 

    Rz[1][0] = Math.sin(zangle); 

    Rz[1][1] = Math.cos(zangle); 

    Rz[1][2] = 0; 

    Rz[2][0] = 0 

    Rz[2][1] = 0; 

    Rz[2][2] = 1; 



    this.multipt(Rz); 

    this.erase(); 

    this.draw(); 

    } 




    // ----------------------------------------------------------------------------------------------------- 



    function processKeyDown(evt) 

    {      

    if (evt.ctrlKey) 

    { 

     switch (evt.keyCode) 

     { 

     case constants.upArrow: 

      // No operation other than preventing the default behavior of the arrow key. 

      evt.preventDefault(); // This prevents the default behavior of the arrow keys, which is to scroll the browser window when scroll bars are present. The user can still scroll the window with the mouse.    

      break; 

     case constants.downArrow: 

      // No operation other than preventing the default behavior of the arrow key. 

      evt.preventDefault(); 

      break; 

     case constants.leftArrow: 

      // console.log("ctrl+leftArrow"); 
       zangle=zangle-0.05; 
       update(); 
     if(zangle<=-2*Math.PI) 
     { 
      zangle=0; 

     } 
      surface.zRotate(-1); // The sign determines if the surface rotates "clockwise" or "counterclockwise". 

      evt.preventDefault(); 

      break; 

     case constants.rightArrow: 

      // console.log("ctrl+rightArrow"); 
      zangle=zangle+0.05; 
      update(); 
     if(zangle>=2*Math.PI) 
     { 
      zangle=0; 

     } 
      surface.zRotate(1); 

      evt.preventDefault(); 

      break; 

     } 

     return; // When the control key is pressed, only the left and right arrows have meaning, no need to process any other key strokes (i.e., bail now). 

    } 



    // Assert: The control key is not pressed. 



    switch (evt.keyCode) 

    { 

     case constants.upArrow: 

     // console.log("upArrow"); 

     xangle=xangle+0.05; 
     update(); 
     if(xangle>=2*Math.PI) 
     { 
      xangle=0; 

     } 

     surface.xRotate(1); 

     evt.preventDefault(); 

     break; 

     case constants.downArrow: 

     // console.log("downArrow"); 
     xangle=xangle-0.05; 
     update(); 
     if(xangle<=-2*Math.PI) 
     { 

      xangle=0; 
     } 

     surface.xRotate(-1); 

     evt.preventDefault(); 

     break; 

     case constants.leftArrow: 

     // console.log("leftArrow"); 
     yangle=yangle-0.05; 
     update(); 
     if(yangle<=-2*Math.PI) 
     { 
      yangle=0; 

     } 
     surface.yRotate(-1); 

     evt.preventDefault(); 

     break; 

     case constants.rightArrow: 

     // console.log("rightArrow"); 
     yangle=yangle+0.05; 
     update(); 
     if(yangle>=2*Math.PI) 
     { 
      yangle=0; 

     } 
     surface.yRotate(1); 

     evt.preventDefault(); 

     break; 

    } 

    } 



    // ----------------------------------------------------------------------------------------------------- 
Surface.prototype.plot = function(x, y, z) 
    /* 
    add the point (x, y, z) (in 3 x 1 vector format) to the surface. 
    */ 
    { 

     this.points.push(point(x, y, z)); // Store a surface point 
     var x=0; 
     for (var x = constants.xMin; x <= constants.xMax; x += constants.xDelta) 
     { 
     this.points.push(point(x, 0, 0)); 
     } 
     p6=1; 
     p1=this.points.length-1; 
     p4=this.points.length; 
     /*var y=-0.2 
     for (var x = constants.xMax+1; x <= constants.xMax+2; x += constants.xDelta) 
     { 
     this.points.push(point(x, y, 0)); 
     y=y+0.002 
     }*/ 

     /*for (var x = constants.xMax+1; x <= constants.xMax+2; x += constants.xDelta) 
     { 
     this.points.push(point(11, 0, 0)) 
     }*/ 
     for (var x = constants.xMin; x <= constants.xMax; x += constants.yDelta) 
     { 
     this.points.push(point(0, x, 0)); 
     } 
     p2=this.points.length-1; 
     p5=this.points.length; 
     for (var x = constants.xMin; x <= constants.xMax; x += constants.yDelta) 
     { 
     this.points.push(point(0,0,x)); 
     } 
     p3=this.points.length-1; 

    } 
    Surface.prototype.plot1 = function(x, y, z) 
    /* 
    add the point (x, y, z) (in 3 x 1 vector format) to the surface. 
    */ 
    {  


     this.points.push(point(x, y, z)); // Store a surface point 
    surface.xRotatept(); 
    surface.yRotatept(); 

    surface.zRotatept(); 
     this.draw(); 

    } 


    function onloadInit() 

    { 

    appendCanvasElement(); // Create and append the canvas element to the DOM. 

    surface.draw(); // Draw the surface on the canvas. 

    document.addEventListener('keydown', processKeyDown, false); // Used to detect if the control key has been pressed. 

    } 



    // ----------------------------------------------------------------------------------------------------- 




    //surface.generate(); // Creates the set of points reprsenting the surface. Must be called before color(). 
surface.plot(0,0,0); 
    surface.color(); // Based on the min and max z-coordinate values, chooses colors for each point based on the point's z-ccordinate value (i.e., height). 

    window.addEventListener('load', onloadInit, false); // Perform processing that must occur after the page has fully loaded. 

    </script> 

</head> 

    <body> 
<table align="center"> 
<tr><td> 
<h5 style="color:#606">Enter the value of (X,Y,Z)</h5> 
      <input type="text" value="5" class="num-input" width="50" size="2" id="x-input"> 
      <input type="text" value="5" class="num-input" width="50" size="2" id="y-input"> 
      <input type="text" value="2" class="num-input" width="50" size="2" id="z-input"> 
      <input type="button" value="Plot Point" onClick="surface.plot1(document.getElementById('x-input').value,document.getElementById('y-input').value,document.getElementById('z-input').value); "> 

      </td></tr></table> 
<table align="center"> <tr><td> 
<span id="xa">0</span>deg<br> 
<span id="ya">0</span>deg<br> 
<span id="za">0</span>deg</td></tr></table> 
</body> 

</html> 
+0

是的,我bwroga糾正。您需要將所有旋轉累加到一個最終的變換矩陣中。這裏是一個關於3D旋轉和它們變換矩陣的非常好的教程 - http://www.html5rocks.com/en/tutorials/webgl/webgl_orthographic_3d/ – markE 2013-02-22 18:39:08

回答

1

旋轉沿多軸最終輸出可以根據訂單有所不同,你旋轉軸',你需要做的是記錄沿着eac的總旋轉h軸(如三個數字,不使用矩陣)。每次更新旋轉值時,將所有三個總旋轉以正確的順序應用於單位矩陣(嘗試x,y,z)。始終使用相同的順序。然後使用它來轉換你的座標。

+0

現在我將沿着每個x,y和z軸的旋轉角度存儲起來在單獨的變量,然後我沿着x應用旋轉矩陣,然後y然後z.Is這正確的是我在做什麼? – Navaneet 2013-02-23 04:47:50

+0

@Navaneet這應該是正確的。每次應用旋轉之前,請不要忘記將旋轉矩陣重置爲單位矩陣。 – bwroga 2013-02-23 05:27:24

+0

我所做的就是在xyz平面或圖形上繪製一個點,然後像我想要的那樣旋轉。在旋轉過程中,我會跟蹤爲x,y,z軸旋轉的角度。然後,如果繪製一個新點,那麼我不會重置x,y和z軸,而是將該點繪製在處於某種旋轉模式的同一平面上。什麼可能是旋轉的順序我做了旋轉矩陣的那一點關於x軸先x軸然後y最後z。但我仍然有錯誤。另外,在圍繞一個軸旋轉時,我只能跟蹤該軸的角度旋轉。是這樣嗎? – Navaneet 2013-02-23 05:43:05

0

這裏是我的意見:

JAVASCRIPT

var canvas = document.getElementById("myCanvas"); 
var ctx2 = canvas.getContext("2d"); 
ctx2.fillStyle='#333'; 

ctx2.fillRect(50,50,100,100); 
var ctx = canvas.getContext("2d"); 


ctx.fillStyle='red'; 

var deg = Math.PI/180; 

ctx.save(); 
    ctx.translate(100, 100); 
    ctx.rotate(45 * deg); 
    ctx.fillRect(-50,-50,100,100); 
ctx.restore(); 

ctx2是舊的位置和CTX是形狀的新位置。根據要定位形狀的位置,必須使用相同的x,y座標來轉換形狀。然後你必須輸入值爲ctx.fillRect(x,y,w,h);保留x和y作爲-ve值(高度和寬度的一半以保持在畫布的對角線上,否則改爲操縱它)。和h,w作爲你想要的值。

DEMO