Skip to content

Commit

Permalink
Implement getAbsoluteBoundingBox and getAbsoluteMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
padolsey committed Oct 30, 2012
1 parent 1093cb2 commit c0e67b6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v0.4.2
-------------------

* Added support for getAbsoluteBoundingBox and getAbsoluteMatrix on
DisplayObject

v0.4.1 / 2012-10-26
-------------------
Expand Down
25 changes: 25 additions & 0 deletions src/runner/display_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,37 @@ define([
return this;
},

/**
* Gets the matrix of the DisplayObject combined with all ancestor matrices
*
* @returns {Matrix} The resulting matrix
*/
getAbsoluteMatrix: function() {
var matrix = this.attr('matrix').clone();
var parent = this;
while ((parent = parent.parent) && parent.id !== 0) {
matrix.concat(parent.attr('matrix'));
}
return matrix;
},

/**
* Computed the absolute bounding box relative to the top-most ancestor
*
* @returns {Object} an object with all box properties
* (left, top, right, bottom, width, height)
*/
getAbsoluteBoundingBox: function() {
return this.getBoundingBox( this.getAbsoluteMatrix() );
},

/**
* Computes bounding boxes and single data points of a display object.
*
* @param {Matrix} [transform=null] A transform to apply to all points
* before computation.
* @returns {Object} an object with all box properties
* (left, top, right, bottom, width, height)
*/
getBoundingBox: function(transform) {
var x = 0, y = 0;
Expand Down
41 changes: 40 additions & 1 deletion test/display_object-spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
define([
'bonsai/runner/display_object',
'bonsai/runner/group',
'bonsai/event_emitter',
'bonsai/runner/matrix',
'bonsai/tools',
'common/mock',
'common/displayobject-lifecycle'
], function(DisplayObject, EventEmitter, Matrix, tools, mock, testLifeCycle) {
], function(DisplayObject, Group, EventEmitter, Matrix, tools, mock, testLifeCycle) {
describe('DisplayObject', function() {
testLifeCycle(function() {
return new DisplayObject();
Expand Down Expand Up @@ -209,6 +210,44 @@ define([
});
});

describe('getAbsoluteBoundingBox', function() {
it('Gets absolute bbox relative to top-most ancestor', function() {
var a = new Group().attr('scaleX', 2);
var b = new Group().attr('scaleY', 1.5).attr('scaleX', 2).attr('x', 20);
var obj = new DisplayObject();
b.addTo(a);
obj.addTo(b);
obj.attr({
x: 100,
y: 100
});
expect(obj.getAbsoluteBoundingBox()).toEqual({
top: 150,
left: 440,
bottom: 150,
right: 440,
width: 0,
height: 0
});
});
});

describe('getAbsoluteMatrix', function() {
it('Gets the matrix of the DisplayObject concat\'d with all ancestors\' matrices', function() {
var a = new Group().attr('scaleX', 2);
var b = new Group().attr('scaleY', 1.5).attr('scaleX', 2).attr('x', 20);
var obj = new DisplayObject();
b.addTo(a);
obj.addTo(b);
obj.attr({
x: 100,
y: 100
});
var m = obj.getAbsoluteMatrix();
expect(m).toEqual(new Matrix(4, 0, 0, 1.5, 440, 150));
});
});

describe('with transform parameter', function() {
var outerTransform = new Matrix(0.313, 1.527, -0.429, 0.240, 145.25, 234.75);
var zeroTransformed = outerTransform.transformPoint({x: 0, y: 0});
Expand Down

0 comments on commit c0e67b6

Please sign in to comment.