Polygon

Polygon is a shape with multiple corners at specified points. Each point is an X and Y coordinate relative to the shapes X and Y position.

Polygon extends Shape class.

Constructor

new Polygon(strokeopt, backgroundopt, opacityopt, xopt, yopt, _pointsopt)

Creates an instance of Polygon shape object.

Parameters:
NameTypeAttributesDefaultDescription
strokeStroke<optional>
null

A border of the shape.

backgroundstring<optional>
null

Color, gradient, or image background, similar to CSS. Use an empty string to remove the background.

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 corner of the shape [{x:number,y:number}, ...]. For example a simple start has 10 points.

Classes

Polygon

Methods

addPoint(point, indexopt) → {Polygon}

Adds a point to the Polygon 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 Polygon shape object for chaining.

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

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

Sets or gets a point of the Polygon {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 Polygon or throws an Error.

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

// or...

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

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

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 Polygon for method chaining.

Type: 
Array.<object> | Polygon
Examples
<h2>Getting existing points as an Array:</h2>
let _points = polygon.points(); // -> array of objects
<h2>Setting a new array of points:</h2>
polygon.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 Polygon 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