Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix setting matrix/scale

  • Loading branch information...
commit 9ab2f5e9ef3b6928f66c68ceff4ae7ede1956eae 1 parent 919cf22
@davidaurelio davidaurelio authored
Showing with 28 additions and 3 deletions.
  1. +16 −3 src/runner/display_object.js
  2. +12 −0 test/display_object-spec.js
View
19 src/runner/display_object.js
@@ -20,7 +20,7 @@ define([
*/
var uid = 1;
- var atan2 = Math.atan2, PI = Math.PI;
+ var atan2 = Math.atan2, sqrt = Math.sqrt, PI = Math.PI;
var isfinite = isFinite; // local reference for faster lookup
/**
@@ -87,13 +87,26 @@ define([
}
function setMatrix(matrix) {
+ /*
+ The internally stored matrix is 'unscaled', and scale is
+ stored seperately. This approach allows for non-destructive scaling to 0.
+
+ When setting the matrix, we need to convert it to the unscaled form, i.e.
+ extracting the scale, storing it into the respective internal variables,
+ and apply the reverse scale to the matrix.
+ */
+
+ var scaleX = this._scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
+ var scaleY = this._scaleY = sqrt(matrix.d * matrix.d + matrix.c * matrix.c);
+
var m = this._matrix;
- m.a = this._scaleX = matrix.a;
+ m.a = matrix.a;
m.b = matrix.b;
m.c = matrix.c;
- m.d = this._scaleY = matrix.d;
+ m.d = matrix.d;
m.tx = matrix.tx;
m.ty = matrix.ty;
+ m.scale( 1/scaleX || 1, 1/scaleY || 1); // avoid scaling by NaN
}
function getX() {
View
12 test/display_object-spec.js
@@ -157,6 +157,18 @@ define([
expect(d.attr('scale')).toBe((2 + 3) / 2);
});
+ it('should return the an equivalent matrix when setting the matrix attribute', function() {
+ var scaleX = 2;
+ var scaleY = 3;
+ var d = new DisplayObject(), m = new Matrix(scaleX, 0, 0, scaleY);
+
+ // scale up, then set the matrix
+ d.attr('scaleX', 1.5);
+ d.attr('scaleY', 2);
+ d.attr('matrix', m);
+
+ expect(d.attr('matrix')).toEqual(new Matrix(scaleX, 0, 0, scaleY));
+ });
});
it('should mark the object for update when the matrix is updated', function() {

0 comments on commit 9ab2f5e

Please sign in to comment.
Something went wrong with that request. Please try again.