Skip to main content
Version: Phaser v4.0.0

Line

Defines a Line segment: a finite portion of a line described by a start point (x1, y1) and an end point (x2, y2). Unlike an infinite mathematical line, a Line segment has a fixed length and direction.

Line segments are used throughout Phaser for collision detection, geometry calculations, path and boundary definitions, and placement utilities. You can retrieve a point at any percentage along the line via getPoint, get an array of evenly-spaced points via getPoints, or query the bounding extents through the left, right, top, and bottom accessors.

Constructor

new Line([x1], [y1], [x2], [y2])

Parameters

nametypeoptionaldefaultdescription
x1numberYes0The x coordinate of the line's starting point.
y1numberYes0The y coordinate of the line's starting point.
x2numberYes0The x coordinate of the line's ending point.
y2numberYes0The y coordinate of the line's ending point.

Scope: static

Source: src/geom/line/Line.js#L14
Since: 3.0.0

Public Methods

Angle

<static> Angle(line)

Description:

Calculate the angle of the line in radians.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the angle of.

Returns: number - The angle of the line, in radians.

Source: src/geom/line/Angle.js#L7
Since: 3.0.0


BresenhamPoints

<static> BresenhamPoints(line, [stepRate], [results])

Description:

Using Bresenham's line algorithm this will return an array of all coordinates on the given line.

The start and end points are rounded before this runs as the algorithm works on integers.

Parameters:

nametypeoptionaldefaultdescription
linePhaser.Geom.LineNoThe line.
stepRatenumberYes1Controls how densely the points are sampled along the line. A value of 1 returns every integer coordinate; a value of 2 returns every other coordinate, and so on.
resultsArray.<Phaser.Types.Math.Vector2Like>YesAn optional array to push the resulting coordinates into.

Returns: Array.<Phaser.Types.Math.Vector2Like> - The array of coordinates on the line.

Source: src/geom/line/BresenhamPoints.js#L7
Since: 3.0.0


CenterOn

<static> CenterOn(line, x, y)

Description:

Center a line on the given coordinates.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to center.
xnumberNoThe horizontal coordinate to center the line on.
ynumberNoThe vertical coordinate to center the line on.

Returns: Phaser.Geom.Line - The centered line.

Source: src/geom/line/CenterOn.js#L8
Since: 3.0.0


Clone

<static> Clone(source)

Description:

Clone the given line.

Parameters:

nametypeoptionaldescription
sourcePhaser.Geom.LineNoThe source line to clone.

Returns: Phaser.Geom.Line - The cloned line.

Source: src/geom/line/Clone.js#L9
Since: 3.0.0


CopyFrom

<static> CopyFrom(source, dest)

Description:

Copy the values of one line to a destination line.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
sourcePhaser.Geom.LineNoThe source line to copy the values from.
destPhaser.Geom.LineNoThe destination line to copy the values to.

Returns: Phaser.Geom.Line - The destination line.

Source: src/geom/line/CopyFrom.js#L7
Since: 3.0.0


Equals

<static> Equals(line, toCompare)

Description:

Compares two lines for strict equality. Two lines are considered equal if their start and end point coordinates all match exactly: x1, y1, x2, and y2.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe first line to compare.
toComparePhaser.Geom.LineNoThe second line to compare.

Returns: boolean - true if the two lines have identical start and end point coordinates, otherwise false.

Source: src/geom/line/Equals.js#L7
Since: 3.0.0


Extend

<static> Extend(line, left, [right])

Description:

Extends the start and end points of a Line by the given amounts.

The amounts can be positive or negative. Positive values will increase the length of the line, while negative ones will decrease it.

If no right value is provided it will extend the length of the line equally in both directions.

Pass a value of zero to leave the start or end point unchanged.

The left value extends outward from the start point (x1, y1) along the line's direction, and the right value extends outward from the end point (x2, y2).

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line instance to extend.
leftnumberNoThe amount to extend the start of the line by.
rightnumberYesThe amount to extend the end of the line by. If not given it will be set to the left value.

Returns: Phaser.Geom.Line - The modified Line instance.

Source: src/geom/line/Extend.js#L9
Since: 3.16.0


GetEasedPoints

<static> GetEasedPoints(line, ease, quantity, [collinearThreshold], [easeParams])

Description:

Returns an array of quantity Points where each point is taken from the given Line, spaced out according to the ease function specified.


const line = new Phaser.Geom.Line(100, 300, 700, 300);

const points = Phaser.Geom.Line.GetEasedPoints(line, 'sine.out', 32)

In the above example, the points array will contain 32 points spread-out across the length of line, where the position of each point is determined by the Sine.out ease function.

You can optionally provide a collinear threshold. In this case, the resulting points are checked against each other, and if they are < collinearThreshold distance apart, they are dropped from the results. This can help avoid lots of clustered points at far ends of the line with tightly-packed eases such as Quartic. Leave the value set to zero to skip this check.

Note that if you provide a collinear threshold, the resulting array may not always contain quantity points.

Tags:

  • generic

Parameters:

nametypeoptionaldefaultdescription
linePhaser.Geom.LineNoThe Line object.
easestring | functionNoThe ease to use. This can be either a string from the EaseMap, or a custom function.
quantitynumberNoThe number of points to return. Note that if you provide a collinearThreshold, the resulting array may not always contain this number of points.
collinearThresholdnumberYes0An optional threshold. The final array is reduced so that each point is spaced out at least this distance apart. This helps reduce clustering in noisy eases.
easeParamsArray.<number>YesAn optional array of ease parameters to go with the ease.

Returns: Array.<Phaser.Math.Vector2> - An array of Math.Vector2s containing the coordinates of the points on the line.

Source: src/geom/line/GetEasedPoints.js#L11
Since: 3.23.0


GetMidPoint

<static> GetMidPoint(line, [out])

Description:

Get the midpoint of the given line.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to get the midpoint of.
outPhaser.Math.Vector2YesAn optional Vector2 object to store the midpoint in.

Returns: Phaser.Math.Vector2 - The midpoint of the Line.

Source: src/geom/line/GetMidPoint.js#L9
Since: 3.0.0


GetNearestPoint

<static> GetNearestPoint(line, vec, [out])

Description:

Gets the nearest point on the infinite line defined by the given Line segment to the specified point. Note that the returned point is projected onto the full line, not clamped to the segment endpoints.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to get the nearest point on.
vecPhaser.Math.Vector2NoThe Vector2 to get the nearest point to.
outPhaser.Math.Vector2YesAn optional Vector2 object, to store the coordinates of the nearest point on the line.

Returns: Phaser.Math.Vector2 - The nearest point on the line.

Source: src/geom/line/GetNearestPoint.js#L10
Since: 3.16.0


GetNormal

<static> GetNormal(line, [out])

Description:

Calculate the normal of the given line.

The normal of a line is a vector that points perpendicular from it.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the normal of.
outPhaser.Math.Vector2YesAn optional Vector2 object to store the normal in.

Returns: Phaser.Math.Vector2 - The normal of the Line.

Source: src/geom/line/GetNormal.js#L11
Since: 3.0.0


getPoint

<instance> getPoint(position, [output])

Description:

Get a point on a line that's a given percentage along its length.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
positionnumberNoA value between 0 and 1, where 0 is the start, 0.5 is the middle and 1 is the end of the line.
outputPhaser.Math.Vector2YesAn optional Vector2 object to store the coordinates of the point on the line.

Returns: Phaser.Math.Vector2 - A Vector2 object containing the coordinates of the point on the line.

Source: src/geom/line/Line.js#L95
Since: 3.0.0


GetPoint

<static> GetPoint(line, position, [out])

Description:

Get a point on a line that's a given percentage along its length.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line.
positionnumberNoA value between 0 and 1, where 0 is the start, 0.5 is the middle and 1 is the end of the line.
outPhaser.Math.Vector2YesAn optional Vector2 object to store the coordinates of the point on the line.

Returns: Phaser.Math.Vector2 - The point on the line.

Source: src/geom/line/GetPoint.js#L9
Since: 3.0.0


getPointA

<instance> getPointA([vec2])

Description:

Returns a Vector2 object that corresponds to the start of this Line.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
vec2Phaser.Math.Vector2YesA Vector2 object to set the results in. If undefined a new Vector2 will be created.

Returns: Phaser.Math.Vector2 - A Vector2 object that corresponds to the start of this Line.

Source: src/geom/line/Line.js#L206
Since: 3.0.0


getPointB

<instance> getPointB([vec2])

Description:

Returns a Vector2 object that corresponds to the end of this Line.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
vec2Phaser.Math.Vector2YesA Vector2 object to set the results in. If undefined a new Vector2 will be created.

Returns: Phaser.Math.Vector2 - A Vector2 object that corresponds to the end of this Line.

Source: src/geom/line/Line.js#L227
Since: 3.0.0


getPoints

<instance> getPoints(quantity, [stepRate], [output])

Description:

Get a number of points along a line's length.

Provide a quantity to get an exact number of points along the line.

Provide a stepRate to ensure a specific distance between each point on the line. Set quantity to 0 when providing a stepRate.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
quantitynumberNoThe number of points to place on the line. Set to 0 to use stepRate instead.
stepRatenumberYesThe distance between each point on the line. When set, quantity is implied and should be set to 0.
outputArray.<Phaser.Math.Vector2>YesAn optional array of Vector2 objects to store the coordinates of the points on the line.

Returns: Array.<Phaser.Math.Vector2> - An array of Vector2 objects containing the coordinates of the points on the line.

Source: src/geom/line/Line.js#L113
Since: 3.0.0


GetPoints

<static> GetPoints(line, quantity, [stepRate], [out])

Description:

Get a number of evenly-spaced points along a line, starting from the first endpoint (x1, y1). The last endpoint (x2, y2) is not included in the returned points.

Provide a quantity to get an exact number of points along the line.

Provide a stepRate to ensure a specific distance between each point on the line. Set quantity to 0 when providing a stepRate.

See also GetEasedPoints for a way to distribute the points across the line according to an ease type or input function.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line.
quantitynumberNoThe number of points to place on the line. Set to 0 to use stepRate instead.
stepRatenumberYesThe distance between each point on the line. When set, quantity is calculated as the line length divided by this value; quantity should be set to 0.
outArray.<Phaser.Math.Vector2>YesAn optional array of Vector2 objects to store the coordinates of the points on the line.

Returns: Array.<Phaser.Math.Vector2> - An array of Vector2 objects containing the coordinates of the points on the line.

Source: src/geom/line/GetPoints.js#L10
Since: 3.0.0


getRandomPoint

<instance> getRandomPoint([point])

Description:

Returns a randomly chosen point on this Line, selected with uniform distribution along its length. If a Vector2 is provided it will be populated with the result and returned; otherwise a new Vector2 is created.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
pointPhaser.Math.Vector2YesAn instance of a Vector2 to be modified.

Returns: Phaser.Math.Vector2 - A random point on the Line.

Source: src/geom/line/Line.js#L137
Since: 3.0.0


GetShortestDistance

<static> GetShortestDistance(line, point)

Description:

Calculates the shortest (perpendicular) distance from an infinite line, defined by the two endpoints of the given Line object, to the given Point. If the line has zero length (both endpoints are identical), this function returns false.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to get the distance from.
pointPhaser.Types.Math.Vector2LikeNoThe point to get the shortest distance to.

Returns: boolean, number - The shortest perpendicular distance from the line to the point, or false if the line has zero length.

Source: src/geom/line/GetShortestDistance.js#L8
Since: 3.16.0


Height

<static> Height(line)

Description:

Calculate the height of the given line.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the height of.

Returns: number - The height of the line.

Source: src/geom/line/Height.js#L7
Since: 3.0.0


Length

<static> Length(line)

Description:

Calculate the length of the given line.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the length of.

Returns: number - The length of the line.

Source: src/geom/line/Length.js#L7
Since: 3.0.0


NormalAngle

<static> NormalAngle(line)

Description:

Get the angle of the normal of the given line in radians.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the angle of the normal of.

Returns: number - The angle of the normal of the line in radians.

Source: src/geom/line/NormalAngle.js#L11
Since: 3.0.0


NormalX

<static> NormalX(line)

Description:

Returns the x component of the normal vector of the given line.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe Line object to get the normal value from.

Returns: number - The x component of the normal vector of the line.

Source: src/geom/line/NormalX.js#L10
Since: 3.0.0


NormalY

<static> NormalY(line)

Description:

The Y value of the normal of the given line. The normal of a line is a vector that points perpendicular from it.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the normal of.

Returns: number - The Y value of the normal of the Line.

Source: src/geom/line/NormalY.js#L10
Since: 3.0.0


Offset

<static> Offset(line, x, y)

Description:

Translates both endpoints of the given Line by the specified horizontal and vertical amounts, effectively moving the line to a new position in 2D space while preserving its length and angle.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to offset.
xnumberNoThe horizontal offset to add to the line.
ynumberNoThe vertical offset to add to the line.

Returns: Phaser.Geom.Line - The modified Line object, with both endpoints moved by the given offset.

Source: src/geom/line/Offset.js#L7
Since: 3.0.0


PerpSlope

<static> PerpSlope(line)

Description:

Calculate the perpendicular slope of the given line.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the perpendicular slope of.

Returns: number - The perpendicular slope of the line.

Source: src/geom/line/PerpSlope.js#L7
Since: 3.0.0


Random

<static> Random(line, [out])

Description:

Returns a random point on a given Line.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe Line to calculate the random point on.
outPhaser.Math.Vector2YesAn instance of a Vector2 to be modified.

Returns: Phaser.Math.Vector2 - A random point on the Line stored in a Vector2.

Source: src/geom/line/Random.js#L9
Since: 3.0.0


ReflectAngle

<static> ReflectAngle(lineA, lineB)

Description:

Calculates the reflected angle of Line A off the surface represented by Line B. This is the outgoing angle based on the angle of incidence (Line A) and the surface normal of Line B. The result is in radians.

Parameters:

nametypeoptionaldescription
lineAPhaser.Geom.LineNoThe incident line whose angle of incidence is used.
lineBPhaser.Geom.LineNoThe surface line, used to calculate the normal angle of reflection.

Returns: number - The reflected angle of Line A off the surface of Line B, in radians.

Source: src/geom/line/ReflectAngle.js#L10
Since: 3.0.0


Rotate

<static> Rotate(line, angle)

Description:

Rotate a line around its midpoint by the given angle in radians.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to rotate.
anglenumberNoThe angle of rotation in radians.

Returns: Phaser.Geom.Line - The rotated line.

Source: src/geom/line/Rotate.js#L9
Since: 3.0.0


RotateAroundPoint

<static> RotateAroundPoint(line, point, angle)

Description:

Rotate a line around a point by the given angle in radians.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to rotate.
pointPhaser.Math.Vector2NoThe point to rotate the line around.
anglenumberNoThe angle of rotation in radians.

Returns: Phaser.Geom.Line - The rotated line.

Source: src/geom/line/RotateAroundPoint.js#L9
Since: 3.0.0


RotateAroundXY

<static> RotateAroundXY(line, x, y, angle)

Description:

Rotate a line around the given coordinates by the given angle in radians.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to rotate.
xnumberNoThe horizontal coordinate to rotate the line around.
ynumberNoThe vertical coordinate to rotate the line around.
anglenumberNoThe angle of rotation in radians.

Returns: Phaser.Geom.Line - The rotated line.

Source: src/geom/line/RotateAroundXY.js#L7
Since: 3.0.0


setFromObjects

<instance> setFromObjects(start, end)

Description:

Sets this Line to match the x/y coordinates of the two given Vector2Like objects.

Parameters:

nametypeoptionaldescription
startPhaser.Types.Math.Vector2LikeNoAny object with public x and y properties, whose values will be assigned to the x1/y1 components of this Line.
endPhaser.Types.Math.Vector2LikeNoAny object with public x and y properties, whose values will be assigned to the x2/y2 components of this Line.

Returns: Phaser.Geom.Line - This Line object.

Source: src/geom/line/Line.js#L184
Since: 3.70.0


setTo

<instance> setTo([x1], [y1], [x2], [y2])

Description:

Set new coordinates for the line endpoints.

Parameters:

nametypeoptionaldefaultdescription
x1numberYes0The x coordinate of the line's starting point.
y1numberYes0The y coordinate of the line's starting point.
x2numberYes0The x coordinate of the line's ending point.
y2numberYes0The y coordinate of the line's ending point.

Returns: Phaser.Geom.Line - This Line object.

Source: src/geom/line/Line.js#L155
Since: 3.0.0


SetToAngle

<static> SetToAngle(line, x, y, angle, length)

Description:

Set a line to a given position, angle and length.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to set.
xnumberNoThe horizontal start position of the line.
ynumberNoThe vertical start position of the line.
anglenumberNoThe angle of the line in radians.
lengthnumberNoThe length of the line.

Returns: Phaser.Geom.Line - The updated line.

Source: src/geom/line/SetToAngle.js#L7
Since: 3.0.0


Slope

<static> Slope(line)

Description:

Calculate the slope of the given line.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the slope of.

Returns: number - The slope of the line.

Source: src/geom/line/Slope.js#L7
Since: 3.0.0


Width

<static> Width(line)

Description:

Calculates the width of the given line, defined as the absolute difference between the x-coordinates of its two endpoints (x1 and x2). This represents the horizontal extent of the line, not its geometric length. To get the true length of the line, use Phaser.Geom.Line.Length instead.

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineNoThe line to calculate the width of.

Returns: number - The width of the line, i.e. the absolute difference between its x1 and x2 coordinates.

Source: src/geom/line/Width.js#L7
Since: 3.0.0


Public Members

bottom

bottom: number

Description:

The bottom-most y coordinate of this Line, i.e. the greater of y1 and y2. When set, the endpoint that currently holds the larger y value is moved to the new position.

Source: src/geom/line/Line.js#L335
Since: 3.0.0


left

left: number

Description:

The left-most x coordinate of this Line, i.e. the lesser of x1 and x2. When set, the endpoint that currently holds the smaller x value is moved to the new position.

Source: src/geom/line/Line.js#L248
Since: 3.0.0


right: number

Description:

The right-most x coordinate of this Line, i.e. the greater of x1 and x2. When set, the endpoint that currently holds the larger x value is moved to the new position.

Source: src/geom/line/Line.js#L277
Since: 3.0.0


top

top: number

Description:

The top-most y coordinate of this Line, i.e. the lesser of y1 and y2. When set, the endpoint that currently holds the smaller y value is moved to the new position.

Source: src/geom/line/Line.js#L306
Since: 3.0.0


type

type: number

Description:

The geometry constant type of this object: GEOM_CONST.LINE. Used for fast type comparisons.

Source: src/geom/line/Line.js#L47
Since: 3.19.0


x1

x1: number

Description:

The x coordinate of the line's starting point.

Source: src/geom/line/Line.js#L58
Since: 3.0.0


x2

x2: number

Description:

The x coordinate of the line's ending point.

Source: src/geom/line/Line.js#L76
Since: 3.0.0


y1

y1: number

Description:

The y coordinate of the line's starting point.

Source: src/geom/line/Line.js#L67
Since: 3.0.0


y2

y2: number

Description:

The y coordinate of the line's ending point.

Source: src/geom/line/Line.js#L85
Since: 3.0.0