Class: Polygon

Polygon

A class to define a shape via user defined coordinates.

Polygon can accept the following different constructor arguments:

  • An array of Point objects
  • An array of coordinate pairs

These can be passed as a single array, or as a sequence of arguments.

import { Polygon } from 'pixi.js';

// create a polygon object from an array of points, or an array of coordinate pairs
const polygon1 = new Polygon([ new Point(0, 0), new Point(0, 100), new Point(100, 100) ]);
const polygon2 = new Polygon([ 0, 0, 0, 100, 100, 100 ]);

// or create a polygon object from a sequence of points, or coordinate pairs
const polygon3 = new Polygon(new Point(0, 0), new Point(0, 100), new Point(100, 100));
const polygon4 = new Polygon(0, 0, 0, 100, 100, 100);

new Polygon (…points)

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. new Polygon(new Point(), new Point(), ...), or the arguments passed can be flat x,y values e.g. new Polygon(x,y, x,y, x,y, ...) where x and y are Numbers.

Implements

Members

closePath boolean

false after moveTo, true after closePath. In all other cases it is true.

lastX number readonly

Get the last X coordinate of the polygon

lastY number readonly

Get the last Y coordinate of the polygon

points number[]

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'

x number readonly

Get the first X coordinate of the polygon

y number readonly

Get the first Y coordinate of the polygon

Methods

clone () Polygon

Creates a clone of this polygon.

Returns:
Type Description
Polygon
  • A copy of the polygon.

contains (x, y) boolean

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
  • Whether the x/y coordinates are within this polygon.

copyFrom (polygon) this

Copies another polygon to this one.

Name Type Description
polygon Polygon

The polygon to copy from.

Returns:
Type Description
this Returns itself.

copyTo (polygon) Polygon

Copies this polygon to another one.

Name Type Description
polygon Polygon

The polygon to copy to.

Returns:
Type Description
Polygon Returns given parameter.

getBounds (out) Rectangle

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

strokeContains (x, y, strokeWidth) boolean

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

Name Type 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

Returns:
Type Description
boolean Whether the x/y coordinates are within this polygon