Skip to content

Commit

Permalink
Optimize Matrix2d and fix skew spec conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Jan 9, 2019
1 parent ca611a4 commit 88adb2a
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions lib/Matrix2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class Matrix2D {
reset = function() {
this.a = this.d = 1;
this.b = this.c = this.tx = this.ty = 0;
this.hasInitialState = true;
return this;
};

Expand All @@ -99,6 +100,16 @@ export default class Matrix2D {
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
append = function(a, b, c, d, tx, ty) {
if (this.hasInitialState) {
this.hasInitialState = false;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.tx = tx;
this.ty = ty;
return this;
}
const a1 = this.a;
const b1 = this.b;
const c1 = this.c;
Expand Down Expand Up @@ -141,7 +152,7 @@ export default class Matrix2D {
skewX,
skewY,
regX,
regY
regY,
) {
let cos, sin;
if (rotation % 360) {
Expand All @@ -153,35 +164,17 @@ export default class Matrix2D {
sin = 0;
}

const a = cos * scaleX;
const b = sin * scaleX;
const c = -sin * scaleY;
const d = cos * scaleY;

if (skewX || skewY) {
// TODO: can this be combined into a single append operation?
skewX *= DEG_TO_RAD;
skewY *= DEG_TO_RAD;
this.append(
Math.cos(skewY),
Math.sin(skewY),
Math.sin(skewX),
Math.cos(skewX),
x,
y
);
this.append(
cos * scaleX,
sin * scaleX,
-sin * scaleY,
cos * scaleY,
0,
0
);
const b1 = Math.tan(skewY * DEG_TO_RAD);
const c1 = Math.tan(skewX * DEG_TO_RAD);
this.append(a + c1 * b, b1 * a + b, c + c1 * d, b1 * c + d, x, y);
} else {
this.append(
cos * scaleX,
sin * scaleX,
-sin * scaleY,
cos * scaleY,
x,
y
);
this.append(a, b, c, d, x, y);
}

if (regX || regY) {
Expand Down

0 comments on commit 88adb2a

Please sign in to comment.