Indicates if the polygon path is closed.
An array of the points of this polygon stored as a flat array of numbers.
Readonly
typeThe type of the object, mainly used to avoid instanceof
checks
// Check shape type
const shape = new Polygon([0, 0, 100, 0, 50, 100]);
console.log(shape.type); // 'polygon'
// Use in type guards
if (shape.type === 'polygon') {
// TypeScript knows this is a Polygon
console.log(shape.points.length);
}
SHAPE_PRIMITIVE For all shape types
Get the last X coordinate of the polygon.
The x-coordinate of the last vertex
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.lastX); // 300
Get the last Y coordinate of the polygon.
The y-coordinate of the last vertex
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.lastY); // 400
Get the first X coordinate of the polygon
Get the first Y coordinate of the polygon
Creates a clone of this polygon.
A copy of the polygon
// Basic cloning
const original = new Polygon([0, 0, 100, 0, 50, 100]);
const copy = original.clone();
// Clone and modify
const modified = original.clone();
modified.points[0] = 10; // Modify first x coordinate
Checks whether the x and y coordinates passed to this function are contained within this polygon. Uses raycasting algorithm for point-in-polygon testing.
The X coordinate of the point to test
The Y coordinate of the point to test
Whether the x/y coordinates are within this polygon
// Basic containment check
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
const isInside = polygon.contains(25, 25); // true
Checks if this polygon completely contains another polygon. Used for detecting holes in shapes, like when parsing SVG paths.
The polygon to test for containment
True if this polygon completely contains the other polygon
// Basic containment check
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
Copies another polygon to this one.
The polygon to copy from
Returns itself
// Basic copying
const source = new Polygon([0, 0, 100, 0, 50, 100]);
const target = new Polygon();
target.copyFrom(source);
Copies this polygon to another one.
The polygon to copy to
Returns given parameter
// Basic copying
const source = new Polygon([0, 0, 100, 0, 50, 100]);
const target = new Polygon();
source.copyTo(target);
Returns the framing rectangle of the polygon as a Rectangle object.
Optional
out: RectangleOptional rectangle to store the result
The framing rectangle
// Basic bounds calculation
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
const bounds = polygon.getBounds();
// bounds: x=0, y=0, width=100, height=100
// Reuse existing rectangle
const rect = new Rectangle();
polygon.getBounds(rect);
Determines whether the polygon's points are arranged in a clockwise direction. Uses the shoelace formula (surveyor's formula) to calculate 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.
true
if the polygon's points are arranged clockwise, false
if counter-clockwise
// Check polygon winding
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
console.log(polygon.isClockwise()); // Check direction
// Use in path construction
const hole = new Polygon([25, 25, 75, 25, 75, 75, 25, 75]);
if (hole.isClockwise() === shape.isClockwise()) {
hole.points.reverse(); // Reverse for proper hole winding
}
Checks whether the x and y coordinates given are contained within this polygon including the stroke.
The X coordinate of the point to test
The Y coordinate of the point to test
The width of the line to check
The alignment of the stroke (0 = inner, 0.5 = centered, 1 = outer)
Whether the x/y coordinates are within this polygon's stroke
// Basic stroke check
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
const isOnStroke = polygon.strokeContains(25, 25, 4); // 4px line width
// Check with different alignments
const innerStroke = polygon.strokeContains(25, 25, 4, 0); // Inside
const centerStroke = polygon.strokeContains(25, 25, 4, 0.5); // Centered
const outerStroke = polygon.strokeContains(25, 25, 4, 1); // Outside
Returns a string representation of an object.
A class to define a shape via user defined coordinates. Used for creating complex shapes and hit areas with custom points.
Example
See
Point For point objects used in construction