您可以將數字分解回各種組件。有兩種常用的技術可以做到這一點,稱爲QR和LU。
如果你給它當前的矩陣值,你可以這樣做:
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
};
}
現在你可以使用該對象和它的值來設置旋轉,縮放等
參見我transformation-matrix-js和Decomposition of 2D transform-matrices爲更多細節。
來源
2015-06-01 21:38:56
K3N