Polyline

Polyline is a shape with multiple lines drawn from point to point for each provided point. Each point is an X and Y coordinate relative to the shapes X and Y position.

Polyline is similar to Polygon, but the area within points is not filled in with background, and first and last points are not connected.

Just like a Line shape, Polyline is a geometric line that doesn't have an area to be filled. For the line to be visible you specify the stroke size to be more that 0.

Polyline extends Shape class.

Constructor

new Polyline(strokeopt, opacityopt, xopt, yopt, _pointsopt)

Creates an instance of Polygon shape object.

Parameters:
NameTypeAttributesDefaultDescription
strokeStroke<optional>

A border of the shape.

opacitynumber<optional>
1

Opacity of the shape (0 to 1).

xnumber<optional>
0

Horizontal position from the left edge.

ynumber<optional>
0

Vertical position from the top edge.

_pointsArray.<object><optional>
[]

An array of point objects that represent each point where lines begin, connect and end: [{x:number,y:number}, ...].

Classes

Polyline

Methods

addPoint(point, indexopt) → {Polyline}

Adds a point to the Polyline as a last point or at a specified index.

Parameters:
NameTypeAttributesDescription
pointObject

A point object with X and Y coordinates (e.g., {x: 20, y:30});

indexnumber<optional>

If specified, the point will be added at this position; otherwise at the end.

Returns:

Returns this Polyline shape object for chaining.

Type: 
Polyline
Examples
<h2>Adding a point to the end of the points array:</h2>
polyline.addPoint({x:20, y:30});
<h2>Adding a point at a specified index:</h2>
polyline.addPoint({x:30, y:60}, 0); // adds a point the beginning of points array

point(index, point_or_x, yopt) → {Object|Polyline}

Sets or gets a point of the Polyline {x:number, y:number} at a specified index.

Parameters:
NameTypeAttributesDescription
indexnumber

Index of the existing point to update.

point_or_xObject | number

New point object or X coordinate of the new point.

ynumber<optional>

Y coordinate of the new point.

Returns:

When getting a point, returns a point object or throws and Error. When setting the point, returns this Polyline or throws an Error.

Type: 
Object | Polyline
Examples
<h2>Updating a point at index 3:</h2>
polyline.point(3, 50, 80); // x = 50, y = 80

// or...

polyline.point(3, {x:50, y:80});
<h2>Getting a point at index 3:</h2>
let p = polyline.point(3);

points(pointsopt) → {Array.<object>|Polyline}

Sets an array of new points, or get an array of existing point.

Parameters:
NameTypeAttributesDescription
pointsArray.<object><optional>

An array of new point objects [{x:number, y:number}, ...]. Omit to get current point.

Returns:

When called without arguments, returns current points; otherwise returns this Polyline for method chaining.

Type: 
Array.<object> | Polyline
Examples
<h2>Getting existing points as an Array:</h2>
let _points = polyline.points(); // -> array of objects
<h2>Setting a new array of points:</h2>
polyline.points({
    {x:10, y:10},
    {x:40, y:10},
    {x:60, y:60},
    {x:30, y:60},
    {x:10, y:10}
});

removePoint(index) → {Object|null}

Removes a point from the Polyline at a specified index.

Parameters:
NameTypeDescription
indexnumber

Index of the point to return.

Returns:

Returns the deleted point or null of there was no point at specified index.

Type: 
Object | null

toObject(includeopt) → {object}

Returns a JSON object that contains this shape properties. It may be converted back to this shape.

Parameters:
NameTypeAttributesDefaultDescription
includestring<optional>
"all"

Specified what to include. "all" is default. Valid values are: "all", "stroke", "background", "points", "basic".

Returns:

Returns an object containing this shape's properties.

Type: 
object