diff --git a/src/extras/TilingSprite.js b/src/extras/TilingSprite.js index db220b6..2f3643b 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -242,6 +242,21 @@ } /** + * Updates the bounds of the tiling sprite. + * + * @private + */ + _calculateBounds() + { + const minX = -this._width * this.anchor._x; + const minY = -this._height * this.anchor._y; + const maxX = this._width; + const maxY = this._height; + + this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); + } + + /** * Gets the local bounds of the sprite object. * * @param {PIXI.Rectangle} rect - The output rectangle. @@ -274,82 +289,6 @@ } /** - * Returns the framing rectangle of the sprite as a Rectangle object - * - * @return {PIXI.Rectangle} the framing rectangle - */ - getBounds() - { - const width = this._width; - const height = this._height; - - const w0 = width * (1 - this.anchor.x); - const w1 = width * -this.anchor.x; - - const h0 = height * (1 - this.anchor.y); - const h1 = height * -this.anchor.y; - - const worldTransform = this.worldTransform; - - const a = worldTransform.a; - const b = worldTransform.b; - const c = worldTransform.c; - const d = worldTransform.d; - const tx = worldTransform.tx; - const ty = worldTransform.ty; - - const x1 = (a * w1) + (c * h1) + tx; - const y1 = (d * h1) + (b * w1) + ty; - - const x2 = (a * w0) + (c * h1) + tx; - const y2 = (d * h1) + (b * w0) + ty; - - const x3 = (a * w0) + (c * h0) + tx; - const y3 = (d * h0) + (b * w0) + ty; - - const x4 = (a * w1) + (c * h0) + tx; - const y4 = (d * h0) + (b * w1) + ty; - - let minX = 0; - let maxX = 0; - let minY = 0; - let maxY = 0; - - minX = x1; - minX = x2 < minX ? x2 : minX; - minX = x3 < minX ? x3 : minX; - minX = x4 < minX ? x4 : minX; - - minY = y1; - minY = y2 < minY ? y2 : minY; - minY = y3 < minY ? y3 : minY; - minY = y4 < minY ? y4 : minY; - - maxX = x1; - maxX = x2 > maxX ? x2 : maxX; - maxX = x3 > maxX ? x3 : maxX; - maxX = x4 > maxX ? x4 : maxX; - - maxY = y1; - maxY = y2 > maxY ? y2 : maxY; - maxY = y3 > maxY ? y3 : maxY; - maxY = y4 > maxY ? y4 : maxY; - - const bounds = this._bounds; - - bounds.x = minX; - bounds.width = maxX - minX; - - bounds.y = minY; - bounds.height = maxY - minY; - - // store a reference so that if this function gets called again in the render cycle we do not have to recalculate - this._currentBounds = bounds; - - return bounds; - } - - /** * Checks if a point is inside this tiling sprite. * * @param {PIXI.Point} point - the point to check diff --git a/src/extras/TilingSprite.js b/src/extras/TilingSprite.js index db220b6..2f3643b 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -242,6 +242,21 @@ } /** + * Updates the bounds of the tiling sprite. + * + * @private + */ + _calculateBounds() + { + const minX = -this._width * this.anchor._x; + const minY = -this._height * this.anchor._y; + const maxX = this._width; + const maxY = this._height; + + this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); + } + + /** * Gets the local bounds of the sprite object. * * @param {PIXI.Rectangle} rect - The output rectangle. @@ -274,82 +289,6 @@ } /** - * Returns the framing rectangle of the sprite as a Rectangle object - * - * @return {PIXI.Rectangle} the framing rectangle - */ - getBounds() - { - const width = this._width; - const height = this._height; - - const w0 = width * (1 - this.anchor.x); - const w1 = width * -this.anchor.x; - - const h0 = height * (1 - this.anchor.y); - const h1 = height * -this.anchor.y; - - const worldTransform = this.worldTransform; - - const a = worldTransform.a; - const b = worldTransform.b; - const c = worldTransform.c; - const d = worldTransform.d; - const tx = worldTransform.tx; - const ty = worldTransform.ty; - - const x1 = (a * w1) + (c * h1) + tx; - const y1 = (d * h1) + (b * w1) + ty; - - const x2 = (a * w0) + (c * h1) + tx; - const y2 = (d * h1) + (b * w0) + ty; - - const x3 = (a * w0) + (c * h0) + tx; - const y3 = (d * h0) + (b * w0) + ty; - - const x4 = (a * w1) + (c * h0) + tx; - const y4 = (d * h0) + (b * w1) + ty; - - let minX = 0; - let maxX = 0; - let minY = 0; - let maxY = 0; - - minX = x1; - minX = x2 < minX ? x2 : minX; - minX = x3 < minX ? x3 : minX; - minX = x4 < minX ? x4 : minX; - - minY = y1; - minY = y2 < minY ? y2 : minY; - minY = y3 < minY ? y3 : minY; - minY = y4 < minY ? y4 : minY; - - maxX = x1; - maxX = x2 > maxX ? x2 : maxX; - maxX = x3 > maxX ? x3 : maxX; - maxX = x4 > maxX ? x4 : maxX; - - maxY = y1; - maxY = y2 > maxY ? y2 : maxY; - maxY = y3 > maxY ? y3 : maxY; - maxY = y4 > maxY ? y4 : maxY; - - const bounds = this._bounds; - - bounds.x = minX; - bounds.width = maxX - minX; - - bounds.y = minY; - bounds.height = maxY - minY; - - // store a reference so that if this function gets called again in the render cycle we do not have to recalculate - this._currentBounds = bounds; - - return bounds; - } - - /** * Checks if a point is inside this tiling sprite. * * @param {PIXI.Point} point - the point to check diff --git a/test/core/TilingSprite.js b/test/core/TilingSprite.js new file mode 100644 index 0000000..d128b79 --- /dev/null +++ b/test/core/TilingSprite.js @@ -0,0 +1,27 @@ +'use strict'; + +describe('PIXI.Sprite', function () +{ + describe('getBounds()', function () + { + it('must have correct value according to _width and _height', function () + { + const parent = new PIXI.Container(); + const texture = new PIXI.Texture(new PIXI.BaseTexture()); + const tilingSprite = new PIXI.TilingSprite(texture, 200, 300); + + parent.addChild(tilingSprite); + + tilingSprite.anchor.set(0.5, 0.5); + tilingSprite.scale.set(-2, -2); + tilingSprite.position.set(50, 40); + + const bounds = tilingSprite.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/extras/TilingSprite.js b/src/extras/TilingSprite.js index db220b6..2f3643b 100644 --- a/src/extras/TilingSprite.js +++ b/src/extras/TilingSprite.js @@ -242,6 +242,21 @@ } /** + * Updates the bounds of the tiling sprite. + * + * @private + */ + _calculateBounds() + { + const minX = -this._width * this.anchor._x; + const minY = -this._height * this.anchor._y; + const maxX = this._width; + const maxY = this._height; + + this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); + } + + /** * Gets the local bounds of the sprite object. * * @param {PIXI.Rectangle} rect - The output rectangle. @@ -274,82 +289,6 @@ } /** - * Returns the framing rectangle of the sprite as a Rectangle object - * - * @return {PIXI.Rectangle} the framing rectangle - */ - getBounds() - { - const width = this._width; - const height = this._height; - - const w0 = width * (1 - this.anchor.x); - const w1 = width * -this.anchor.x; - - const h0 = height * (1 - this.anchor.y); - const h1 = height * -this.anchor.y; - - const worldTransform = this.worldTransform; - - const a = worldTransform.a; - const b = worldTransform.b; - const c = worldTransform.c; - const d = worldTransform.d; - const tx = worldTransform.tx; - const ty = worldTransform.ty; - - const x1 = (a * w1) + (c * h1) + tx; - const y1 = (d * h1) + (b * w1) + ty; - - const x2 = (a * w0) + (c * h1) + tx; - const y2 = (d * h1) + (b * w0) + ty; - - const x3 = (a * w0) + (c * h0) + tx; - const y3 = (d * h0) + (b * w0) + ty; - - const x4 = (a * w1) + (c * h0) + tx; - const y4 = (d * h0) + (b * w1) + ty; - - let minX = 0; - let maxX = 0; - let minY = 0; - let maxY = 0; - - minX = x1; - minX = x2 < minX ? x2 : minX; - minX = x3 < minX ? x3 : minX; - minX = x4 < minX ? x4 : minX; - - minY = y1; - minY = y2 < minY ? y2 : minY; - minY = y3 < minY ? y3 : minY; - minY = y4 < minY ? y4 : minY; - - maxX = x1; - maxX = x2 > maxX ? x2 : maxX; - maxX = x3 > maxX ? x3 : maxX; - maxX = x4 > maxX ? x4 : maxX; - - maxY = y1; - maxY = y2 > maxY ? y2 : maxY; - maxY = y3 > maxY ? y3 : maxY; - maxY = y4 > maxY ? y4 : maxY; - - const bounds = this._bounds; - - bounds.x = minX; - bounds.width = maxX - minX; - - bounds.y = minY; - bounds.height = maxY - minY; - - // store a reference so that if this function gets called again in the render cycle we do not have to recalculate - this._currentBounds = bounds; - - return bounds; - } - - /** * Checks if a point is inside this tiling sprite. * * @param {PIXI.Point} point - the point to check diff --git a/test/core/TilingSprite.js b/test/core/TilingSprite.js new file mode 100644 index 0000000..d128b79 --- /dev/null +++ b/test/core/TilingSprite.js @@ -0,0 +1,27 @@ +'use strict'; + +describe('PIXI.Sprite', function () +{ + describe('getBounds()', function () + { + it('must have correct value according to _width and _height', function () + { + const parent = new PIXI.Container(); + const texture = new PIXI.Texture(new PIXI.BaseTexture()); + const tilingSprite = new PIXI.TilingSprite(texture, 200, 300); + + parent.addChild(tilingSprite); + + tilingSprite.anchor.set(0.5, 0.5); + tilingSprite.scale.set(-2, -2); + tilingSprite.position.set(50, 40); + + const bounds = tilingSprite.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/index.js b/test/core/index.js index 0393e14..2ca4fe3 100755 --- a/test/core/index.js +++ b/test/core/index.js @@ -5,6 +5,7 @@ require('./DisplayObject'); require('./getLocalBounds'); require('./Sprite'); +require('./TilingSprite'); require('./Text'); require('./toGlobal'); require('./toLocal');