diff --git a/src/core/display/Bounds.js b/src/core/display/Bounds.js index 0fc6b6d..3f40416 100644 --- a/src/core/display/Bounds.js +++ b/src/core/display/Bounds.js @@ -62,23 +62,20 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -Bounds.prototype.getRectangle = function() +Bounds.prototype.getRectangle = function(rect) { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - if(!this.rect) - { - this.rect = new Rectangle(0, 0, 1, 1); - } + rect = rect || new Rectangle(0, 0, 1, 1); - this.rect.x = this.minX; - this.rect.y = this.minY; - this.rect.width = this.maxX - this.minX; - this.rect.height = this.maxY - this.minY; + rect.x = this.minX; + rect.y = this.minY; + rect.width = this.maxX - this.minX; + rect.height = this.maxY - this.minY; - return this.rect; + return rect; }; /** diff --git a/src/core/display/Bounds.js b/src/core/display/Bounds.js index 0fc6b6d..3f40416 100644 --- a/src/core/display/Bounds.js +++ b/src/core/display/Bounds.js @@ -62,23 +62,20 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -Bounds.prototype.getRectangle = function() +Bounds.prototype.getRectangle = function(rect) { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - if(!this.rect) - { - this.rect = new Rectangle(0, 0, 1, 1); - } + rect = rect || new Rectangle(0, 0, 1, 1); - this.rect.x = this.minX; - this.rect.y = this.minY; - this.rect.width = this.maxX - this.minX; - this.rect.height = this.maxY - this.minY; + rect.x = this.minX; + rect.y = this.minY; + rect.width = this.maxX - this.minX; + rect.height = this.maxY - this.minY; - return this.rect; + return rect; }; /** diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index a0b41c2..34f5e56 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -3,6 +3,7 @@ TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), Bounds = require('./Bounds'), + math = require('../math'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -87,6 +88,8 @@ this._bounds = new Bounds(); this._boundsID = 0; this._lastBoundsID = -1; + this._boundsRect = null; + this._localBoundsRect = null; /** * The original, cached mask of the object @@ -366,11 +369,12 @@ /** * * - * Retrieves the bounds of the displayObject as a rectangle object - * + * Retrieves the bounds of the displayObject as a rectangle object. + * @param skipUpdate {boolean} setting to true will stop the transforms of the scene graph from being updated. This means the calculation returned MAY be out of date BUT will give you a nice performance boost + * @param rect {PIXI.Rectangle} Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ -DisplayObject.prototype.getBounds = function (skipUpdate) +DisplayObject.prototype.getBounds = function (skipUpdate, rect) { if(!skipUpdate) { @@ -392,15 +396,25 @@ this.calculateBounds(); } - return this._bounds.getRectangle(this._bounds); + if(!rect) + { + if(!this._boundsRect) + { + this._boundsRect = new math.Rectangle(); + } + + rect = this._boundsRect; + } + + return this._bounds.getRectangle(rect); }; /** * Retrieves the local bounds of the displayObject as a rectangle object - * + * @param rect {PIXI.Rectangle} Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ -DisplayObject.prototype.getLocalBounds = function () +DisplayObject.prototype.getLocalBounds = function (rect) { var transformRef = this.transform; var parentRef = this.parent; @@ -408,7 +422,17 @@ this.parent = null; this.transform = _tempDisplayObjectParent.transform; - var bounds = this.getBounds(); + if(!rect) + { + if(!this._localBoundsRect) + { + this._localBoundsRect = new math.Rectangle(); + } + + rect = this._localBoundsRect; + } + + var bounds = this.getBounds(rect); this.parent = parentRef; this.transform = transformRef; diff --git a/src/core/display/Bounds.js b/src/core/display/Bounds.js index 0fc6b6d..3f40416 100644 --- a/src/core/display/Bounds.js +++ b/src/core/display/Bounds.js @@ -62,23 +62,20 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -Bounds.prototype.getRectangle = function() +Bounds.prototype.getRectangle = function(rect) { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - if(!this.rect) - { - this.rect = new Rectangle(0, 0, 1, 1); - } + rect = rect || new Rectangle(0, 0, 1, 1); - this.rect.x = this.minX; - this.rect.y = this.minY; - this.rect.width = this.maxX - this.minX; - this.rect.height = this.maxY - this.minY; + rect.x = this.minX; + rect.y = this.minY; + rect.width = this.maxX - this.minX; + rect.height = this.maxY - this.minY; - return this.rect; + return rect; }; /** diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index a0b41c2..34f5e56 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -3,6 +3,7 @@ TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), Bounds = require('./Bounds'), + math = require('../math'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -87,6 +88,8 @@ this._bounds = new Bounds(); this._boundsID = 0; this._lastBoundsID = -1; + this._boundsRect = null; + this._localBoundsRect = null; /** * The original, cached mask of the object @@ -366,11 +369,12 @@ /** * * - * Retrieves the bounds of the displayObject as a rectangle object - * + * Retrieves the bounds of the displayObject as a rectangle object. + * @param skipUpdate {boolean} setting to true will stop the transforms of the scene graph from being updated. This means the calculation returned MAY be out of date BUT will give you a nice performance boost + * @param rect {PIXI.Rectangle} Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ -DisplayObject.prototype.getBounds = function (skipUpdate) +DisplayObject.prototype.getBounds = function (skipUpdate, rect) { if(!skipUpdate) { @@ -392,15 +396,25 @@ this.calculateBounds(); } - return this._bounds.getRectangle(this._bounds); + if(!rect) + { + if(!this._boundsRect) + { + this._boundsRect = new math.Rectangle(); + } + + rect = this._boundsRect; + } + + return this._bounds.getRectangle(rect); }; /** * Retrieves the local bounds of the displayObject as a rectangle object - * + * @param rect {PIXI.Rectangle} Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ -DisplayObject.prototype.getLocalBounds = function () +DisplayObject.prototype.getLocalBounds = function (rect) { var transformRef = this.transform; var parentRef = this.parent; @@ -408,7 +422,17 @@ this.parent = null; this.transform = _tempDisplayObjectParent.transform; - var bounds = this.getBounds(); + if(!rect) + { + if(!this._localBoundsRect) + { + this._localBoundsRect = new math.Rectangle(); + } + + rect = this._localBoundsRect; + } + + var bounds = this.getBounds(rect); this.parent = parentRef; this.transform = transformRef; diff --git a/test/unit/core/Bounds.test.js b/test/unit/core/Bounds.test.js index 886b75b..d3aa989 100644 --- a/test/unit/core/Bounds.test.js +++ b/test/unit/core/Bounds.test.js @@ -323,5 +323,40 @@ }); + it('should return a different rectangle if getting local bounds after global bounds ', function() { + + var parent = new PIXI.Container(); + var texture = PIXI.RenderTexture.create(10, 10); + + var sprite = new PIXI.Sprite(texture); + + sprite.position.x = 20; + sprite.position.y = 20; + + sprite.scale.x = 2; + sprite.scale.y = 2; + + parent.addChild(sprite); + + + var bounds = sprite.getBounds(); + + expect(bounds.x).to.equal(20); + expect(bounds.y).to.equal(20); + expect(bounds.width).to.equal(20); + expect(bounds.height).to.equal(20); + + + var localBounds = sprite.getLocalBounds(); + + expect(bounds.x).to.equal(20); + expect(bounds.y).to.equal(20); + expect(bounds.width).to.equal(20); + expect(bounds.height).to.equal(20); + + + + }); + });