Skip to content

paintvec


Class: Vec2

Defined in: index.ts:16

Vec2 represents a 2D vector, point, or size.

Remarks

All operations are immutable and return new Vec2 instances.

Example

js
const a = new Vec2(1, 2)
const b = new Vec2(3, 4)
a.add(b) //=> Vec2(4, 6)
a.sub(b) //=> Vec2(-2, -2)
b.length //=> 5

Constructors

Constructor

new Vec2(x, y): Vec2

Defined in: index.ts:22

Creates a new Vec2 instance.

Parameters

x

number = 0

The x component of this vector

y

number = x

The y component of this vector (defaults to x)

Returns

Vec2

Properties

x

x: number = 0

Defined in: index.ts:23

The x component of this vector


y

y: number = x

Defined in: index.ts:24

The y component of this vector (defaults to x)


zero

readonly static zero: Vec2

Defined in: index.ts:295

Zero vector (0, 0).

Accessors

width

Get Signature

get width(): number

Defined in: index.ts:31

Gets the x component as width.

Returns

number

The x component


height

Get Signature

get height(): number

Defined in: index.ts:39

Gets the y component as height.

Returns

number

The y component


neg

Get Signature

get neg(): Vec2

Defined in: index.ts:109

Gets the negation of this vector.

Returns

Vec2

A new Vec2 with negated components


length

Get Signature

get length(): number

Defined in: index.ts:117

Gets the length (magnitude) of this vector.

Returns

number

The Euclidean length


squaredLength

Get Signature

get squaredLength(): number

Defined in: index.ts:125

Gets the squared length of this vector.

Returns

number

The squared length (useful for comparison without sqrt)


angle

Get Signature

get angle(): number

Defined in: index.ts:133

Gets the angle of this vector from the positive x-axis.

Returns

number

The angle in radians, in the range [-π, π]


floor

Get Signature

get floor(): Vec2

Defined in: index.ts:172

Gets the floor of each component.

Returns

Vec2

A new Vec2 with floored components


ceil

Get Signature

get ceil(): Vec2

Defined in: index.ts:180

Gets the ceiling of each component.

Returns

Vec2

A new Vec2 with ceiled components


round

Get Signature

get round(): Vec2

Defined in: index.ts:188

Gets the rounded value of each component.

Returns

Vec2

A new Vec2 with rounded components


abs

Get Signature

get abs(): Vec2

Defined in: index.ts:196

Gets the absolute value of each component.

Returns

Vec2

A new Vec2 with absolute values


members

Get Signature

get members(): [number, number]

Defined in: index.ts:229

Converts this vector to a tuple array.

Returns

[number, number]

A tuple of [x, y]

Methods

equals()

equals(v): boolean

Defined in: index.ts:48

Checks if this vector equals another vector.

Parameters

v

Vec2

The vector to compare with

Returns

boolean

True if both x and y components are equal


add()

add(v): Vec2

Defined in: index.ts:62

Adds a vector or scalar to this vector.

Parameters

v

Vector or scalar to add

number | Vec2

Returns

Vec2

A new Vec2 with the sum

Example

js
new Vec2(1, 2).add(new Vec2(3, 4)) // Vec2(4, 6)
new Vec2(1, 2).add(5) // Vec2(6, 7)

sub()

sub(v): Vec2

Defined in: index.ts:74

Subtracts a vector or scalar from this vector.

Parameters

v

Vector or scalar to subtract

number | Vec2

Returns

Vec2

A new Vec2 with the difference


mul()

mul(v): Vec2

Defined in: index.ts:86

Multiplies this vector by a vector or scalar.

Parameters

v

Vector or scalar to multiply by

number | Vec2

Returns

Vec2

A new Vec2 with the product


div()

div(v): Vec2

Defined in: index.ts:98

Divides this vector by a vector or scalar.

Parameters

v

Vector or scalar to divide by

number | Vec2

Returns

Vec2

A new Vec2 with the quotient


dot()

dot(other): number

Defined in: index.ts:142

Calculates the dot product with another vector.

Parameters

other

Vec2

The other vector

Returns

number

The dot product


cross()

cross(other): number

Defined in: index.ts:151

Calculates the 2D cross product with another vector.

Parameters

other

Vec2

The other vector

Returns

number

The z-component of the 3D cross product


mix()

mix(other, ratio): Vec2

Defined in: index.ts:161

Linearly interpolates between this vector and another.

Parameters

other

Vec2

The target vector

ratio

number

The interpolation ratio (0 = this, 1 = other)

Returns

Vec2

A new Vec2 with the interpolated value


clamp()

clamp(min, max): Vec2

Defined in: index.ts:206

Clamps each component within the given range.

Parameters

min

Vec2

The minimum values

max

Vec2

The maximum values

Returns

Vec2

A new Vec2 with clamped components


transform()

transform(transform): Vec2

Defined in: index.ts:218

Transforms this vector by a transformation matrix.

Parameters

transform

Transform

The transformation to apply

Returns

Vec2

A new transformed Vec2


toString()

toString(): string

Defined in: index.ts:237

Returns a string representation of this vector.

Returns

string

A string in the format "Vec2(x,y)"


from()

static from(options): Vec2

Defined in: index.ts:252

Creates a Vec2 from various input formats.

Parameters

options

A number (for both components), array, or object with x/y

number | number[] | { x?: number; y?: number; }

Returns

Vec2

A new Vec2 instance

Example

js
Vec2.from(5) // Vec2(5, 5)
Vec2.from([3, 4]) // Vec2(3, 4)
Vec2.from({ x: 1, y: 2 }) // Vec2(1, 2)

min()

static min(...vs): Vec2

Defined in: index.ts:275

Returns a vector with the minimum values from each component.

Parameters

vs

...Vec2[]

Vectors to compare

Returns

Vec2

A new Vec2 with minimum values


max()

static max(...vs): Vec2

Defined in: index.ts:286

Returns a vector with the maximum values from each component.

Parameters

vs

...Vec2[]

Vectors to compare

Returns

Vec2

A new Vec2 with maximum values


normalize()

normalize(): Vec2

Defined in: index.ts:301

Normalizes this vector to unit length.

Returns

Vec2

A new Vec2 with length 1, or zero vector if length is 0


rotate()

rotate(angle): Vec2

Defined in: index.ts:312

Rotates this vector by the given angle.

Parameters

angle

number

The rotation angle in radians

Returns

Vec2

A new rotated Vec2


min()

min(other): Vec2

Defined in: index.ts:326

Returns a vector with the minimum x and y from this and another vector.

Parameters

other

Vec2

The other vector

Returns

Vec2

A new Vec2 with minimum components


max()

max(other): Vec2

Defined in: index.ts:338

Returns a vector with the maximum x and y from this and another vector.

Parameters

other

Vec2

The other vector

Returns

Vec2

A new Vec2 with maximum components


toArray()

toArray(): [number, number]

Defined in: index.ts:349

Converts this vector to an array.

Returns

[number, number]

An array of [x, y]

Released under the MIT License.