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 hole
const shape = new Shape(
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
],
[
[
[3, 3],
[5, 3],
[5, 5],
[3, 5],
],
]
);

// Get the shape's center
const center = shape.getCenter(); // [5, 5]

// Get the bounding rectangle
const boundingRect = shape.getBoundingRect(); // [[0, 0], [10, 10]]

// Scale the shape by a factor of 2
shape.scale(2);

// Draw the shape on a canvas
shape.draw(context);

Constructors

  • Constructs a new Shape instance.

    Parameters

    • path: T_Path

      The main path of the shape, represented as an array of points.

    • holes: T_Path[] = []

      Optional holes, each represented as a path. Defaults to an empty array.

    Returns Shape

    Will throw an error if any hole has fewer than 3 points.

Properties

holes: T_Path[]

An array of holes, where each hole is represented as a path.

path: T_Path

The main path of the shape, defined as an array of points.

Accessors

  • get nFaces(): number
  • Gets the number of faces (edges) in the shape's main path.

    Returns number

Methods

  • Draws the shape on a canvas context, including its holes.

    Parameters

    • context: CanvasRenderingContext2D

      The 2D rendering context of a canvas.

    Returns void

  • Calculates the axis-aligned bounding rectangle of the shape.

    Returns [T_Point, T_Point]

    A tuple of two points: the top-left [minX, minY] and bottom-right [maxX, maxY].

  • Calculates the centroid of the shape's main path.

    Returns T_Point

    The center of the shape as [x, y].

  • Retrieves a specific face (edge) of the shape by index.

    Parameters

    • faceIdx: number

      The index of the face.

    Returns T_Line

    The face as a line [start, end].

  • Scales the shape by a given factor.

    Parameters

    • s: number

      The scale factor.

    Returns void

  • Applies a transformation matrix to the shape's path and holes.

    Parameters

    • matrix: Matrix

      A transformation matrix to apply.

    Returns this

    The transformed shape.