diff --git a/packages/graphics/src/Graphics.js b/packages/graphics/src/Graphics.js index 7c11c8f..857de22 100644 --- a/packages/graphics/src/Graphics.js +++ b/packages/graphics/src/Graphics.js @@ -1033,9 +1033,18 @@ _calculateBounds() { this.finishPoly(); - const lb = this.geometry.bounds; - this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + const geometry = this.geometry; + + // skipping when graphics is empty, like a container + if (!geometry.graphicsData.length) + { + return; + } + + const { minX, minY, maxX, maxY } = geometry.bounds; + + this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); } /** diff --git a/packages/graphics/src/Graphics.js b/packages/graphics/src/Graphics.js index 7c11c8f..857de22 100644 --- a/packages/graphics/src/Graphics.js +++ b/packages/graphics/src/Graphics.js @@ -1033,9 +1033,18 @@ _calculateBounds() { this.finishPoly(); - const lb = this.geometry.bounds; - this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + const geometry = this.geometry; + + // skipping when graphics is empty, like a container + if (!geometry.graphicsData.length) + { + return; + } + + const { minX, minY, maxX, maxY } = geometry.bounds; + + this._bounds.addFrame(this.transform, minX, minY, maxX, maxY); } /** diff --git a/packages/graphics/test/index.js b/packages/graphics/test/index.js index d280e8d..06e097a 100644 --- a/packages/graphics/test/index.js +++ b/packages/graphics/test/index.js @@ -386,9 +386,12 @@ describe('_calculateBounds', function () { - it('should only call updateLocalBounds once', function () + it('should only call updateLocalBounds once when not empty', function () { const graphics = new Graphics(); + + graphics.drawRect(0, 0, 10, 10); + const spy = sinon.spy(graphics.geometry, 'calculateBounds'); graphics._calculateBounds(); @@ -399,6 +402,21 @@ expect(spy).to.have.been.calledOnce; }); + + it('should not call updateLocalBounds when empty', function () + { + const graphics = new Graphics(); + + const spy = sinon.spy(graphics.geometry, 'calculateBounds'); + + graphics._calculateBounds(); + + expect(spy).to.not.have.been.called; + + graphics._calculateBounds(); + + expect(spy).to.not.have.been.called; + }); }); describe('getBounds', function () @@ -433,6 +451,55 @@ expect(width).to.equal(104); expect(height).to.equal(204); }); + + it('should be zero for empty Graphics', function () + { + const graphics = new Graphics(); + + const { x, y, width, height } = graphics.getBounds(); + + expect(x).to.equal(0); + expect(y).to.equal(0); + expect(width).to.equal(0); + expect(height).to.equal(0); + }); + + it('should be zero after clear', function () + { + const graphics = new Graphics(); + + graphics + .lineStyle(4, 0xff0000) + .beginFill(0x0) + .drawRect(10, 20, 100, 200) + .clear(); + + const { x, y, width, height } = graphics.getBounds(); + + expect(x).to.equal(0); + expect(y).to.equal(0); + expect(width).to.equal(0); + expect(height).to.equal(0); + }); + + it('should be equal of childs bounds when empty', function () + { + const graphics = new Graphics(); + const child = new Graphics(); + + child + .beginFill(0x0) + .drawRect(10, 20, 100, 200); + + graphics.addChild(child); + + const { x, y, width, height } = graphics.getBounds(); + + expect(x).to.equal(10); + expect(y).to.equal(20); + expect(width).to.equal(100); + expect(height).to.equal(200); + }); }); describe('drawCircle', function ()