Represents a 2D geometric shape with a path and optional holes.
This class provides methods for calculating properties, transformations, and rendering the shape.
// Create a shape with a square path and a square holeconst shape = new Shape( [ [0, 0], [10, 0], [10, 10], [0, 10], ], [ [ [3, 3], [5, 3], [5, 5], [3, 5], ], ]);// Get the shape's centerconst center = shape.getCenter(); // [5, 5]// Get the bounding rectangleconst boundingRect = shape.getBoundingRect(); // [[0, 0], [10, 10]]// Scale the shape by a factor of 2shape.scale(2);// Draw the shape on a canvasshape.draw(context); Copy
// Create a shape with a square path and a square holeconst shape = new Shape( [ [0, 0], [10, 0], [10, 10], [0, 10], ], [ [ [3, 3], [5, 3], [5, 5], [3, 5], ], ]);// Get the shape's centerconst center = shape.getCenter(); // [5, 5]// Get the bounding rectangleconst boundingRect = shape.getBoundingRect(); // [[0, 0], [10, 10]]// Scale the shape by a factor of 2shape.scale(2);// Draw the shape on a canvasshape.draw(context);
Constructs a new Shape instance.
Shape
The main path of the shape, represented as an array of points.
Optional holes, each represented as a path. Defaults to an empty array.
Will throw an error if any hole has fewer than 3 points.
An array of holes, where each hole is represented as a path.
The main path of the shape, defined as an array of points.
Gets the number of faces (edges) in the shape's main path.
Creates a deep copy of the shape.
A cloned Shape instance.
Draws the shape on a canvas context, including its holes.
The 2D rendering context of a canvas.
Calculates the axis-aligned bounding rectangle of the shape.
A tuple of two points: the top-left [minX, minY] and bottom-right [maxX, maxY].
[minX, minY]
[maxX, maxY]
Calculates the centroid of the shape's main path.
The center of the shape as [x, y].
[x, y]
Retrieves a specific face (edge) of the shape by index.
The index of the face.
The face as a line [start, end].
[start, end]
Scales the shape by a given factor.
The scale factor.
Applies a transformation matrix to the shape's path and holes.
A transformation matrix to apply.
The transformed shape.
Represents a 2D geometric shape with a path and optional holes.
This class provides methods for calculating properties, transformations, and rendering the shape.
Example