Skip to content

Commit

Permalink
Merge branch 'transforms-to-be-set-by-matrix'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Aurelio committed Dec 3, 2012
2 parents c26e437 + feadeab commit 019908c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
v0.4.2
-------------------

* Add support for an `interactive` attribute on all DisplayObjects which allows
pointer-events to be received (true) or to pass through (false)
* Setting an attribute to the actual value won't trigger a render message.
Expand Down
24 changes: 23 additions & 1 deletion src/runner/display_object.js
Expand Up @@ -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

/**
Expand Down Expand Up @@ -87,13 +87,35 @@ 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 = matrix.a;
m.b = matrix.b;
m.c = matrix.c;
m.d = matrix.d;
m.tx = matrix.tx;
m.ty = matrix.ty;

if (scaleX !== 1 || scaleY !== 1) {
// Make sure we rotate around the chosen origin
var origin = m.transformPoint(this._origin);
m.tx -= origin.x;
m.ty -= origin.y;
m.scale( 1/scaleX || 1, 1/scaleY || 1); // avoid scaling by NaN
m.tx += origin.x;
m.ty += origin.y;
}
}

function getX() {
Expand Down
54 changes: 54 additions & 0 deletions test/display_object-spec.js
Expand Up @@ -124,6 +124,60 @@ define([
expect(m.ty).toBe(-100);
});

it('should set the scale properties when a matrix is updated', function () {
var d, m;
d = new DisplayObject();
m = new Matrix(2, 0, 0, 2, 0, 0);
d.attr('matrix', m);
expect(d.attr('scaleX')).toBe(2);
expect(d.attr('scaleY')).toBe(2);
expect(d.attr('scale')).toBe(2);

// reset
d.attr('scaleX', 1);
d.attr('scaleY', 1);
expect(d.attr('scaleX')).toBe(1);
expect(d.attr('scaleY')).toBe(1);
expect(d.attr('scale')).toBe(1);
});

it('should allow the matrix to be updated after setting a scale', function () {
var d, m;
d = new DisplayObject();
m = new Matrix(2, 0, 0, 3, 0, 0);

// scale up, then set the matrix
d.attr('scaleX', 1.5);
d.attr('scaleY', 2);
d.attr('matrix', m);

expect(d.attr('scaleX')).toBe(2);
expect(d.attr('scaleY')).toBe(3);

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 tx = 123, ty = 654;
var d = new DisplayObject(), m = new Matrix(scaleX, 0, 0, scaleY, tx, ty);

// 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, tx, ty));
});
});

it('should mark the object for update when the matrix is updated', function() {
var d = new DisplayObject();
d.stage = {registry: {needsDraw: {}}};

d.attr('matrix', new Matrix());
expect(d.stage.registry.needsDraw).toHaveOwnProperties(d.id);
});

it('should use the origin attribute for rotation', function() {
Expand Down

0 comments on commit 019908c

Please sign in to comment.