(matrix: number[])
| 25 | |
| 26 | // TODO: Decompose rotations over X and Y axis |
| 27 | export function decompose2DTransformMatrix(matrix: number[]): TransformFunctionsInfo { |
| 28 | verifyTransformMatrix(matrix); |
| 29 | |
| 30 | const [A, B, C, D, E, F] = [...matrix]; |
| 31 | const determinant = A * D - B * C; |
| 32 | const translate = { x: E || 0, y: F || 0 }; |
| 33 | |
| 34 | // rewrite with obj destructuring using the identity matrix |
| 35 | let rotate = 0; |
| 36 | let scale = { x: 1, y: 1 }; |
| 37 | if (A || B) { |
| 38 | const R = Math.sqrt(A * A + B * B); |
| 39 | rotate = B > 0 ? Math.acos(A / R) : -Math.acos(A / R); |
| 40 | scale = { x: R, y: determinant / R }; |
| 41 | } else if (C || D) { |
| 42 | const R = Math.sqrt(C * C + D * D); |
| 43 | rotate = Math.PI / 2 - (D > 0 ? Math.acos(-C / R) : -Math.acos(C / R)); |
| 44 | scale = { x: determinant / R, y: R }; |
| 45 | } |
| 46 | |
| 47 | rotate = radiansToDegrees(rotate); |
| 48 | |
| 49 | return { translate, rotate: { x: 0, y: 0, z: rotate }, scale }; |
| 50 | } |
| 51 | |
| 52 | function verifyTransformMatrix(matrix: number[]) { |
| 53 | if (matrix.length < 6) { |
no test coverage detected