@hex-engine/2d
@hex-engine/2d is the main package that you will interact with when using Hex Engine. It has several named exports, which are each documented here.
NOTE:
@hex-engine/2dalso re-exports everything from@hex-engine/core, but those exports are not documented here. Check the API documentation for@hex-engine/coreif you can't find what you're looking for here.
Models
@hex-engine/2d includes several "Models", which are classes representing common data structures that are used throughout the library.
Circle
Available since version: 0.0.1
import { Circle } from "@hex-engine/2d";
Represents a circle; a shape with infinite points along its edge that are all equidistant from its center.
The distance between the center and the edge points is known as the circle's radius.
Static Methods
constructor
Available since version: 0.0.1
constructor(radius: number)
Creates a new Circle.
Properties
radius
Available since version: 0.0.1
radius: number
The radius of this circle; the length of a line segment that starts at the circle's center and goes to its edge.
This property is readonly in versions prior to 0.1.0.
diameter
Available since version: 0.0.1
diameter: number
The diameter of this circle; the length of a line segment that starts at the circle's edge, crosses through the circle's center, and continues to the opposite edge.
This property is readonly in versions prior to 0.1.0.
width
Available since version: 0.0.1
width: number
The width of this circle; same as the diameter.
This property is readonly in versions prior to 0.1.0.
height
Available since version: 0.0.1
height: number
The height of this circle; same as the diameter.
This property is readonly in versions prior to 0.1.0.
Methods
boundingRectangle
Available since version: 0.0.1
boundingRectangle(): Polygon
Creates a rectangular polygon whose width and height are double this circle's radius; said in other words, returns the rectangle that this circle could be perfectly inscribed in.
containsPoint
Available since version: 0.0.1
containsPoint(point: Vector): boolean
Returns a value indicating if a given point is either within the circle or on the its edge.
equals
Available since version: 0.0.1
equals(other: Circle): boolean
Returns whether this circle has the same radius as another.
draw
Available since version: 0.0.1
draw(context: CanvasRenderingContext2D, strokeOrFill: "stroke" | "fill", { x = 0, y = 0 }: { x?: number | undefined; y?: number | undefined } = {}): void
Draws this circle onto a canvas context, using the current stroke or fill style.
Grid
Available since version: 0.0.0
class Grid<T>
import { Grid } from "@hex-engine/2d";
Represents a two-dimensional Grid with arbitrary contents in each cell.
Static Methods
constructor
Available since version: 0.0.0
constructor(rows: number, columns: number, defaultValue: T)
constructor(rowsAndCols: Vector, defaultValue: T)
Creates a new Grid.
Properties
size
Available since version: 0.0.0
size: Vector
The size of the grid, in rows and columns.
defaultValue
Available since version: 0.0.0
defaultValue: T
The default value to initialize empty cells with.
Methods
setData
Available since version: 0.0.0
setData(data: Array<T>): void
Fill in the grid with the provided data, represented as a 2D array.
get
Available since version: 0.0.0
get(row: number, column: number): T
get(pos: Vector): T
Get the value in the cell at the given row and column index.
Throws if the index is outside the grid, the same as set does.
set
Available since version: 0.0.0
set(row: number, column: number, value: T): void
set(pos: Vector, value: T): void
Set the value in the cell at the given row and column index.
contents
Available since version: 0.0.0
*contents(): Generator<[number, number, T]>
Returns an iterable of all the contents of this grid and their row and column indices.
Vector
Available since version: 0.0.1
import { Vector } from "@hex-engine/2d";
A two-dimensional vector, used to represent points, sizes, and more.
In versions prior to 0.0.1, this class was called
Vec2. In versions >= 0.0.1 but prior to 0.2.0, this class was calledPoint.
Static Properties
ZERO
Available since version: 0.11.2
ZERO: Vector
A Vector with x and y of 0. Do NOT mutate this.
Static Methods
constructor
Available since version: 0.0.0
constructor(x: number, y: number)
Creates a new Vector.
from
Available since version: 0.0.1
static from({ x, y }: { x: number; y: number }): Vector
Create a Vector from any object with an x property and a y property.
fromAngleAndMagnitude
Available since version: 0.2.0
static fromAngleAndMagnitude(angle: number, magnitude: number): Vector
Create a Vector from an angle and magnitude.
Properties
x
Available since version: 0.0.0
x: number
y
Available since version: 0.0.0
y: number
angle
Available since version: 0.2.0
angle: number
Angles are expressed as the clockwise distance from the x-axis. This is different from how angles are expressed in normal coordinate space; the reason for this difference is because when drawing to a canvas, the positive y axis points downwards instead of upwards.

magnitude
Available since version: 0.2.0
magnitude: number
The distance of the point (x, y) from the origin (0, 0).
Methods
clone
Available since version: 0.0.1
clone(): Vector
Create a new Vector with the same x and y values as this one.
opposite
Available since version: 0.0.1
opposite(): Vector
Create a new Vector whose x and y values have the opposite sign as this one's.