LogoPixi’VN
pixi-jsClasses

Class: Polygon

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:37

A class to define a shape via user defined coordinates. Used for creating complex shapes and hit areas with custom points.

Example

// Create polygon from array of points
const polygon1 = new Polygon([
    new Point(0, 0),
    new Point(0, 100),
    new Point(100, 100)
]);

// Create from array of coordinates
const polygon2 = new Polygon([0, 0, 0, 100, 100, 100]);

// Create from sequence of points
const polygon3 = new Polygon(
    new Point(0, 0),
    new Point(0, 100),
    new Point(100, 100)
);

// Create from sequence of coordinates
const polygon4 = new Polygon(0, 0, 0, 100, 100, 100);

// Use as container hit area
container.hitArea = new Polygon([0, 0, 100, 0, 50, 100]);

See

Point For point objects used in construction

Standard

Implements

Constructors

Constructor

> new Polygon(points): Polygon

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:96

Parameters

points

number[] | PointData[]

Returns

Polygon

Constructor

> new Polygon(...points): Polygon

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:97

Parameters

points

number[] | PointData[]

Returns

Polygon

Properties

closePath

> closePath: boolean

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:76

Indicates if the polygon path is closed.

Example

// Create open polygon
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
polygon.closePath = false;

// Check path state
if (polygon.closePath) {
    // Last point connects to first
}

Remarks

  • True by default
  • False after moveTo
  • True after closePath

Default

true

points

> points: number[]

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:56

An array of the points of this polygon stored as a flat array of numbers.

Example

// Access points directly
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
console.log(polygon.points); // [0, 0, 100, 0, 50, 100]

// Modify points
polygon.points[0] = 10; // Move first x coordinate
polygon.points[1] = 10; // Move first y coordinate

Remarks

  • Stored as [x1, y1, x2, y2, ...]
  • Each pair represents a vertex
  • Length is always even
  • Can be modified directly

type

> readonly type: SHAPE_PRIMITIVE

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:95

The type of the object, mainly used to avoid instanceof checks

Example

// 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);
}

Default

'polygon'

See

SHAPE_PRIMITIVE For all shape types

Implementation of

ShapePrimitive.type

Accessors

lastX

Get Signature

> get lastX(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:261

Get the last X coordinate of the polygon.

Example
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.lastX); // 300
See
Returns

number

The x-coordinate of the last vertex


lastY

Get Signature

> get lastY(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:275

Get the last Y coordinate of the polygon.

Example
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.lastY); // 400
See
Returns

number

The y-coordinate of the last vertex


startX

Get Signature

> get startX(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:301

Get the first X coordinate of the polygon.

Example
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.x); // 0
See
Returns

number

The x-coordinate of the first vertex


startY

Get Signature

> get startY(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:315

Get the first Y coordinate of the polygon.

Example
// Basic coordinate access
const polygon = new Polygon([0, 0, 100, 200, 300, 400]);
console.log(polygon.y); // 0
See
Returns

number

The y-coordinate of the first vertex


x

Get Signature

> get x(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:281

Get the last X coordinate of the polygon.

Deprecated

since 8.11.0, use Polygon.lastX instead.

Returns

number

The X coordinate of the shape

Implementation of

ShapePrimitive.x


y

Get Signature

> get y(): number

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:287

Get the last Y coordinate of the polygon.

Deprecated

since 8.11.0, use Polygon.lastY instead.

Returns

number

The Y coordinate of the shape

Implementation of

ShapePrimitive.y

Methods

clone()

> clone(): Polygon

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:159

Creates a clone of this polygon.

Returns

Polygon

A copy of the polygon

Example

// 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

See

Implementation of

ShapePrimitive.clone


contains()

> contains(x, y): boolean

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:175

Checks whether the x and y coordinates passed to this function are contained within this polygon. Uses raycasting algorithm for point-in-polygon testing.

Parameters

x

number

The X coordinate of the point to test

y

number

The Y coordinate of the point to test

Returns

boolean

Whether the x/y coordinates are within this polygon

Example

// Basic containment check
const polygon = new Polygon([0, 0, 100, 0, 50, 100]);
const isInside = polygon.contains(25, 25); // true

See

Implementation of

ShapePrimitive.contains


containsPolygon()

> containsPolygon(polygon): boolean

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:142

Checks if this polygon completely contains another polygon. Used for detecting holes in shapes, like when parsing SVG paths.

Parameters

polygon

Polygon

The polygon to test for containment

Returns

boolean

True if this polygon completely contains the other polygon

Example

// 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

Remarks

  • Uses bounds check for quick rejection
  • Tests all points for containment

See


copyFrom()

> copyFrom(polygon): this

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:231

Copies another polygon to this one.

Parameters

polygon

Polygon

The polygon to copy from

Returns

this

Returns itself

Example

// Basic copying
const source = new Polygon([0, 0, 100, 0, 50, 100]);
const target = new Polygon();
target.copyFrom(source);

See

Implementation of

ShapePrimitive.copyFrom


copyTo()

> copyTo(polygon): Polygon

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:246

Copies this polygon to another one.

Parameters

polygon

Polygon

The polygon to copy to

Returns

Polygon

Returns given parameter

Example

// Basic copying
const source = new Polygon([0, 0, 100, 0, 50, 100]);
const target = new Polygon();
source.copyTo(target);

See

Implementation of

ShapePrimitive.copyTo


getBounds()

> getBounds(out?): Rectangle

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:216

Returns the framing rectangle of the polygon as a Rectangle object.

Parameters

out?

Rectangle

Optional rectangle to store the result

Returns

Rectangle

The framing rectangle

Example

// 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);

See

Implementation of

ShapePrimitive.getBounds


isClockwise()

> isClockwise(): boolean

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:121

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.

Returns

boolean

true if the polygon's points are arranged clockwise, false if counter-clockwise

Example

// 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
}

strokeContains()

> strokeContains(x, y, strokeWidth, alignment?): boolean

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:197

Checks whether the x and y coordinates given are contained within this polygon including the stroke.

Parameters

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

The alignment of the stroke (1 = inner, 0.5 = centered, 0 = outer)

Returns

boolean

Whether the x/y coordinates are within this polygon's stroke

Example

// 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, 1);   // Inside
const centerStroke = polygon.strokeContains(25, 25, 4, 0.5); // Centered
const outerStroke = polygon.strokeContains(25, 25, 4, 0);   // Outside

See

Implementation of

ShapePrimitive.strokeContains


toString()

> toString(): string

Defined in: node_modules/pixi.js/lib/maths/shapes/Polygon.d.ts:247

Returns a string representation of an object.

Returns

string