victor.js

A JavaScript 2D vector maths library for Node.js and the browser.

Extra Forky!

This page documents a fork of Victor that is currently maintained.

That means issues are being resolved and pull requests will be merged!

Open Source

Victor is licensed under the MIT license and can be used without restrictions.

Fork Victor on GitHub and help make it better.

Installation

You can use NPM to install Victor.



					

Documentation

Properties

.x

Holds the value for the X component.


						

.y

Holds the value for the Y component.


						

Constructors

Different constructors allow instanciation in most practical way for the situation.

new Victor(x, y)

Can be used without the new keyword.

Params
  • Number x The X component
  • Number y The Y component
Return
  • Victor The new vector

						

Victor.fromArray(array)

Params
  • Array array An array that contains the X component in the first element and the Y component in the second
Return
  • Victor The new vector

						

Victor.fromObject(obj)

Params
  • Object obj An object containing the X component in the x property and the Y component in the y property
Return
  • Victor The new vector

						

Utility

.clone()

Creates a new Victor with the same X and Y components.

Return
  • Victor A new vector

						

.copyX(vector) chainable

Copies the X component of another vector in to itself.

Params
  • Victor vector
Return
  • Victor this

						

.copyY(vector) chainable

Copies the Y component of another vector in to itself.

Params
  • Victor vector
Return
  • Victor this

						

.copy(vector) chainable

Copies the X and Y components of another vector in to itself.

Params
  • Victor vector
Return
  • Victor this

						

.toString()

Returns a string representation of the X and Y components.

Return
  • String

						

.toArray()

Returns an array representation of the X and Y components.

Return
  • Array

						

.toObject()

Returns an object representation of tha X and Y components.

Return
  • Object

						

Manipulation

All manipulation functions are chainable.

.addX(vector) chainable

Adds another vector's X component to itself.

Params
  • Victor vector
Return
  • Victor this

						

.addY(vector) chainable

Adds another vector's Y component to itself.

Params
  • Victor vector
Return
  • Victor this

						

.add(vector) chainable

Adds another vector to itself.

Params
  • Victor vector
Return
  • Victor this

							

.subtractX(vector) chainable

Subtracts another vector's X component from itself.

Params
  • Victor vector
Return
  • Victor this

						

.subtractY(vector) chainable

Subtracts another vector's Y component from itself.

Params
  • Victor vector
Return
  • Victor this

						

.subtract(vector) chainable

Subtracts another vector from itself.

Params
  • Victor vector
Return
  • Victor this

						

.multiplyX(vector) chainable

Multiplies the X component with the X component of another vector.

Params
  • Victor vector
Return
  • Victor this

						

.multiplyY(vector) chainable

Multiplies the Y component with the Y component of another vector.

Params
  • Victor vector
Return
  • Victor this

						

.multiply(vector) chainable

Multiplies both components with another vector.

Params
  • Victor vector
Return
  • Victor this

						

.divideX(vector) chainable

Divides the X component by the X component of another vector.

Params
  • Victor vector
Return
  • Victor this

						

.divideY(scalar) chainable

Divides the Y component by the Y component of another vector.

Params
  • Victor vector
Return
  • Victor this

						

.divide(vector) chainable

Divides both components by another vector.

Params
  • Victor vector
Return
  • Victor this

						

.invertX() chainable

Inverts the X component.

Return
  • Victor this

						

.invertY() chainable

Inverts the Y component.

Return
  • Victor this

						

.invert() chainable

Inverts both components.

Return
  • Victor this

						

.mixX(vector, amount) chainable

Performs a linear blend / interpolation of the X component towards another vector.

Params
  • Victor vector
  • Number amount Value between 0 and 1. Default: 0.5
Return
  • Victor this

						

.mixY(vector, amount) chainable

Performs a linear blend / interpolation of the Y component towards another vector.

Params
  • Victor vector
  • Number amount Value between 0 and 1. Default: 0.5
Return
  • Victor this

						

.mix(vector, amount) chainable

Performs a linear blend / interpolation towards another vector.

Params
  • Victor vector
  • Number amount Value between 0 and 1. Default: 0.5
Return
  • Victor this

						

.normalize() chainable

Normalizes the vector by scaling it down to a length of 1 while keeping its direction.

Return
  • Victor this

.norm() chainable

Alias of normalize.


						

.limit(max, multiplier) chainable

If either component is greater than max, multiplies the component by multiplier.

Params
  • Number max
  • Number multiplier
Return
  • Victor this

						

.unfloat() chainable

Rounds the components to integer numbers.

Return
  • Victor this

						

.rotate(angle) chainable

Rotates the vector by a rotation angle, given in radians CCW from +X axis.

Params
  • Number angle Angle in radians
Return
  • Victor this

.rotateDeg(angle) chainable

Same as rotate but uses degrees.

Params
  • Number angle Angle in degrees
Return
  • Victor this

						

.rotateTo(angle) chainable

Sets the angle of a vector, preserving only its magnitude.

Params
  • Number angle Angle in radians
Return
  • Victor this

.rotateToDeg(angle) chainable

Same as rotateTo but uses degrees.

Params
  • Number angle Angle in degrees
Return
  • Victor this

.rotateBy

This function is deprecated and its behaviour is undefined.

Use rotate or rotateTo instead.

.rotateByDeg

This function is deprecated and its behaviour is undefined.

Use rotateDeg or rotateToDeg instead.

.randomizeX(topLeft, bottomRight) chainable

Randomizes the X component with a value between topLeft and bottomRight.

Params
  • Victor topLeft
  • Victor bottomRight
Return
  • Victor this

						

.randomizeY(topLeft, bottomRight) chainable

Randomizes the Y component with a value between topLeft and bottomRight.

Params
  • Victor topLeft
  • Victor bottomRight
Return
  • Victor this

						

.randomize(topLeft, bottomRight) chainable

Randomizes the components with a value between topLeft and bottomRight.

Params
  • Victor topLeft
  • Victor bottomRight
Return
  • Victor this

							

.randomizeAny(topLeft, bottomRight) chainable

Randomly randomizes either the X component or the Y component with a value between topLeft and bottomRight.

Params
  • Victor topLeft
  • Victor bottomRight
Return
  • Victor this

						

Products

.dot(vector)

Returns the dot product of two vectors.

Params
  • Victor vector
Return
  • Number Dot product

						

.cross(vector)

Returns the cross product of two vectors.

Params
  • Victor vector
Return
  • Number Cross product

						

.length()

Returns the length / magnitude.

Return
  • Number Length

.magnitude()

Alias for length.


						

.setLength(scalar) chainable

Sets the length of the vector to the given value, while keeping it pointing in the same direction.

Params
  • Scalar scalar
Return
  • Victor this

.lengthSq()

Returns the squared length / magnitude. If the length is only needed for comparison, this function is faster than length.

Return
  • Number Squared length

						

.distanceX(vector)

Returns the distance of the X component from another vector.

Params
  • Victor vector
Return
  • Number Distance

.absDistanceX(vector)

Same as distanceX but always returns an absolute value.


						

.distanceY(vector)

Returns the distance of the Y component from another vector.

Params
  • Victor vector
Return
  • Number Distance

.absDistanceY(vector)

Same as distanceY but always returns an absolute value.


						

.distance(vector)

Returns the euclidean distance between two vectors.

Params
  • Victor vector
Return
  • Number Distance

						

.distanceSq(vector)

Returns the squared euclidean distance between two vectors. If the distance is only needed for comparison, this function is faster than distance.

Params
  • Victor vector
Return
  • Number Squared distance

						

.horizontalAngle()

Returns the angle towards X in radians.

Return
  • Number Angle

.angle()

Alias for horizontalAngle.

.direction()

Alias for horizontalAngle.

.horizontalAngleDeg()

Same as horizontalAngle but returns degrees.

Return
  • Number Angle

.angleDeg()

Alias for horizontalAngleDeg.


							

.verticalAngle()

Returns the angle towards Y in radians.

Return
  • Number Angle

.verticalAngleDeg()

Same as verticalAngle but returns degrees.

Return
  • Number Angle

						

Credits

This fork is maintained by Andrei D. Robu.

Contributors

The original project was made by: