vec2-fn

A type-safe set of functions for working with vec2-like objects and arrays.

Usage no npm install needed!

<script type="module">
  import vec2Fn from 'https://cdn.skypack.dev/vec2-fn';
</script>

README

vec2-fn

What's this?

This is a small JS library containing some utility functions for working with vector 2 (like) objects an arrays. The functions are sometimes not pure in the sense that they will modify the vector you provide to them. This allows the reuse of vector object instances and compatibility between different vector class types.

Install

npm i -S vec2-fn

API Docs

API docs can be found here

ES usage:

// Import star
import * as vec2 from "vec2-fn";
vec2.equal({ x: 0, y: 0 }, vec2.ZERO);

// Import named
import { equal, ZERO } from "vec2-fn";
equal({ x: 0, y: 0 }, ZERO);

Node usage:

// Import all
const vec2 = require("vec2-fn");
vec2.equal({ x: 0, y: 0 }, vec2.ZERO);

// Assign named
const { equal, ZERO } = require("vec2-fn");
equal({ x: 0, y: 0 }, ZERO);

Static usage:

Old school method

<script src="./bin/vec2.js"></script>
<script>
    vec2.equal({ x: 0, y: 0 }, vec2.ZERO);
</script>

For static usage, ambient type definitions can optionally be referenced here node_modules/vec2-fn/bin/vec2.d.ts.

Table of contents

Interfaces

Type aliases

Variables

Functions

Type aliases

Vec2Array

Ƭ Vec2Array<T>: T

Vec2-like array object.

Type parameters

Name Type Default
T number[] [number, number]

Defined in: types/vec2-array.ts:2


Vec2Param

Ƭ Vec2Param: Vec2 | Vec2Array

A Vec2 array or object.

Defined in: types/vec2-param.ts:5


Vec2Readonly

Ƭ Vec2Readonly: Readonly<Vec2>

Readonly Vec2

Defined in: types/vec2-readonly.ts:4

Variables

APPROX_THRESHOLD

Const APPROX_THRESHOLD: 1e-8= 0.00000001

Approximate threshold for approx comparison

Defined in: constants/approx-threshold.ts:2


MINUS_ONE

Const MINUS_ONE: Vec2Readonly

Constant readonly Vec2 with both values equalling -1

Defined in: constants/minus-one.ts:4


ONE

Const ONE: Vec2Readonly

Constant readonly Vec2 with both values equalling 1

Defined in: constants/one.ts:4


PRECISION

Const PRECISION: 11= 11

The number of decimal places calculations are made to.

Defined in: constants/precision.ts:2


VEC2_DOWN

Const VEC2_DOWN: Vec2Readonly

Normalized Vec2 pointing down

Defined in: constants/vec2-down.ts:4


VEC2_DOWN_LEFT

Const VEC2_DOWN_LEFT: Vec2Readonly

Normalized Vec2 pointing down-left

Defined in: constants/vec2-down-left.ts:5


VEC2_DOWN_RIGHT

Const VEC2_DOWN_RIGHT: Vec2Readonly

Normalized Vec2 pointing down-right

Defined in: constants/vec2-down-right.ts:5


VEC2_LEFT

Const VEC2_LEFT: Vec2Readonly

Normalized Vec2 pointing left

Defined in: constants/vec2-left.ts:4


VEC2_RIGHT

Const VEC2_RIGHT: Vec2Readonly

Normalized Vec2 pointing right

Defined in: constants/vec2-right.ts:4


VEC2_UP

Const VEC2_UP: Vec2Readonly

Normalized Vec2 pointing up

Defined in: constants/vec2-up.ts:4


VEC2_UP_LEFT

Const VEC2_UP_LEFT: Vec2Readonly

Normalized Vec2 pointing up-left

Defined in: constants/vec2-up-left.ts:5


VEC2_UP_RIGHT

Const VEC2_UP_RIGHT: Vec2Readonly

Normalized Vec2 pointing up-right

Defined in: constants/vec2-up-right.ts:5


ZERO

Const ZERO: Vec2Readonly

Constant readonly Vec2 with both values equalling 0

Defined in: constants/zero.ts:4

Functions

abs

abs<T>(target: T): T

Calculate vec2 absolute value.

example

abs({ x: -1, y: -1 }); // -> { x: 0, y: 0 }

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target vector.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/abs.ts:19

abs<D>(target: Vec2Param, dest?: D): D

Calculate vec2 absolute value.

example

abs({ x: -1, y: -1 }); // -> { x: 0, y: 0 }

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target vector.
dest? D Destination object to store the result.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/abs.ts:34


add

add<T>(target: T, value: Vec2Param | number): T

Add vectors together.

example

add({ x: -1, y: -1 }, { x: 1, y: 1 }); // -> { x: 0, y: 0 }

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target vector.
value Vec2Param | number The vector to add.

Returns: T

  • A reference to the modified target object.

Defined in: fn/add.ts:17

add<D>(target: Vec2Param, value: Vec2Param | number, dest: D): D

Add vectors together.

example

let result = { x: 0, y: 0 };

add({ x: -1, y: -1 }, { x: 1, y: 1 }, result); // -> { x: 0, y: 0 }

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target vector.
value Vec2Param | number The vector to add.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/add.ts:34


angleTo

angleTo(to: Vec2Param, from?: Vec2Param): number

Calculate angle.

Parameters

Name Type Description
to Vec2Param Vector to calculate to.
from Vec2Param Vector to calculate from. Defaults to ZERO.

Returns: number

  • Number angle result.

Defined in: fn/angle-to.ts:14


approx

approx(v1: Vec2Param, v2: Vec2Param, threshold?: number): boolean

Calculate whether two vectors are approximately equal.

Parameters

Name Type Description
v1 Vec2Param First vector for comparison.
v2 Vec2Param Second vector for comparison.
threshold number -

Returns: boolean

  • Whether the vectors are approximately equal or not.

Defined in: fn/approx.ts:15


ceil

ceil<T>(target: T): T

Ceil the values of a vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/ceil.ts:12

ceil<D>(target: Vec2Param, dest: D): D

Ceil the values of a vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination vector.

Defined in: fn/ceil.ts:20


clamp

clamp<T>(target: T, vMin: Vec2Param | number, vMax: Vec2Param | number): T

Clamp a vector by min and max limits.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target object.
vMin Vec2Param | number Minimum vector.
vMax Vec2Param | number Maximum vector.

Returns: T

  • A reference to the modified target object.

Defined in: fn/clamp.ts:14

clamp<D>(target: Vec2Param, vMin: Vec2Param | number, vMax: Vec2Param | number, dest: D): D

Clamp a vector by min and max limits.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target object.
vMin Vec2Param | number Minimum vector.
vMax Vec2Param | number Maximum vector.
dest D Destination object to store the values.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/clamp.ts:24


copy

copy<D>(target: Vec2Param, dest: D): D

Copy one vectors values to another.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target to be copied from.
dest D The destination object.

Returns: D

  • The destination object reference.

Defined in: fn/copy.ts:13


distance

distance(from: Vec2Param, to: Vec2Param): number

Get the distance between two vectors.

Parameters

Name Type Description
from Vec2Param From vector.
to Vec2Param To vector.

Returns: number

  • Number distance result.

Defined in: fn/distance.ts:11


distanceSq

distanceSq(from: Vec2Param, to: Vec2Param): number

Get the squared distance between two vectors.

Parameters

Name Type Description
from Vec2Param From vector.
to Vec2Param To vector.

Returns: number

  • Number distance squared result.

Defined in: fn/distance-sq.ts:13


divide

divide<T>(target: T, value: Vec2Param | number): T

Divide two vectors.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target vector.
value Vec2Param | number The vector to divide by.

Returns: T

  • A reference to the modified target object.

Defined in: fn/divide.ts:11

divide<D>(target: Vec2Param, value: Vec2Param | number, dest: D): D

Divide two vectors.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target vector.
value Vec2Param | number The vector to divide by.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/divide.ts:20


dot

dot(v1: Vec2Param, v2: Vec2Param): number

Calculate the dot product of two vectors.

Parameters

Name Type Description
v1 Vec2Param Vector 1.
v2 Vec2Param = Vector 2.

Returns: number

  • Number result of the dot product.

Defined in: fn/dot.ts:13


dotPerp

dotPerp(v1: Vec2Param, v2: Vec2Param): number

Calculate the perpendicular dot product of two vectors.

Parameters

Name Type Description
v1 Vec2Param Vector 1.
v2 Vec2Param = Vector 2.

Returns: number

  • Number result of the perpendicular dot product.

Defined in: fn/dot-perp.ts:13


equal

equal(target: Vec2Param, value: Vec2Param | number): boolean

Check whether two vectors are exactly equal to system precision.

Parameters

Name Type Description
target Vec2Param Target vector.
value Vec2Param | number Value vector.

Returns: boolean

  • Boolean result of whether the vectors are equal.

Defined in: fn/equal.ts:13


floor

floor<T>(target: T): T

Floor the values of a vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/floor.ts:12

floor<D>(target: Vec2Param, dest: D): D

Floor the values of a vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination vector.

Defined in: fn/floor.ts:20


isOne

isOne(target: Vec2Param): boolean

Check whether a vector is exactly equal to one (1,1).

Parameters

Name Type Description
target Vec2Param Target vector.

Returns: boolean

  • Boolean result of whether the vector is equal to one.

Defined in: fn/is-one.ts:11


isVec2LikeArray

isVec2LikeArray<T>(value: T): value is T extends [number, number] ? T : never

Check whether a value is a vec2-like array.

Type parameters

Name Type
T unknown

Parameters

Name Type Description
value T Input value to check.

Returns: value is T extends [number, number] ? T : never

  • Boolean result of whether the input is a vec2-like array.

Defined in: fn/is-vec2-array.ts:9


isVec2LikeObject

isVec2LikeObject<T>(value: T): value is T extends Vec2 ? T : never

Check whether a value is a vec2-like object.

Type parameters

Name Type
T unknown

Parameters

Name Type Description
value T Input value to check.

Returns: value is T extends Vec2 ? T : never

  • Boolean result of whether the input is a vec2-like object.

Defined in: fn/is-vec2-object.ts:12


isZero

isZero(vec1: Vec2Param): boolean

Check whether a vector is exactly equal to zero (0,0).

Parameters

Name Type
vec1 Vec2Param

Returns: boolean

  • Boolean result of whether the vector is equal to zero.

Defined in: fn/is-zero.ts:11


length

length(vec: Vec2Param): number

Get the length of a vector.

Parameters

Name Type Description
vec Vec2Param The input vector.

Returns: number

  • The number result of the vector length.

Defined in: fn/length.ts:10


lengthSq

lengthSq(vec: Vec2Param): number

Get the squared length of a vector.

Parameters

Name Type Description
vec Vec2Param The input vector.

Returns: number

  • The number result of the squared vector length.

Defined in: fn/length-sq.ts:12


lerp

lerp<T, V>(target: T, value: V, k: number): T

Linear interpolate two vectors.

Type parameters

Name Type
T Vec2Param
V Vec2Param

Parameters

Name Type Description
target T Vector A.
value V Vector B.
k number The amount to interpolate by.

Returns: T

  • A reference to the modified target object.

Defined in: fn/lerp.ts:15

lerp<D>(target: Vec2Param, value: Vec2Param, k: number, dest: D): D

Linear interpolate two vectors.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Vector A.
value Vec2Param Vector B.
k number The amount to interpolate by.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/lerp.ts:25


max

max<D>(dest: D, ...values: (Vec2Param | number)[]): D

Returns the max x and y of a set of vectors.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
dest D -
...values (Vec2Param | number)[] -

Returns: D

Defined in: fn/max.ts:13


min

min<D>(dest: D, ...values: (Vec2Param | number)[]): D

Returns the min x and y of a set of vectors.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
dest D -
...values (Vec2Param | number)[] -

Returns: D

Defined in: fn/min.ts:13


multiply

multiply<T>(target: T, value: Vec2Param | number): T

Multiply two vectors.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.
value Vec2Param | number The vector to multiply by.

Returns: T

  • A reference to the modified target object.

Defined in: fn/multiply.ts:11

multiply<D>(target: Vec2Param, value: Vec2Param | number, dest: D): D

Multiply two vectors.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
value Vec2Param | number The vector to multiply by.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination object.

Defined in: fn/multiply.ts:20


negate

negate<T>(target: T): T

Negate a vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/negate.ts:12

negate<D>(target: Vec2Param, dest: D): D

Negate a vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination vector.

Defined in: fn/negate.ts:20


normalize

normalize<T>(target: T): T

Normalize a vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target can be vec object or array.

Returns: T

  • The modified target object.

Defined in: fn/normalize.ts:13

normalize<D>(target: Vec2Param, dest?: D): D

Normalize a vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param Target can be vec object or array.
dest? D destination object to store the result.

Returns: D

  • A reference to the destination object.

Defined in: fn/normalize.ts:21


rotate

rotate<T>(target: T, angle: number): T

Rotate a vector around zero point.

readonly - A reference to the modified target vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.
angle number The amount of rotation to apply in radians.

Returns: T

Defined in: fn/rotate.ts:14

rotate<D>(target: Vec2Param, angle: number, dest?: D): D

Rotate a vector around zero point.

readonly - A reference to the modified destination vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
angle number The amount of rotation to apply in radians.
dest? D Optional destination object to store the result.

Returns: D

Defined in: fn/rotate.ts:23


round

round<T>(target: T): T

Round the values of a vector.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/round.ts:12

round<D>(target: Vec2Param, dest: D): D

Round the values of a vector.

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
dest D Destination object to store the result.

Returns: D

  • A reference to the modified destination vector.

Defined in: fn/round.ts:20


subtract

subtract<T>(target: T, value: Vec2Param | number): T

Subtract one vector from another.

example

subtract({ x: 1, y: 1 }, { x: 1, y: 1 }); // -> { x: 0, y: 0 }

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T The target vector.
value Vec2Param | number The vector to subtract.

Returns: T

  • A reference to the modified target vector.

Defined in: fn/subtract.ts:17

subtract<D>(target: Vec2Param, value: Vec2Param | number, dest?: D): D

Subtract one vector from another.

example

let result = { x: 0, y: 0 };
subtract({ x: 1, y: 1 }, { x: 1, y: 1 }, result); // -> { x: 0, y: 0 }

Type parameters

Name Type
D Vec2Param

Parameters

Name Type Description
target Vec2Param The target vector.
value Vec2Param | number The vector to subtract.
dest? D Destination object to store the result.

Returns: D

  • A reference to the modified destination vector.

Defined in: fn/subtract.ts:33


toArray

toArray(target: Vec2): Vec2Array

Convert a vec2-like object to an array.

Parameters

Name Type
target Vec2

Returns: Vec2Array

  • Newly created array.

Defined in: fn/to-array.ts:12


toObject

toObject(target: Vec2Array): Vec2

Convert a vec2-like array to an object.

Parameters

Name Type
target Vec2Array

Returns: Vec2

  • Newly created Vec2 object.

Defined in: fn/to-object.ts:12


zero

zero<T>(target: T): T

Set vector to zero.

Type parameters

Name Type
T Vec2Param

Parameters

Name Type Description
target T Target Vec2-like object the values will be written to.

Returns: T

  • A reference to the object the result was written to.

Defined in: fn/zero.ts:12