Home Reference Source Test
public class | source

Vector

Extends:

Array → Vector

Simple extension to js Array to allow function chaining when doing vector operations. A vector instance can be used the same way as a native Array instance. Note that length will therefore return the number of vector components, and not its scalar value. Use the scalar function if you want to know the vector's length.

Instantiate a vector by its constructor or using one of the factory functions (vec2, vec3...)

Static Method Summary

Static Public Methods
public static

Instantiate a vector from an array.

Member Summary

Public Members
public

[i]: *

assign value

Method Summary

Public Methods
public

add(vectors: ...number[]): Vector

Add one or more vectors to this vector

public

addScaled(vector: number[], factor: number): Vector

Add a scaled version of a vector to this vector

public

Clone/copy vector

public

cross(other: number[], target: Array/Vector): Vector

Find the cross product vector between this vector and the passed in argument.

public

cross2(other: number[]): number

Calculate the psudo cross product between this vector and the passed in argument.

public

distance(other: number[]): number

Calculate the distance between this coordinates to the coordinates supplied in the argument

public

dot(other: number[]): number

Calculate the dot product between this vector and the passed in argument

public

isNull(epsilon: number): *

Test if this vector is a null vector

public

negate(target: Array/Vector): Vector

Negate all components of this vector

public

normalize(target: Array/Vector): Vector

Normalize this vector

public

Calculate the scalar product (length) of this vector.

public

scale(factor: number, target: Array/Vector): Vector

Scale this vector by a factor

public

set(v: ...number): Vector

Assign values to this vector

public

sub(vectors: ...number[]): Vector

Subtract one or more vectors from this vector

public

subScaled(vector: number[], factor: number): Vector

Subtract a scaled version of a vector from this vector

public

swizzle(pattern: string, target: Array/Vector): Vector

Mimic the swizzle feature in glsl.

Static Public Methods

public static fromArray(arr: number[]): Vector source

Instantiate a vector from an array.

Params:

NameTypeAttributeDescription
arr number[]

array of numbers

Return:

Vector

Public Members

public [i]: * source

assign value

Public Methods

public add(vectors: ...number[]): Vector source

Add one or more vectors to this vector

Params:

NameTypeAttributeDescription
vectors ...number[]

vectors to add to this

Return:

Vector

public addScaled(vector: number[], factor: number): Vector source

Add a scaled version of a vector to this vector

Params:

NameTypeAttributeDescription
vector number[]

vectors to add to this

factor number

scaling factor

Return:

Vector

public clone(): Vector source

Clone/copy vector

Return:

Vector

public cross(other: number[], target: Array/Vector): Vector source

Find the cross product vector between this vector and the passed in argument. Only for 3d vectors!

Params:

NameTypeAttributeDescription
other number[]

3d vector

target Array/Vector

optional array/vector to avoid mutating this vector

Return:

Vector

public cross2(other: number[]): number source

Calculate the psudo cross product between this vector and the passed in argument. Only for 2d vectors!

Params:

NameTypeAttributeDescription
other number[]

2d vector

Return:

number

psudo cross product

public distance(other: number[]): number source

Calculate the distance between this coordinates to the coordinates supplied in the argument

Params:

NameTypeAttributeDescription
other number[]

coordinates

Return:

number

distance

public dot(other: number[]): number source

Calculate the dot product between this vector and the passed in argument

Params:

NameTypeAttributeDescription
other number[]

vector

Return:

number

dot product

public isNull(epsilon: number): * source

Test if this vector is a null vector

Params:

NameTypeAttributeDescription
epsilon number

epsilon

Return:

*

public negate(target: Array/Vector): Vector source

Negate all components of this vector

Params:

NameTypeAttributeDescription
target Array/Vector

optional array/vector to avoid mutating this vector

Return:

Vector

public normalize(target: Array/Vector): Vector source

Normalize this vector

Params:

NameTypeAttributeDescription
target Array/Vector

optional array/vector to avoid mutating this vector

Return:

Vector

public scalar(): number source

Calculate the scalar product (length) of this vector.

Return:

number

public scale(factor: number, target: Array/Vector): Vector source

Scale this vector by a factor

Params:

NameTypeAttributeDescription
factor number

scaling factor

target Array/Vector

optional array/vector to avoid mutating this vector

Return:

Vector

public set(v: ...number): Vector source

Assign values to this vector

Params:

NameTypeAttributeDescription
v ...number

values to set

Return:

Vector

public sub(vectors: ...number[]): Vector source

Subtract one or more vectors from this vector

Params:

NameTypeAttributeDescription
vectors ...number[]

vectors to subtract from this vector

Return:

Vector

public subScaled(vector: number[], factor: number): Vector source

Subtract a scaled version of a vector from this vector

Params:

NameTypeAttributeDescription
vector number[]

vector to subtract from this

factor number

scaling factor

Return:

Vector

public swizzle(pattern: string, target: Array/Vector): Vector source

Mimic the swizzle feature in glsl. Supply the swizzle pattern as a string, using the defined accessors (xyzw, rgba, ijkl or stuv): ex: vec.swizzle('xxyw') or vec.swizzle('rrra)

Params:

NameTypeAttributeDescription
pattern string

swizzle pattern

target Array/Vector

optional array/vector to avoid mutating this vector

Return:

Vector