Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/gameobjects/shape/line/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -98,7 +95,7 @@ var Line = new Class({
this._endWidth = 1;

this.setPosition(x, y);
this.setSize(width, height);
this.updateSize();

if (strokeColor !== undefined)
{
Expand Down Expand Up @@ -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;
49 changes: 45 additions & 4 deletions tests/gameobjects/shape/line/Line.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Line = require('../../../../src/gameobjects/shape/line/Line');
var GeomLine = require('../../../../src/geom/line/Line');

describe('Line', function ()
{
Expand Down Expand Up @@ -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 ()
Expand Down Expand Up @@ -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);
});
});
});