2012-05-15 13 views
6

Ho un elemento cubo che viene ruotato e tradotto in uno spazio 3d.Come eseguire il reverse engineering di una trasformazione di matrix3d ​​Webkit

Voglio scoprire solo la parte di rotazione di questa trasformazione in un dato momento. È possibile?

ho:

var cube = document.getElementById("cube"); 
var transform = getComputedStyle(cube).webkitTransform; 

//the value of transform is: 
// matrix3d(-1, 0, 0.00000000000000012246467991473532, 0, 
//   0, 1, 0, 0, 
//   -0.00000000000000012246467991473532, 0, -1, 0, 
//   0, 0, 0, 1) 

Aiuto? :-)

+0

ottenere valore y della trasformazione? potresti voler dare un'occhiata a translate(). che converte la matrice in un valore. più su questo qui [tradurre trasformazione] (https://developer.mozilla.org/en/CSS/transform-function#translate()) –

+0

Forse non seguo, ma sono bloccato con questa matrice3d, la trasformazione viene applicato per me dal codice di qualcun altro. Sto solo cercando di vedere se riesco a tornare a GIUSTO sulla rotazione –

+0

ho frainteso. pensato che vuoi usare il valore della trasformazione per fare qualcosa. ignorare il commento. Ho trovato una funzione su questo link [Leggi getRotationDegrees] (http://stackoverflow.com/questions/3336101/css3-animation-on-transform-rotate-way-to-fetch-current-deg-of-the-rotating- EL). E leggi il link sopra per ottenere quale valore appartiene a Y. e usa il modo in cui usano la funzione per ottenere il grado Y. Leggerò su questo più tardi, se non hai la risposta. scusa per non essere abbastanza disponibile. sto andando a lavoro. :) –

risposta

4

Se il tuo oggetto viene ruotato solo su Y, allora sì, la rotazione è semplice da calcolare: è il arccos of Matrix elements 1,1 o 3,3 o l'arsin di - (elemento 3,1) o arcsin dell'elemento 1 , 3. Nel tuo esempio specifico, è di circa 180 gradi. Se le rotazioni superano di poco l'asse y, è necessario iniziare a tracciare le rotazioni precedenti e può diventare più confuso.

La tua traduzione sarà solo la riga inferiore della matrice di output, 0,0,0 in questo caso.

vedere esempi su http://www.inversereality.org/tutorials/graphics%20programming/3dwmatrices.html

4

È possibile recuperare informazioni di trasformazione con la funzione "nuova WebKitCSSMatrix". Per esempio:

var matrix = new WebKitCSSMatrix($('#element').css('-webkit-transform')); 
var translateZ = matrix.m43; 

Ogni parte della matrice è spiegare qui: http://9elements.com/html5demos/matrix3d/ È possibile recuperare le proprietà più complexe come rotationX, per Matrix 3D con la matematica pasticcio:

var rotationX = Math.acos(matrix.a) * (180/Math.PI); 
var rotationY = Math.asin(matrix.b) * (180/Math.PI); 
2

funzione data determina rotation*, transform* e scale per il dato elemento DOM cross-browser.

Se volete qualcosa di più, o di trovare bug in matematica, si tratta di una discussione sulla math.stackexchange.com

Testato sui browser elencati in http://caniuse.com/transforms3d:

var reverseEngineerTransform3d = function (domElement, parameterName) { 

     parameterName = parameterName.toLowerCase(); 

     var computedStyle = window.getComputedStyle(domElement), 
      matrixStyle = computedStyle.transform || computedStyle.WebkitTransform || computedStyle.MozTransform || computedStyle.msTransform || computedStyle.OTransform; 

     if (matrixStyle) { 
      matrixStyle = matrixStyle.trim(); 
     } 

     if (matrixStyle === 'none') { 
      if (parameterName.indexOf('rotate') === 0) { 
       return '0deg'; 
      } else if (parameterName.indexOf('translate') === 0) { 
       return '0px'; 
      } else if (parameterName === 'scale') { 
       return 1; 
      } else { 
       throw "Unsupported 3D parameter " + parameterName; 
      } 
     } 

     else if (!matrixStyle || matrixStyle.substr(0, 9) !== 'matrix3d(') { 
      //return something or die ????? 
      throw "Something is wrong with 3D transform style. Probably no style applied at all OR unknown CSS3 vendor OR unknown/unsupported 3D matrix representation string OR CSS3 3D transform is not fully supported in this browser"; 
     } 

     var matrix = window.WebKitCSSMatrix && (new WebKitCSSMatrix(matrixStyle)) || 
       window.MozCSSMatrix && (new MozCSSMatrix(matrixStyle)) || 
       window.MsCSSMatrix && (new MsCSSMatrix(matrixStyle)) || 
       window.OCSSMatrix && (new OCSSMatrix(matrixStyle)) || 
       window.CSSMatrix && (new CSSMatrix(matrixStyle)); // sorry 4 copy-paste my friends 

     if (!matrix || isNaN(matrix.a) || isNaN(matrix.b) || isNaN(matrix.c) || isNaN(matrix.m41) || isNaN(matrix.m42) || isNaN(matrix.m43) || isNaN(matrix.m11)) { 
      throw "Could not catch CSSMatrix constructor for current browser, OR the constructor has returned a non-standard matrix object (need .a, .b, .c and mXX numerical properties to work)"; 
     } 


     // todo: giving a parameters array (and returning an array) is a good idea, or we could return everything if no parameters given 

     switch (parameterName) { 

      case 'rotatey': // in degrees 
       return Math.acos(matrix.m11)*180/Math.PI * (matrix.m13 > 0 ? -1 : 1) + 'deg'; 

      case 'rotatex': // in degrees 
       return Math.asin(matrix.m22)*180/Math.PI + 'deg'; 

      case 'rotatez': // in degrees 
       throw "TODO: Sorry, math people. I really need help here! Please implement this case for me. This will help you: http://9elements.com/html5demos/matrix3d/"; 

      case 'translatex': // in pixels 
       return matrix.m41 + 'px'; 

      case 'translatey': // in pixels 
       return matrix.m42 + 'px'; 

      case 'translatez': // in pixels 
       return matrix.m43 + 'px'; 

      case 'scale': // no units 
       if (matrix.m11 === matrix.m22 && matrix.m22 === matrix.m33) { 
        return matrix.m11; 
       } else { 
        throw "I'm not so smart to calculate scale from this matrix"; 
       } 

      // todo: anything else? skew ???? 

      default: 
       throw "This function accepts 3d-parameter name (case-insensitive string) as the second argument. Currently supported: 'rotate[xyz]', 'translate[xyz]', 'scale'. This parameter is unsupported: " + parameterName; 
     } 
    }; 
+1

Uno con migliori abilità matematiche può estendere le possibilità di funzione. Una volta ottenuta la matrice, possiamo eseguire il reverse engineering di un sacco di cose – Dan

+0

// Todo: un buon articolo che elaborerò presto: http://css-tricks.com/get-value-of-css-rotation-through- javascript / – Dan

Problemi correlati