Skip to content

Commit

Permalink
Fix Sprite.getLocalBounds corrupting bounds (pixijs#8087)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 authored Jan 26, 2022
1 parent 713ef61 commit c25beb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/sprite/src/Sprite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BLEND_MODES } from '@pixi/constants';
import { Texture } from '@pixi/core';
import { Container } from '@pixi/display';
import { Bounds, Container } from '@pixi/display';
import { ObservablePoint, Point, Rectangle } from '@pixi/math';
import { settings } from '@pixi/settings';
import { sign } from '@pixi/utils';
Expand Down Expand Up @@ -417,10 +417,15 @@ export class Sprite extends Container
// we can do a fast local bounds if the sprite has no children!
if (this.children.length === 0)
{
this._bounds.minX = this._texture.orig.width * -this._anchor._x;
this._bounds.minY = this._texture.orig.height * -this._anchor._y;
this._bounds.maxX = this._texture.orig.width * (1 - this._anchor._x);
this._bounds.maxY = this._texture.orig.height * (1 - this._anchor._y);
if (!this._localBounds)
{
this._localBounds = new Bounds();
}

this._localBounds.minX = this._texture.orig.width * -this._anchor._x;
this._localBounds.minY = this._texture.orig.height * -this._anchor._y;
this._localBounds.maxX = this._texture.orig.width * (1 - this._anchor._x);
this._localBounds.maxY = this._texture.orig.height * (1 - this._anchor._y);

if (!rect)
{
Expand All @@ -432,7 +437,7 @@ export class Sprite extends Container
rect = this._localBoundsRect;
}

return this._bounds.getRectangle(rect);
return this._localBounds.getRectangle(rect);
}

return super.getLocalBounds.call(this, rect);
Expand Down
30 changes: 30 additions & 0 deletions packages/sprite/test/Sprite.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ describe('Sprite', function ()
expect(bounds.width).to.equal(20);
expect(bounds.height).to.equal(30);
});

it('should not corrupt bounds', function ()
{
const texture = RenderTexture.create({ width: 20, height: 30 });
const sprite = new Sprite(texture);

sprite.scale.x = 2;
sprite.y = -5;

let bounds = sprite.getBounds(false);

expect(bounds.x).to.equal(0);
expect(bounds.y).to.equal(-5);
expect(bounds.width).to.equal(40);
expect(bounds.height).to.equal(30);

bounds = sprite.getLocalBounds();

expect(bounds.x).to.equal(0);
expect(bounds.y).to.equal(0);
expect(bounds.width).to.equal(20);
expect(bounds.height).to.equal(30);

bounds = sprite.getBounds(true);

expect(bounds.x).to.equal(0);
expect(bounds.y).to.equal(-5);
expect(bounds.width).to.equal(40);
expect(bounds.height).to.equal(30);
});
});

describe('containsPoint', function ()
Expand Down

0 comments on commit c25beb7

Please sign in to comment.