diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d60ef70..182530e 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -333,10 +333,10 @@ // 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; - this._bounds.maxY = this._texture.orig.height; + 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._x); if (!rect) { diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d60ef70..182530e 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -333,10 +333,10 @@ // 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; - this._bounds.maxY = this._texture.orig.height; + 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._x); if (!rect) { diff --git a/src/extras/TilingSprite.js b/src/extras/TilingSprite.js index 2f3643b..80902a3 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -248,10 +248,10 @@ */ _calculateBounds() { - const minX = -this._width * this.anchor._x; - const minY = -this._height * this.anchor._y; - const maxX = this._width; - const maxY = this._height; + const minX = this._width * -this._anchor._x; + const minY = this._height * -this._anchor._y; + const maxX = this._width * (1 - this._anchor._x); + const maxY = this._height * (1 - this._anchor._y); this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); } @@ -267,10 +267,10 @@ // we can do a fast local bounds if the sprite has no children! if (this.children.length === 0) { - this._bounds.minX = -this._width * this.anchor._x; - this._bounds.minY = -this._height * this.anchor._y; - this._bounds.maxX = this._width; - this._bounds.maxY = this._height; + this._bounds.minX = this._width * -this._anchor._x; + this._bounds.minY = this._height * -this._anchor._y; + this._bounds.maxX = this._width * (1 - this._anchor._x); + this._bounds.maxY = this._height * (1 - this._anchor._x); if (!rect) { @@ -300,11 +300,11 @@ const width = this._width; const height = this._height; - const x1 = -width * this.anchor.x; + const x1 = -width * this.anchor._x; if (tempPoint.x > x1 && tempPoint.x < x1 + width) { - const y1 = -height * this.anchor.y; + const y1 = -height * this.anchor._y; if (tempPoint.y > y1 && tempPoint.y < y1 + height) { diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d60ef70..182530e 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -333,10 +333,10 @@ // 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; - this._bounds.maxY = this._texture.orig.height; + 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._x); if (!rect) { diff --git a/src/extras/TilingSprite.js b/src/extras/TilingSprite.js index 2f3643b..80902a3 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -248,10 +248,10 @@ */ _calculateBounds() { - const minX = -this._width * this.anchor._x; - const minY = -this._height * this.anchor._y; - const maxX = this._width; - const maxY = this._height; + const minX = this._width * -this._anchor._x; + const minY = this._height * -this._anchor._y; + const maxX = this._width * (1 - this._anchor._x); + const maxY = this._height * (1 - this._anchor._y); this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); } @@ -267,10 +267,10 @@ // we can do a fast local bounds if the sprite has no children! if (this.children.length === 0) { - this._bounds.minX = -this._width * this.anchor._x; - this._bounds.minY = -this._height * this.anchor._y; - this._bounds.maxX = this._width; - this._bounds.maxY = this._height; + this._bounds.minX = this._width * -this._anchor._x; + this._bounds.minY = this._height * -this._anchor._y; + this._bounds.maxX = this._width * (1 - this._anchor._x); + this._bounds.maxY = this._height * (1 - this._anchor._x); if (!rect) { @@ -300,11 +300,11 @@ const width = this._width; const height = this._height; - const x1 = -width * this.anchor.x; + const x1 = -width * this.anchor._x; if (tempPoint.x > x1 && tempPoint.x < x1 + width) { - const y1 = -height * this.anchor.y; + const y1 = -height * this.anchor._y; if (tempPoint.y > y1 && tempPoint.y < y1 + height) { diff --git a/test/core/Sprite.js b/test/core/Sprite.js index 3eb2f56..4553ddf 100755 --- a/test/core/Sprite.js +++ b/test/core/Sprite.js @@ -61,4 +61,30 @@ expect(sprite.scale.y).to.be.below(0); }); }); + + describe('getBounds()', function () + { + it('must have correct value according to texture size, width, height and anchor', function () + { + const parent = new PIXI.Container(); + const texture = new PIXI.RenderTexture.create(20, 30); + const sprite = new PIXI.Sprite(texture); + sprite.width = 200; + sprite.height = 300; + + parent.addChild(sprite); + + sprite.scale.x *= 2; + sprite.scale.y *= 2; + sprite.anchor.set(0.5, 0.5); + sprite.position.set(50, 40); + + const bounds = sprite.getBounds(); + + expect(bounds.x).to.equal(-150); + expect(bounds.y).to.equal(-260); + expect(bounds.width).to.equal(400); + expect(bounds.height).to.equal(600); + }); + }); }); diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d60ef70..182530e 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -333,10 +333,10 @@ // 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; - this._bounds.maxY = this._texture.orig.height; + 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._x); if (!rect) { diff --git a/src/extras/TilingSprite.js b/src/extras/TilingSprite.js index 2f3643b..80902a3 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -248,10 +248,10 @@ */ _calculateBounds() { - const minX = -this._width * this.anchor._x; - const minY = -this._height * this.anchor._y; - const maxX = this._width; - const maxY = this._height; + const minX = this._width * -this._anchor._x; + const minY = this._height * -this._anchor._y; + const maxX = this._width * (1 - this._anchor._x); + const maxY = this._height * (1 - this._anchor._y); this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); } @@ -267,10 +267,10 @@ // we can do a fast local bounds if the sprite has no children! if (this.children.length === 0) { - this._bounds.minX = -this._width * this.anchor._x; - this._bounds.minY = -this._height * this.anchor._y; - this._bounds.maxX = this._width; - this._bounds.maxY = this._height; + this._bounds.minX = this._width * -this._anchor._x; + this._bounds.minY = this._height * -this._anchor._y; + this._bounds.maxX = this._width * (1 - this._anchor._x); + this._bounds.maxY = this._height * (1 - this._anchor._x); if (!rect) { @@ -300,11 +300,11 @@ const width = this._width; const height = this._height; - const x1 = -width * this.anchor.x; + const x1 = -width * this.anchor._x; if (tempPoint.x > x1 && tempPoint.x < x1 + width) { - const y1 = -height * this.anchor.y; + const y1 = -height * this.anchor._y; if (tempPoint.y > y1 && tempPoint.y < y1 + height) { diff --git a/test/core/Sprite.js b/test/core/Sprite.js index 3eb2f56..4553ddf 100755 --- a/test/core/Sprite.js +++ b/test/core/Sprite.js @@ -61,4 +61,30 @@ expect(sprite.scale.y).to.be.below(0); }); }); + + describe('getBounds()', function () + { + it('must have correct value according to texture size, width, height and anchor', function () + { + const parent = new PIXI.Container(); + const texture = new PIXI.RenderTexture.create(20, 30); + const sprite = new PIXI.Sprite(texture); + sprite.width = 200; + sprite.height = 300; + + parent.addChild(sprite); + + sprite.scale.x *= 2; + sprite.scale.y *= 2; + sprite.anchor.set(0.5, 0.5); + sprite.position.set(50, 40); + + const bounds = sprite.getBounds(); + + expect(bounds.x).to.equal(-150); + expect(bounds.y).to.equal(-260); + expect(bounds.width).to.equal(400); + expect(bounds.height).to.equal(600); + }); + }); }); diff --git a/test/core/TilingSprite.js b/test/core/TilingSprite.js index d128b79..1d3fa13 100644 --- a/test/core/TilingSprite.js +++ b/test/core/TilingSprite.js @@ -4,7 +4,7 @@ { describe('getBounds()', function () { - it('must have correct value according to _width and _height', function () + it('must have correct value according to _width, _height and anchor', function () { const parent = new PIXI.Container(); const texture = new PIXI.Texture(new PIXI.BaseTexture()); @@ -13,7 +13,7 @@ parent.addChild(tilingSprite); tilingSprite.anchor.set(0.5, 0.5); - tilingSprite.scale.set(-2, -2); + tilingSprite.scale.set(-2, 2); tilingSprite.position.set(50, 40); const bounds = tilingSprite.getBounds();