È possibile scomporre i numeri in vari componenti. Ci sono due tecniche comuni per fare questo chiamato QR e LU in breve.
Se si alimentano i valori della matrice di corrente che si potrebbe fare:
function decompose(a, b, c, d, e, f, useLU) {
var acos = Math.acos, // caching for readability below
atan = Math.atan,
sqrt = Math.sqrt,
pi = Math.PI,
translate = {x: e, y: f},
rotation = 0,
scale = {x: 1, y: 1},
skew = {x: 0, y: 0},
determ = a * d - b * c; // get determinant
if (useLU) {
if (a) {
skew = {x: atan(c/a), y: atan(b/a)};
scale = {x: a, y: determ/a};
}
else if (b) {
rotation = pi * 0.5;
scale = {x: b, y: determ/b};
skew.x = atan(d/b);
}
else { // a = b = 0
scale = {x: c, y: d};
skew.x = pi * 0.25;
}
}
else {
// Apply the QR-like decomposition.
if (a || b) {
var r = sqrt(a*a + b*b);
rotation = b > 0 ? acos(a/r) : -acos(a/r);
scale = {x: r, y: determ/r};
skew.x = atan((a*c + b*d)/(r*r));
}
else if (c || d) {
var s = sqrt(c*c + d*d);
rotation = pi * 0.5 - (d > 0 ? acos(-c/s) : -acos(c/s));
scale = {x: determ/s, y: s};
skew.y = atan((a*c + b*d)/(s*s));
}
else { // a = b = c = d = 0
scale = {x:0, y:0}; // = invalid matrix
}
}
return {
scale : scale,
translate: translate,
rotation : rotation,
skew : skew
};
}
Ora è possibile utilizzare l'oggetto ei suoi valori per impostare la rotazione, scala ecc
Vedi anche la mia transformation-matrix-js e Decomposition of 2D transform-matrices per più dettagli.