0.2.0 • Published 3 years ago

vec2-fn v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

NameTypeDefault
Tnumber[]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*](https://github.com/lloydevans/vec2-fn/blob/main/docs/md/interfaces/vec2.md)\

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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget vector.
dest?DDestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget vector.
valueVec2Param | numberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget vector.
valueVec2Param | numberThe vector to add.
destDDestination 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

NameTypeDescription
toVec2ParamVector to calculate to.
fromVec2ParamVector 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

NameTypeDescription
v1Vec2ParamFirst vector for comparison.
v2Vec2ParamSecond vector for comparison.
thresholdnumber-

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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
destDDestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget object.
vMinVec2Param | numberMinimum vector.
vMaxVec2Param | numberMaximum 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget object.
vMinVec2Param | numberMinimum vector.
vMaxVec2Param | numberMaximum vector.
destDDestination 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget to be copied from.
destDThe 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

NameTypeDescription
fromVec2ParamFrom vector.
toVec2ParamTo 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

NameTypeDescription
fromVec2ParamFrom vector.
toVec2ParamTo 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget vector.
valueVec2Param | numberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget vector.
valueVec2Param | numberThe vector to divide by.
destDDestination 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

NameTypeDescription
v1Vec2ParamVector 1.
v2Vec2Param= 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

NameTypeDescription
v1Vec2ParamVector 1.
v2Vec2Param= 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

NameTypeDescription
targetVec2ParamTarget vector.
valueVec2Param | numberValue 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
destDDestination 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

NameTypeDescription
targetVec2ParamTarget 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

NameType
Tunknown

Parameters

NameTypeDescription
valueTInput 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

NameType
Tunknown

Parameters

NameTypeDescription
valueTInput 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

NameType
vec1Vec2Param

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

NameTypeDescription
vecVec2ParamThe 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

NameTypeDescription
vecVec2ParamThe 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

NameType
TVec2Param
VVec2Param

Parameters

NameTypeDescription
targetTVector A.
valueVVector B.
knumberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamVector A.
valueVec2ParamVector B.
knumberThe amount to interpolate by.
destDDestination 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

NameType
DVec2Param

Parameters

NameTypeDescription
destD-
...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

NameType
DVec2Param

Parameters

NameTypeDescription
destD-
...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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe target vector.
valueVec2Param | numberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
valueVec2Param | numberThe vector to multiply by.
destDDestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
destDDestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamTarget can be vec object or array.
dest?Ddestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe target vector.
anglenumberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
anglenumberThe amount of rotation to apply in radians.
dest?DOptional 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
destDDestination 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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTThe target vector.
valueVec2Param | numberThe 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

NameType
DVec2Param

Parameters

NameTypeDescription
targetVec2ParamThe target vector.
valueVec2Param | numberThe vector to subtract.
dest?DDestination 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

NameType
targetVec2

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

NameType
targetVec2Array

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

NameType
TVec2Param

Parameters

NameTypeDescription
targetTTarget 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