Name | Type | Description |
---|---|---|
points |
(PointData[] | number[])[] | PointData[] | number[] |
This can be an array of Points
that form the polygon, a flat array of numbers that will be interpreted as [x,y, x,y, ...], or
the arguments passed can be all the points of the polygon e.g.
|
Implements
Members
false
after moveTo, true
after closePath
. In all other cases it is true
.
Get the last X coordinate of the polygon
Get the last Y coordinate of the polygon
An array of the points of this polygon.
type SHAPE_PRIMITIVE readonly
The type of the object, mainly used to avoid instanceof
checks
- Default Value:
- 'polygon'
Get the first X coordinate of the polygon
Get the first Y coordinate of the polygon
Methods
Creates a clone of this polygon.
Returns:
Type | Description |
---|---|
Polygon |
|
Checks whether the x and y coordinates passed to this function are contained within this polygon.
Name | Type | Description |
---|---|---|
x |
number |
The X coordinate of the point to test. |
y |
number |
The Y coordinate of the point to test. |
Returns:
Type | Description |
---|---|
boolean |
|
Checks if this polygon completely contains another polygon.
This is useful for detecting holes in shapes, like when parsing SVG paths. For example, if you have two polygons:
const outerSquare = new Polygon([0,0, 100,0, 100,100, 0,100]); // A square
const innerSquare = new Polygon([25,25, 75,25, 75,75, 25,75]); // A smaller square inside
outerSquare.containsPolygon(innerSquare); // Returns true
innerSquare.containsPolygon(outerSquare); // Returns false
Name | Type | Description |
---|---|---|
polygon |
Polygon |
The polygon to test for containment |
Returns:
Type | Description |
---|---|
boolean | True if this polygon completely contains the other polygon |
Copies another polygon to this one.
Name | Type | Description |
---|---|---|
polygon |
Polygon |
The polygon to copy from. |
Returns:
Type | Description |
---|---|
this | Returns itself. |
Copies this polygon to another one.
Name | Type | Description |
---|---|---|
polygon |
Polygon |
The polygon to copy to. |
Returns:
Type | Description |
---|---|
Polygon | Returns given parameter. |
Returns the framing rectangle of the polygon as a Rectangle object
Name | Type | Attributes | Description |
---|---|---|---|
out |
Rectangle |
<optional> |
optional rectangle to store the result |
Returns:
Type | Description |
---|---|
Rectangle | The framing rectangle |
Determines whether the polygon's points are arranged in a clockwise direction. This is calculated using the "shoelace formula" (also known as surveyor's formula) to find the signed area. A positive area indicates clockwise winding, while negative indicates counter-clockwise.
The formula sums up the cross products of adjacent vertices: For each pair of adjacent points (x1,y1) and (x2,y2), we calculate (x1y2 - x2y1) The final sum divided by 2 gives the signed area - positive for clockwise.
Returns:
Type | Description |
---|---|
boolean | true if the polygon's points are arranged clockwise, false if counter-clockwise |
Checks whether the x and y coordinates given are contained within this polygon including the stroke.
Name | Type | Default | Description |
---|---|---|---|
x |
number |
The X coordinate of the point to test |
|
y |
number |
The Y coordinate of the point to test |
|
strokeWidth |
number |
The width of the line to check |
|
alignment |
number | 0.5 |
The alignment of the stroke, 0.5 by default |
Returns:
Type | Description |
---|---|
boolean | Whether the x/y coordinates are within this polygon |