Vector2js is an open-source project that provide simple 2D vectors for JavaScript applications.
It works on all major browsers + Node.js with 100% test coverage.
To see a live demo with Vectors calculator, please surf with a wider-screen device.
Use the Vectors calculator below to try out Vector2js!
Or enter your own code below:
You can install Vector2js from npm:
npm install vector2js
Or you can just download the repo and include the main JavaScript file from /dist/.
Vector2js exports one main object, Vector. To instanciate a new vector, simply use:
// on nodejs / browserify, require the vector class first:
Or, you can create a new vector from a given angle (its length will be 1 by default):
var Vector = require("vector2js");
// create a new vector
var vec = new Vector(1,1);
// create vector with length of 1 from radian:
Once you created a vector, you can use all the API below.
var vec = Vector.from_radian(0.785398);
// create vector with length of 1 from degree:
var vec = Vector.from_degree(45);
Here are all the API functions of the vector class:
Clone the vector and return the cloned instance.
Return: Cloned Vector.
Get if equal to another vector
Param vector - other vector to compare with.
Return: If Vectors Are Equal.
Set values from another vector. this changes value of self.
Param vector - other vector to get values from.
Return: Self.
Set only the x component from another vector.
Return: Self.
Set only the y component from another vector.
Return: Self.
Convert to a dictionary with {x, y}.
Return: A Dictionary Representation Of The Vector.
Convert to array with x, y.
Return: An Array Representation Of The Vector.
Set values from x, y.
Param x - optional x component to set.
Param y - optional y component to set.
Return: Self.
Clone and flip between x and y values.
Return: Cloned Vector With Flipped X And Y Components.
Flip between x and y values.
Return: Self.
Clone and invert x and y values (like multiply with -1, eg x, y => -x, -y)
Return: Cloned Vector With Inverted Values.
Invert x and y values (like multiply with -1, eg x, y => -x, -y)
Return: Self.
Get the distance from another vector.
Param other - vector to get distance from.
Return: Distance Between Vectors.
Get angle from another vector in radians.
Return: Angle In Radians From This To Other.
Get degrees from another vector in degrees.
Return: Angle In Degrees From This To Other.
Convert this vector to a radian angle.
This is equivalent to doing vector.zero.radiansto(this).
Return: Angle In Radians.
Convert this vector to degree.
This is equivalent to doing vector.zero.degreeto(this).
Return: Angle In Degrees (0-360).
Rotate this vector by a given degree.
Param degrees - degrees to rotate this vector by (can be positive or negative).
Return: Self.
Clone and rotate the vector by a given degree.
Param degrees - degree to rotate this vector by (can be positive or negative).
Return: Cloned Rotated Vector.
Rotate this vector by a given radian.
Param radians - radians to rotate this vector by (can be positive or negative).
Return: Self.
Clone and rotate the vector by a given degree.
Param radians - radians to rotate this vector by (can be positive or negative).
Return: Cloned Rotated Vector.
Calculate the length of this vector (aka magnitude).
Return: Vector Length.
Normalize this vector, eg make length equal 1.
Return: Self.
Clone and normalize the vector.
Return: Normalized Vector.
Add other vector to self.
For example, v(10, 11) + v(5, 6) = v(15, 17).
Param other - vector to add components to self.
Return: Self.
Subtract other vector from self.
For example, v(10, 10) - v(2, 3) = v(8, 7).
Param other - vector to subtract components from self.
Return: Self.
Divide self by other vector.
For example, v(10, 20) / v(2, 5) = v(5, 4).
Param other - vector to divide components from self.
Return: Self.
Multiply self vector by other vector.
For example, v(2, 3) * v(3, 4) = v(6, 12).
Param other - vector to multiply components with self.
Return: Self.
Add scalar value to self.
For example, v(2, 3) + 5 = v(7, 8).
Param val - value to add to components.
Return: Self.
Subtract scalar from self.
For example, v(7, 9) - 5 = v(3, 4).
Param val - value to subtract from components.
Return: Self.
Divide self by scalar.
For example, v(6, 8) / 5 = v(3, 4).
Param val - value to divide components by.
Return: Self.
Multiply self by scalar.
For example, v(2, 3) * 2 = v(4, 6).
Param val - value to multiply components with.
Return: Self.
Clone self and add other vector to it.
Param other - vector to add with.
Return: Cloned Vector.
Clone self and subtract other vector from it.
Param other - vector to subtract with.
Return: Cloned Vector.
Clone self and multiply by other vector.
Param other - vector to multiply with.
Return: Cloned Vector.
Clone self and divide by other vector.
Param other - vector to divide with.
Param scalar - value to divide by.
Return: Cloned Vector.
Clone self and add scalar to it.
Param scalar - value to add.
Return: Cloned Vector.
Clone self and substract scalar from it.
Param scalar - value to subtract.
Return: Cloned Vector.
Clone self and multiply by scalar.
Param scalar - value to multiply with.
Return: Cloned Vector.
Clone self and divide by scalar.
Param scalar - value to divide by.
Return: Cloned Vector.
Clamp vector values into range.
Note: this function does not validate that min < max.
Param min - min value for x, y components.
Param max - max value for x, y components.
Return: Self.
Clone vector and clamp its values.
Note: this function does not validate that min < max.
Param min - min value for x, y components.
Param max - max value for x, y components.
Return: Cloned Vector In Range.
Apply a function on x and y components of the vector.
For example, you can use math.round to round the vector x, y values.
Param func - function to apply on components.
Return: Self.
Clone self and apply a function on x and y components of the clone vector.
For example, you can use math.round to round the vector x, y values.
Param func - function to apply on components.
Return: Cloned Vector.
Turn self to absolute values (eg turn x, y positive).
Return: Self.
Clone and turn to absolute values (eg turn x, y positive).
Return: Cloned Vector.
Turn self to round values (eg turn x, y positive).
Return: Self.
Clone and turn to round values (eg turn x, y positive).
Return: Cloned Vector.
Calculate dot-product of this vector with another vector.
Param other - other vector to calculate dot-product with.
Return: Dot Product.
Calculate cross-product of this vector with another vector.
Param other - other vector to calculate cross-product with.
Return: Dot Product.
If any of the components of this vector are nan, null, undefined, etc. set them to defaults.
Note: 0's are considered to be a valid number and will not be replaced with a default value.
Param x - default value for x if undefined (0 if not defined).
Param y - default value for y if undefined (0 if not defined).
Return: Self.
Create a clone and if any of the components of the vector are nan, null, undefined, etc. set them to default.
Note: 0's are considered to be a valid number and will not be replaced with a default value.
Param x - default value for x if undefined (0 if not defined).
Param y - default value for y if undefined (0 if not defined).
Return: Repaired Clone.
Convert to string in the form of "x,y".
Return: String Representation Of The Vector.
Convert to a string with a given format.
Param format - a string in which %x and %y will be replaced with the vector values.
Return: Formatted String Representing The Vector.
Vector2js comes with few useful constant vectors you can use:
Vector.zero = new Vector(0, 0);
Vector.one = new Vector(1, 1);
Vector.up = new Vector(0, -1);
Vector.down = new Vector(0, 1);
Vector.left = new Vector(-1, 0);
Vector.right = new Vector(1, 0);
Vector.upLeft = new Vector(-1, -1);
Vector.downLeft = new Vector(-1, 1);
Vector.upRight = new Vector(1, -1);
Vector.downRight = new Vector(1, 1);
Having trouble with Vector2js? Feel free to report bugs at github issues or contact me at ronenness@gmail.com.