diff --git a/src/core/display/Transform.js b/src/core/display/Transform.js index 8611925..718d791 100644 --- a/src/core/display/Transform.js +++ b/src/core/display/Transform.js @@ -54,25 +54,23 @@ */ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; } /** - * Updates the skew values when the skew changes. + * Updates the skew values when the skew or rotation changes. * * @private */ updateSkew() { - this._cy = Math.cos(this.skew.y); - this._sy = Math.sin(this.skew.y); - this._nsx = Math.sin(this.skew.x); - this._cx = Math.cos(this.skew.x); + this._cx = Math.cos(this.rotation - this.skew._y); + this._sx = Math.sin(this.rotation - this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) + this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) + this.skew._x); } /** @@ -81,15 +79,14 @@ updateLocalTransform() { const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; + + lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); + lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); } /** @@ -103,15 +100,10 @@ const wt = this.worldTransform; const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c)); lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d)); @@ -156,7 +148,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); + this.updateSkew(); } } diff --git a/src/core/display/Transform.js b/src/core/display/Transform.js index 8611925..718d791 100644 --- a/src/core/display/Transform.js +++ b/src/core/display/Transform.js @@ -54,25 +54,23 @@ */ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; } /** - * Updates the skew values when the skew changes. + * Updates the skew values when the skew or rotation changes. * * @private */ updateSkew() { - this._cy = Math.cos(this.skew.y); - this._sy = Math.sin(this.skew.y); - this._nsx = Math.sin(this.skew.x); - this._cx = Math.cos(this.skew.x); + this._cx = Math.cos(this.rotation - this.skew._y); + this._sx = Math.sin(this.rotation - this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) + this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) + this.skew._x); } /** @@ -81,15 +79,14 @@ updateLocalTransform() { const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; + + lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); + lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); } /** @@ -103,15 +100,10 @@ const wt = this.worldTransform; const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c)); lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d)); @@ -156,7 +148,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); + this.updateSkew(); } } diff --git a/src/core/display/TransformStatic.js b/src/core/display/TransformStatic.js index c0c2f33..521f1f2 100644 --- a/src/core/display/TransformStatic.js +++ b/src/core/display/TransformStatic.js @@ -47,12 +47,10 @@ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; this._localID = 0; this._currentLocalID = 0; @@ -69,16 +67,16 @@ } /** - * Called when skew changes + * Called when skew or rotation changes * * @private */ updateSkew() { - this._cy = Math.cos(this.skew._y); - this._sy = Math.sin(this.skew._y); - this._nsx = Math.sin(this.skew._x); - this._cx = Math.cos(this.skew._x); + this._cx = Math.cos(this.rotation + this.skew._y); + this._sx = Math.sin(this.rotation + this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) - this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) - this.skew._x); this._localID ++; } @@ -93,15 +91,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -126,15 +119,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -191,8 +179,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); - this._localID ++; + this.updateSkew(); } } diff --git a/src/core/display/Transform.js b/src/core/display/Transform.js index 8611925..718d791 100644 --- a/src/core/display/Transform.js +++ b/src/core/display/Transform.js @@ -54,25 +54,23 @@ */ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; } /** - * Updates the skew values when the skew changes. + * Updates the skew values when the skew or rotation changes. * * @private */ updateSkew() { - this._cy = Math.cos(this.skew.y); - this._sy = Math.sin(this.skew.y); - this._nsx = Math.sin(this.skew.x); - this._cx = Math.cos(this.skew.x); + this._cx = Math.cos(this.rotation - this.skew._y); + this._sx = Math.sin(this.rotation - this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) + this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) + this.skew._x); } /** @@ -81,15 +79,14 @@ updateLocalTransform() { const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; + + lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); + lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); } /** @@ -103,15 +100,10 @@ const wt = this.worldTransform; const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c)); lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d)); @@ -156,7 +148,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); + this.updateSkew(); } } diff --git a/src/core/display/TransformStatic.js b/src/core/display/TransformStatic.js index c0c2f33..521f1f2 100644 --- a/src/core/display/TransformStatic.js +++ b/src/core/display/TransformStatic.js @@ -47,12 +47,10 @@ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; this._localID = 0; this._currentLocalID = 0; @@ -69,16 +67,16 @@ } /** - * Called when skew changes + * Called when skew or rotation changes * * @private */ updateSkew() { - this._cy = Math.cos(this.skew._y); - this._sy = Math.sin(this.skew._y); - this._nsx = Math.sin(this.skew._x); - this._cx = Math.cos(this.skew._x); + this._cx = Math.cos(this.rotation + this.skew._y); + this._sx = Math.sin(this.rotation + this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) - this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) - this.skew._x); this._localID ++; } @@ -93,15 +91,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -126,15 +119,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -191,8 +179,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); - this._localID ++; + this.updateSkew(); } } diff --git a/test/core/TransformStatic.js b/test/core/TransformStatic.js new file mode 100644 index 0000000..7e79029 --- /dev/null +++ b/test/core/TransformStatic.js @@ -0,0 +1,74 @@ +'use strict'; + +describe('PIXI.TransformStatic', () => +{ + describe('setFromMatrix', () => + { + it('should decompose correct rotation', () => + { + const eps = 1e-3; + + const transform = new PIXI.TransformStatic(); + const parent = new PIXI.TransformStatic(); + const otherTransform = new PIXI.TransformStatic(); + + transform.position.set(20, 10); + transform.scale.set(2, -3); + transform.rotation = Math.PI / 4; + transform.updateTransform(parent); + + otherTransform.setFromMatrix(transform.worldTransform); + + const position = otherTransform.position; + const scale = otherTransform.scale; + const skew = otherTransform.skew; + + expect(position.x).to.be.closeTo(20, eps); + expect(position.y).to.be.closeTo(10, eps); + expect(scale.x).to.be.closeTo(2, eps); + expect(scale.y).to.be.closeTo(-3, eps); + expect(skew.x).to.be.equal(0); + expect(skew.y).to.be.equal(0); + expect(otherTransform.rotation).to.be.closeTo(Math.PI / 4, eps); + }); + + it('should apply skew before scale, like in CSS', () => + { + // this example looks the same in CSS and in pixi, made with pixi-animate by @bigtimebuddy + + const eps = 1e-3; + + const transform = new PIXI.TransformStatic(); + const parent = new PIXI.TransformStatic(); + const otherTransform = new PIXI.TransformStatic(); + + transform.position.set(387.8, 313.95); + transform.scale.set(0.572, 4.101); + transform.skew.set(-0.873, 0.175); + transform.updateTransform(parent); + + const mat = transform.worldTransform; + + expect(mat.a).to.be.closeTo(0.563, eps); + expect(mat.b).to.be.closeTo(0.010, eps); + expect(mat.c).to.be.closeTo(-3.142, eps); + expect(mat.d).to.be.closeTo(2.635, eps); + expect(mat.tx).to.be.closeTo(387.8, eps); + expect(mat.ty).to.be.closeTo(313.95, eps); + + otherTransform.setFromMatrix(transform.worldTransform); + + const position = otherTransform.position; + const scale = otherTransform.scale; + const skew = otherTransform.skew; + + expect(position.x).to.be.closeTo(387.8, eps); + expect(position.y).to.be.closeTo(313.95, eps); + expect(scale.x).to.be.closeTo(0.572, eps); + expect(scale.y).to.be.closeTo(4.101, eps); + expect(skew.x).to.be.equal(-0.873); + expect(skew.y).to.be.equal(0.175); + expect(otherTransform.rotation).to.be.equal(0); + }); + }); +}); diff --git a/src/core/display/Transform.js b/src/core/display/Transform.js index 8611925..718d791 100644 --- a/src/core/display/Transform.js +++ b/src/core/display/Transform.js @@ -54,25 +54,23 @@ */ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; } /** - * Updates the skew values when the skew changes. + * Updates the skew values when the skew or rotation changes. * * @private */ updateSkew() { - this._cy = Math.cos(this.skew.y); - this._sy = Math.sin(this.skew.y); - this._nsx = Math.sin(this.skew.x); - this._cx = Math.cos(this.skew.x); + this._cx = Math.cos(this.rotation - this.skew._y); + this._sx = Math.sin(this.rotation - this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) + this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) + this.skew._x); } /** @@ -81,15 +79,14 @@ updateLocalTransform() { const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; + + lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); + lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); } /** @@ -103,15 +100,10 @@ const wt = this.worldTransform; const lt = this.localTransform; - const a = this._cr * this.scale.x; - const b = this._sr * this.scale.x; - const c = -this._sr * this.scale.y; - const d = this._cr * this.scale.y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position.x - ((this.pivot.x * lt.a) + (this.pivot.y * lt.c)); lt.ty = this.position.y - ((this.pivot.x * lt.b) + (this.pivot.y * lt.d)); @@ -156,7 +148,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); + this.updateSkew(); } } diff --git a/src/core/display/TransformStatic.js b/src/core/display/TransformStatic.js index c0c2f33..521f1f2 100644 --- a/src/core/display/TransformStatic.js +++ b/src/core/display/TransformStatic.js @@ -47,12 +47,10 @@ this._rotation = 0; - this._sr = Math.sin(0); - this._cr = Math.cos(0); - this._cy = Math.cos(0);// skewY); - this._sy = Math.sin(0);// skewY); - this._nsx = Math.sin(0);// skewX); - this._cx = Math.cos(0);// skewX); + this._cx = 1; // cos rotation + skewY; + this._sx = 0; // sin rotation + skewY; + this._cy = 0; // cos rotation + Math.PI/2 - skewX; + this._sy = 1; // sin rotation + Math.PI/2 - skewX; this._localID = 0; this._currentLocalID = 0; @@ -69,16 +67,16 @@ } /** - * Called when skew changes + * Called when skew or rotation changes * * @private */ updateSkew() { - this._cy = Math.cos(this.skew._y); - this._sy = Math.sin(this.skew._y); - this._nsx = Math.sin(this.skew._x); - this._cx = Math.cos(this.skew._x); + this._cx = Math.cos(this.rotation + this.skew._y); + this._sx = Math.sin(this.rotation + this.skew._y); + this._cy = Math.cos(this.rotation + (Math.PI / 2) - this.skew._x); + this._sy = Math.sin(this.rotation + (Math.PI / 2) - this.skew._x); this._localID ++; } @@ -93,15 +91,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -126,15 +119,10 @@ if (this._localID !== this._currentLocalID) { // get the matrix values of the displayobject based on its transform properties.. - const a = this._cr * this.scale._x; - const b = this._sr * this.scale._x; - const c = -this._sr * this.scale._y; - const d = this._cr * this.scale._y; - - lt.a = (this._cy * a) + (this._sy * c); - lt.b = (this._cy * b) + (this._sy * d); - lt.c = (this._nsx * a) + (this._cx * c); - lt.d = (this._nsx * b) + (this._cx * d); + lt.a = this._cx * this.scale._x; + lt.b = this._sx * this.scale._x; + lt.c = this._cy * this.scale._y; + lt.d = this._sy * this.scale._y; lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c)); lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d)); @@ -191,8 +179,6 @@ set rotation(value) { this._rotation = value; - this._sr = Math.sin(value); - this._cr = Math.cos(value); - this._localID ++; + this.updateSkew(); } } diff --git a/test/core/TransformStatic.js b/test/core/TransformStatic.js new file mode 100644 index 0000000..7e79029 --- /dev/null +++ b/test/core/TransformStatic.js @@ -0,0 +1,74 @@ +'use strict'; + +describe('PIXI.TransformStatic', () => +{ + describe('setFromMatrix', () => + { + it('should decompose correct rotation', () => + { + const eps = 1e-3; + + const transform = new PIXI.TransformStatic(); + const parent = new PIXI.TransformStatic(); + const otherTransform = new PIXI.TransformStatic(); + + transform.position.set(20, 10); + transform.scale.set(2, -3); + transform.rotation = Math.PI / 4; + transform.updateTransform(parent); + + otherTransform.setFromMatrix(transform.worldTransform); + + const position = otherTransform.position; + const scale = otherTransform.scale; + const skew = otherTransform.skew; + + expect(position.x).to.be.closeTo(20, eps); + expect(position.y).to.be.closeTo(10, eps); + expect(scale.x).to.be.closeTo(2, eps); + expect(scale.y).to.be.closeTo(-3, eps); + expect(skew.x).to.be.equal(0); + expect(skew.y).to.be.equal(0); + expect(otherTransform.rotation).to.be.closeTo(Math.PI / 4, eps); + }); + + it('should apply skew before scale, like in CSS', () => + { + // this example looks the same in CSS and in pixi, made with pixi-animate by @bigtimebuddy + + const eps = 1e-3; + + const transform = new PIXI.TransformStatic(); + const parent = new PIXI.TransformStatic(); + const otherTransform = new PIXI.TransformStatic(); + + transform.position.set(387.8, 313.95); + transform.scale.set(0.572, 4.101); + transform.skew.set(-0.873, 0.175); + transform.updateTransform(parent); + + const mat = transform.worldTransform; + + expect(mat.a).to.be.closeTo(0.563, eps); + expect(mat.b).to.be.closeTo(0.010, eps); + expect(mat.c).to.be.closeTo(-3.142, eps); + expect(mat.d).to.be.closeTo(2.635, eps); + expect(mat.tx).to.be.closeTo(387.8, eps); + expect(mat.ty).to.be.closeTo(313.95, eps); + + otherTransform.setFromMatrix(transform.worldTransform); + + const position = otherTransform.position; + const scale = otherTransform.scale; + const skew = otherTransform.skew; + + expect(position.x).to.be.closeTo(387.8, eps); + expect(position.y).to.be.closeTo(313.95, eps); + expect(scale.x).to.be.closeTo(0.572, eps); + expect(scale.y).to.be.closeTo(4.101, eps); + expect(skew.x).to.be.equal(-0.873); + expect(skew.y).to.be.equal(0.175); + expect(otherTransform.rotation).to.be.equal(0); + }); + }); +}); diff --git a/test/core/index.js b/test/core/index.js index a060dde..f23ce06 100755 --- a/test/core/index.js +++ b/test/core/index.js @@ -1,5 +1,6 @@ 'use strict'; +require('./TransformStatic'); require('./Bounds'); require('./Container'); require('./DisplayObject');