This library adds several generic vector methods to the Array prototype. These methods assume that the array is a list of numeric components. Most vector methods produce undefined behavior if the array contains non-numeric elements.
This library doesn't play nice with other libaries since it directly modifies Array.prototype. However, this allows for very clean vector syntax:
> [1, 2].add([3,4])
[4,6]
> [1.6, 2.7].round()
[2,3]
This library is meant to be incredibly lightweight. It currently comes in at 246 bytes minified and gzipped.
x()
- Gets first componentx(value)
- Sets first componenty()
- Gets second componenty(value)
- Sets second componentz()
- Gets thirdz(value)
- Sets thirdadd(vector)
- Returns a new vector that is the sum of this vector andvector
sub(vector)
- Returns a new vector that is the difference between this vectorvector
mul(scalar)
- Returns a new vector with each component multiplied byscalar
div(scalar)
- Returns a new vector with each component divided byscalar
dot(vector)
- Returns the dot product of this vector andvector
cross(vector)
- Returns the cross product of this vector andvector
mag()
- Returns the magnitude of this vectormagSq()
- Returns the magnitude squared of this vectordist(vector)
- Returns the distance between this vector andvector
distSq(vector)
- Returns the distance squared between this vector andvector
norm()
- Return the normal (length 1) vectorabs()
- Performs Math.abs() on each componentceil()
- Performs Math.ceil() on each componentfloor()
- Performs Math.floor() on each componentmax(value)
- Performs Math.max() on each componentmin(value)
- Performs Math.min() on each componentround()
- Performs Math.round() on each componentclamp(min, max)
- Clamps each component between the provided values
clamp(value, min, max)
- Clamps avalue
betweenmin
andmax