Skip to main content

Class: Vec2

Defined in: math/vector2.ts:23

Static operations on Vector2. Mutating operations (set, add, subtract, multiply, multiplyComponents, divide, normalize, floorComponents, rotate, perpendicular, negate) mutate their target argument in place and return it, rather than allocating a new Vector2. Read-only operations (magnitude, magnitudeSquared, dot, cross, distanceTo, equals, toString, toFloat32Array) and clone never mutate their arguments.

Constructors​

Constructor​

new Vec2(): Vec2

Returns​

Vec2

Accessors​

down​

Get Signature​

get static down(): Vector2

Defined in: math/vector2.ts:52

A unit vector pointing downward (0, 1). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2


left​

Get Signature​

get static left(): Vector2

Defined in: math/vector2.ts:60

A unit vector pointing left (-1, 0). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2


one​

Get Signature​

get static one(): Vector2

Defined in: math/vector2.ts:36

A vector with components of 1 (1, 1). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2


Get Signature​

get static right(): Vector2

Defined in: math/vector2.ts:68

A unit vector pointing right (1, 0). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2


up​

Get Signature​

get static up(): Vector2

Defined in: math/vector2.ts:44

A unit vector pointing upward (0, -1). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2


zero​

Get Signature​

get static zero(): Vector2

Defined in: math/vector2.ts:28

A zero vector (0, 0). A fresh vector is created on every access, so it's always safe to mutate.

Returns​

Vector2

Methods​

add()​

static add(target, value): Vector2

Defined in: math/vector2.ts:78

Adds another vector into target, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

value​

Vector2

The vector to add.

Returns​

Vector2

target, for chaining.


clone()​

static clone(vector): Vector2

Defined in: math/vector2.ts:189

Creates a deep copy of a vector.

Parameters​

vector​

Vector2

The vector to copy.

Returns​

Vector2

A new Vector2 with the same component values.


cross()​

static cross(a, b): number

Defined in: math/vector2.ts:264

Calculates the 2D (scalar) cross product of two vectors.

Parameters​

a​

Vector2

The first vector.

b​

Vector2

The second vector.

Returns​

number

The scalar cross product of the two vectors.


distanceTo()​

static distanceTo(a, b): number

Defined in: math/vector2.ts:227

Calculates the distance between two vectors.

Parameters​

a​

Vector2

The first vector.

b​

Vector2

The second vector.

Returns​

number

The distance between the two vectors.


divide()​

static divide(target, scalar): Vector2

Defined in: math/vector2.ts:130

Divides target by a scalar value, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

scalar​

number

The scalar value to divide by.

Returns​

Vector2

target, for chaining.


dot()​

static dot(a, b): number

Defined in: math/vector2.ts:254

Calculates the dot product of two vectors.

Parameters​

a​

Vector2

The first vector.

b​

Vector2

The second vector.

Returns​

number

The dot product of the two vectors.


equals()​

static equals(a, b): boolean

Defined in: math/vector2.ts:208

Checks if two vectors are equal.

Parameters​

a​

Vector2

The first vector.

b​

Vector2

The second vector.

Returns​

boolean

True if the vectors have the same components, false otherwise.


floorComponents()​

static floorComponents(target): Vector2

Defined in: math/vector2.ts:177

Rounds target's components down to the nearest integer, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

Returns​

Vector2

target, for chaining.


magnitude()​

static magnitude(vector): number

Defined in: math/vector2.ts:152

Calculates the magnitude (length) of a vector.

Parameters​

vector​

Vector2

The vector.

Returns​

number

The magnitude of the vector.


magnitudeSquared()​

static magnitudeSquared(vector): number

Defined in: math/vector2.ts:143

Calculates the squared magnitude of a vector. This is faster than Vec2.magnitude as it avoids the square root.

Parameters​

vector​

Vector2

The vector.

Returns​

number

The squared magnitude of the vector.


multiply()​

static multiply(target, scalar): Vector2

Defined in: math/vector2.ts:104

Multiplies target by a scalar value, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

scalar​

number

The scalar value to multiply by.

Returns​

Vector2

target, for chaining.


multiplyComponents()​

static multiplyComponents(target, value): Vector2

Defined in: math/vector2.ts:117

Multiplies target's components by another vector's components, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

value​

Vector2

The vector to multiply components with.

Returns​

Vector2

target, for chaining.


negate()​

static negate(target): Vector2

Defined in: math/vector2.ts:288

Negates both of target's components, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

Returns​

Vector2

target, for chaining.


normalize()​

static normalize(target): Vector2

Defined in: math/vector2.ts:162

Normalizes target to unit length in the same direction, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

Returns​

Vector2

target, for chaining.

Throws​

An error if target has zero length, since its direction is undefined.


perpendicular()​

static perpendicular(target): Vector2

Defined in: math/vector2.ts:274

Rotates target -90 degrees (y, -x) in place, making it perpendicular to its original direction.

Parameters​

target​

Vector2

The vector to mutate.

Returns​

Vector2

target, for chaining.


rotate()​

static rotate(target, angleInRadians): Vector2

Defined in: math/vector2.ts:237

Rotates target by a given angle in radians, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

angleInRadians​

number

The angle in radians to rotate the vector.

Returns​

Vector2

target, for chaining.


subtract()​

static subtract(target, value): Vector2

Defined in: math/vector2.ts:91

Subtracts another vector from target, mutating it in place.

Parameters​

target​

Vector2

The vector to mutate.

value​

Vector2

The vector to subtract.

Returns​

Vector2

target, for chaining.


toFloat32Array()​

static toFloat32Array(vector): Float32Array

Defined in: math/vector2.ts:217

Converts a vector to a glsl-compatible float32 array.

Parameters​

vector​

Vector2

The vector.

Returns​

Float32Array

The 2d vector array (e.g. [5, 3] for { x: 5, y: 3 }).


toString()​

static toString(vector): string

Defined in: math/vector2.ts:198

Returns a string representation of a vector.

Parameters​

vector​

Vector2

The vector.

Returns​

string

A string in the format "(x, y)" with components rounded to 1 decimal place.