diff --git a/src/extras/getGlobalPosition.js b/src/extras/getGlobalPosition.js index b71682f..a23761f 100644 --- a/src/extras/getGlobalPosition.js +++ b/src/extras/getGlobalPosition.js @@ -1,20 +1,20 @@ import * as core from '../core'; /** - * Returns the global position of the displayObject + * Returns the global position of the displayObject. Does not depend on object scale, rotation and pivot. * * @memberof PIXI.DisplayObject# * @param {Point} point - the point to write the global value to. If null a new point will be returned + * @param {boolean} skipUpdate - 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 * @return {Point} The updated point */ -core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point()) +core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point(), skipUpdate = false) { if (this.parent) { - this.displayObjectUpdateTransform(); - - point.x = this.worldTransform.tx; - point.y = this.worldTransform.ty; + this.parent.toGlobal(this.position, point, skipUpdate); } else { diff --git a/src/extras/getGlobalPosition.js b/src/extras/getGlobalPosition.js index b71682f..a23761f 100644 --- a/src/extras/getGlobalPosition.js +++ b/src/extras/getGlobalPosition.js @@ -1,20 +1,20 @@ import * as core from '../core'; /** - * Returns the global position of the displayObject + * Returns the global position of the displayObject. Does not depend on object scale, rotation and pivot. * * @memberof PIXI.DisplayObject# * @param {Point} point - the point to write the global value to. If null a new point will be returned + * @param {boolean} skipUpdate - 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 * @return {Point} The updated point */ -core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point()) +core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point(), skipUpdate = false) { if (this.parent) { - this.displayObjectUpdateTransform(); - - point.x = this.worldTransform.tx; - point.y = this.worldTransform.ty; + this.parent.toGlobal(this.position, point, skipUpdate); } else { diff --git a/test/core/getGlobalPosition.js b/test/core/getGlobalPosition.js new file mode 100644 index 0000000..480bfbc --- /dev/null +++ b/test/core/getGlobalPosition.js @@ -0,0 +1,34 @@ +'use strict'; + +describe('getGlobalPosition', function () +{ + it('should return correct global coordinates of a displayObject, without depending on its pivot', function () + { + var parent = new PIXI.Container(); + + var container = new PIXI.Container(); + + parent.addChild(container); + + parent.position.set(100, 100); + parent.rotation = Math.PI; + parent.scale.set(2, 2); + container.position.set(10, -30); + container.pivot.set(1000, 1000); + + var globalPoint; + + globalPoint = container.toGlobalPosition(globalPoint, false); + + expect(globalPoint.x).to.equal(80); + expect(globalPoint.y).to.equal(160); + + // check but skipUpdate + + parent.position.set(200, 200); + globalPoint = container.toGlobalPosition(globalPoint, true); + + expect(globalPoint.x).to.equal(80); + expect(globalPoint.y).to.equal(160); + }); +}); diff --git a/src/extras/getGlobalPosition.js b/src/extras/getGlobalPosition.js index b71682f..a23761f 100644 --- a/src/extras/getGlobalPosition.js +++ b/src/extras/getGlobalPosition.js @@ -1,20 +1,20 @@ import * as core from '../core'; /** - * Returns the global position of the displayObject + * Returns the global position of the displayObject. Does not depend on object scale, rotation and pivot. * * @memberof PIXI.DisplayObject# * @param {Point} point - the point to write the global value to. If null a new point will be returned + * @param {boolean} skipUpdate - 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 * @return {Point} The updated point */ -core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point()) +core.DisplayObject.prototype.getGlobalPosition = function getGlobalPosition(point = new core.Point(), skipUpdate = false) { if (this.parent) { - this.displayObjectUpdateTransform(); - - point.x = this.worldTransform.tx; - point.y = this.worldTransform.ty; + this.parent.toGlobal(this.position, point, skipUpdate); } else { diff --git a/test/core/getGlobalPosition.js b/test/core/getGlobalPosition.js new file mode 100644 index 0000000..480bfbc --- /dev/null +++ b/test/core/getGlobalPosition.js @@ -0,0 +1,34 @@ +'use strict'; + +describe('getGlobalPosition', function () +{ + it('should return correct global coordinates of a displayObject, without depending on its pivot', function () + { + var parent = new PIXI.Container(); + + var container = new PIXI.Container(); + + parent.addChild(container); + + parent.position.set(100, 100); + parent.rotation = Math.PI; + parent.scale.set(2, 2); + container.position.set(10, -30); + container.pivot.set(1000, 1000); + + var globalPoint; + + globalPoint = container.toGlobalPosition(globalPoint, false); + + expect(globalPoint.x).to.equal(80); + expect(globalPoint.y).to.equal(160); + + // check but skipUpdate + + parent.position.set(200, 200); + globalPoint = container.toGlobalPosition(globalPoint, true); + + expect(globalPoint.x).to.equal(80); + expect(globalPoint.y).to.equal(160); + }); +}); diff --git a/test/core/index.js b/test/core/index.js index 899c633..0393e14 100755 --- a/test/core/index.js +++ b/test/core/index.js @@ -8,6 +8,7 @@ require('./Text'); require('./toGlobal'); require('./toLocal'); +require('./getGlobalPosition'); require('./util'); require('./Point'); require('./ObservablePoint');