Constructor
new Polygon(strokeopt, backgroundopt, opacityopt, xopt, yopt, _pointsopt)
Creates an instance of Polygon shape object.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
stroke | Stroke | <optional> | null | A border of the shape. |
background | string | <optional> | null | Color, gradient, or image background, similar to CSS. Use an empty string to remove the background. |
opacity | number | <optional> | 1 | Opacity of the shape (0 to 1). |
x | number | <optional> | 0 | Horizontal position from the left edge. |
y | number | <optional> | 0 | Vertical position from the top edge. |
_points | Array.<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. |
- See
Classes
Methods
addPoint(point, indexopt) → {Polygon}
Adds a point to the Polygon as a last point or at a specified index.
| Name | Type | Attributes | Description |
|---|---|---|---|
point | Object | A point object with X and Y coordinates (e.g., {x: 20, y:30}); | |
index | number | <optional> | If specified, the point will be added at this position; otherwise at the end. |
Returns this Polygon shape object for chaining.
- Type:
- Polygon
<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 arraypoint(index, point_or_x, yopt) → {Object|Polygon}
Sets or gets a point of the Polygon {x:number, y:number} at a specified index.
| Name | Type | Attributes | Description |
|---|---|---|---|
index | number | Index of the existing point to update. | |
point_or_x | Object | | New point object or X coordinate of the new point. | |
y | number | <optional> | Y coordinate of the new point. |
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
<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.
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<object> | <optional> | An array of new point objects [{x:number, y:number}, ...]. Omit to get current point. |
When called without arguments, returns current points; otherwise returns this Polygon for method chaining.
- Type:
- Array.<object> |
Polygon
<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.
| Name | Type | Description |
|---|---|---|
index | number | Index of the point to return. |
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.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
include | string | <optional> | "all" | Specified what to include. "all" is default. Valid values are: "all", "stroke", "background", "points", "basic". |
Returns an object containing this shape's properties.
- Type:
- object