diff --git a/src/gameobjects/shape/line/Line.js b/src/gameobjects/shape/line/Line.js index f1931f90fa..44960b3b75 100644 --- a/src/gameobjects/shape/line/Line.js +++ b/src/gameobjects/shape/line/Line.js @@ -64,9 +64,6 @@ var Line = new Class({ Shape.call(this, scene, 'Line', new GeomLine(x1, y1, x2, y2)); - var width = Math.max(1, this.geom.right - this.geom.left); - var height = Math.max(1, this.geom.bottom - this.geom.top); - /** * The width (or thickness) of the line. * See the setLineWidth method for extra details on changing this on WebGL. @@ -98,7 +95,7 @@ var Line = new Class({ this._endWidth = 1; this.setPosition(x, y); - this.setSize(width, height); + this.updateSize(); if (strokeColor !== undefined) { @@ -153,9 +150,29 @@ var Line = new Class({ { this.geom.setTo(x1, y1, x2, y2); + this.updateSize(); + return this; - } + }, + /** + * Updates the width and height of the Line based on its geometry. + * + * @method Phaser.GameObjects.Line#updateSize + * @private + * @since 4.1.0 + * + * @return {this} This Line instance. + */ + updateSize: function () + { + var width = Math.max(1, this.geom.right - this.geom.left); + var height = Math.max(1, this.geom.bottom - this.geom.top); + + this.setSize(width, height); + + return this; + } }); module.exports = Line; diff --git a/tests/gameobjects/shape/line/Line.test.js b/tests/gameobjects/shape/line/Line.test.js index 727b5bff8b..8cecb0b0c4 100644 --- a/tests/gameobjects/shape/line/Line.test.js +++ b/tests/gameobjects/shape/line/Line.test.js @@ -1,4 +1,5 @@ var Line = require('../../../../src/gameobjects/shape/line/Line'); +var GeomLine = require('../../../../src/geom/line/Line'); describe('Line', function () { @@ -88,11 +89,9 @@ describe('Line', function () beforeEach(function () { - geomSetTo = vi.fn(); obj = Object.create(Line.prototype); - obj.geom = { - setTo: geomSetTo - }; + obj.geom = new GeomLine(); + geomSetTo = vi.spyOn(obj.geom, 'setTo'); }); it('should delegate to geom.setTo with the provided coordinates', function () @@ -136,5 +135,47 @@ describe('Line', function () Line.prototype.setTo.call(obj); expect(geomSetTo).toHaveBeenCalledWith(undefined, undefined, undefined, undefined); }); + + it('should calculate width from the difference of x2 and x1', function () + { + obj.width = 0; + Line.prototype.setTo.call(obj, 1, 2, 3, 5); + expect(obj.width).toBe(2); + }); + + it('should calculate height from the difference of y2 and y1', function () + { + obj.height = 0; + Line.prototype.setTo.call(obj, 1, 2, 3, 5); + expect(obj.height).toBe(3); + }); + + it('should calculate the correct width when x2 < x1', function () + { + obj.width = 0; + Line.prototype.setTo.call(obj, 5, 3, 2, 1); + expect(obj.width).toBe(3); + }); + + it('should calculate the correct height when y2 < y1', function () + { + obj.height = 0; + Line.prototype.setTo.call(obj, 5, 3, 2, 1); + expect(obj.height).toBe(2); + }); + + it('should set width to 1 (not 0) when x1 equals x2', function () + { + obj.width = 0; + Line.prototype.setTo.call(obj, 1, 2, 1, 5); + expect(obj.width).toBe(1); + }); + + it('should set height to 1 (not 0) when y1 equals y2', function () + { + obj.height = 0; + Line.prototype.setTo.call(obj, 2, 1, 5, 1); + expect(obj.height).toBe(1); + }); }); });