Skip to content

Commit c56bc76

Browse files
committed
Overrided getLocalBounds() for PIXI Sprite, Strip and Graphics
Since they override the getBounds of the DisplayObjectContainer, the getLocalBounds should also be overriden for them to properly work.
1 parent d18f303 commit c56bc76

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/pixi/display/Sprite.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,32 @@ PIXI.Sprite.prototype.getBounds = function(matrix)
316316
return bounds;
317317
};
318318

319+
/**
320+
* Retrieves the non-global local bounds of the Sprite as a rectangle. The calculation takes all visible children into consideration.
321+
*
322+
* @method getLocalBounds
323+
* @return {Rectangle} The rectangular bounding area
324+
*/
325+
PIXI.Sprite.prototype.getLocalBounds = function () {
326+
var matrixCache = this.worldTransform;
327+
328+
this.worldTransform = PIXI.identityMatrix;
329+
330+
for (var i = 0; i < this.children.length; i++) {
331+
this.children[i].updateTransform();
332+
}
333+
334+
var bounds = this.getBounds();
335+
336+
this.worldTransform = matrixCache;
337+
338+
for (i = 0; i < this.children.length; i++) {
339+
this.children[i].updateTransform();
340+
}
341+
342+
return bounds;
343+
};
344+
319345
/**
320346
* Renders the object using the WebGL renderer
321347
*

src/pixi/extras/Strip.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,32 @@ PIXI.Strip.prototype.getBounds = function(matrix)
459459
return bounds;
460460
};
461461

462+
/**
463+
* Retrieves the non-global local bounds of the mesh as a rectangle. The calculation takes all visible children into consideration.
464+
*
465+
* @method getLocalBounds
466+
* @return {Rectangle} The rectangular bounding area
467+
*/
468+
PIXI.Strip.prototype.getLocalBounds = function () {
469+
var matrixCache = this.worldTransform;
470+
471+
this.worldTransform = PIXI.identityMatrix;
472+
473+
for (var i = 0; i < this.children.length; i++) {
474+
this.children[i].updateTransform();
475+
}
476+
477+
var bounds = this.getBounds();
478+
479+
this.worldTransform = matrixCache;
480+
481+
for (i = 0; i < this.children.length; i++) {
482+
this.children[i].updateTransform();
483+
}
484+
485+
return bounds;
486+
};
487+
462488
/**
463489
* Different drawing buffer modes supported
464490
*

src/pixi/primitives/Graphics.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,32 @@ PIXI.Graphics.prototype.getBounds = function(matrix)
922922

923923
};
924924

925+
/**
926+
* Retrieves the non-global local bounds of the graphic shape as a rectangle. The calculation takes all visible children into consideration.
927+
*
928+
* @method getLocalBounds
929+
* @return {Rectangle} The rectangular bounding area
930+
*/
931+
PIXI.Graphics.prototype.getLocalBounds = function () {
932+
var matrixCache = this.worldTransform;
933+
934+
this.worldTransform = PIXI.identityMatrix;
935+
936+
for (var i = 0; i < this.children.length; i++) {
937+
this.children[i].updateTransform();
938+
}
939+
940+
var bounds = this.getBounds();
941+
942+
this.worldTransform = matrixCache;
943+
944+
for (i = 0; i < this.children.length; i++) {
945+
this.children[i].updateTransform();
946+
}
947+
948+
return bounds;
949+
};
950+
925951
/**
926952
* Tests if a point is inside this graphics object
927953
*

0 commit comments

Comments
 (0)