Vector2js
Read the docs
Full docs can be found here.
Install
You can install via npm:
npm install vector2js
Or bower:
bower install vector2js
Or simply download the js files from /dist/ in this repo.
Usage
If you are using nodejs or browserify, you can use Vector2js like this:
// include vector2jsvar Vector = ; // create a new vectorvar vec = 10 10;
If you include the JavaScript from /dist/ directly into your web pages, you will have 'Vector' in global scope and you can create new vectors like this:
// create a new vectorvar vec = 10 10;
Basic usage example:
Here's a basic usage example - adding two vectors and print the result length:
var v1 = 5 10;var v2 = 1 1;console> 12529964086141668
Tests & Browser support
Vector2js is fully testes on Node.js, IE6 & above, Edge, Chrome, FireFox and Opera, and have coverage of 100%.
Test in browsers
To test in browsers just open the file /tests/test.html
with your browser of choice.
Test in Node.js
To test in nodejs first install qunit-cli:
npm install qunit-cli
Then you can run the following command from the project root:
qunit-cli tests/test.js
API
The following are all the API functions the vector class has:
chainableclone 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 chainablechangeSelf Set values from another vector this changes value of self Param vector - other vector to get values from Return: Self chainablechangeSelf Set only the x component from another vector Return: Self chainablechangeSelf 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 chainablechangeSelf Set values from x y Param x - optional x component to set Param y - optional y component to set Return: Self chainableclone Clone and flip between x and y values Return: Cloned Vector With Flipped X And Y Components chainablechangeSelf Flip between x and y values Return: Self chainableclone Clone and invert x and y Return: Cloned Vector With Inverted Values chainablechangeSelf Invert x and 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 vectorzero Return: Angle In Radians Convert this vector to degree This is equivalent to doing vectorzero Return: Angle In chainablechangeSelf Rotate this vector by a given degree Param degrees - degrees to rotate this vector Return: Self chainable Clone and rotate the vector by a given degree Param degrees - degree to rotate this vector Return: Cloned Rotated Vector chainablechangeSelf Rotate this vector by a given radian Param radians - radians to rotate this vector Return: Self chainable Clone and rotate the vector by a given degree Param radians - radians to rotate this vector Return: Cloned Rotated Vector length Calculate the length of this Return: Vector Length chainablechangeSelf Normalize this vector eg make length equal 1 Return: Self chainableclone Clone and normalize the vector Return: Normalized Vector chainablechangeSelf Add other vector to self For example + = Param other - vector to add components to self Return: Self chainablechangeSelf Subtract other vector from self For example - = Param other - vector to subtract components from self Return: Self chainablechangeSelf Divide self by other vector For example / = Param other - vector to divide components from self Return: Self chainablechangeSelf Multiply self vector by other vector For example * = Param other - vector to multiply components with self Return: Self chainablechangeSelf Add scalar value to self For example + 5 = Param val - value to add to components Return: Self chainablechangeSelf Subtract scalar from self For example - 5 = Param val - value to subtract from components Return: Self chainablechangeSelf Divide self by scalar For example / 5 = Param val - value to divide components by Return: Self chainablechangeSelf Multiply self by scalar For example * 2 = Param val - value to multiply components with Return: Self chainableclone Clone self and add other vector to it Param other - vector to add with Return: Cloned Vector chainableclone Clone self and subtract other vector from it Param other - vector to subtract with Return: Cloned Vector chainableclone Clone self and multiply by other vector Param other - vector to multiply with Return: Cloned Vector chainableclone Clone self and divide by other vector Param other - vector to divide with Param scalar - value to divide by Return: Cloned Vector chainableclone Clone self and add scalar to it Param scalar - value to add Return: Cloned Vector chainableclone Clone self and substract scalar from it Param scalar - value to subtract Return: Cloned Vector chainableclone Clone self and multiply by scalar Param scalar - value to multiply with Return: Cloned Vector chainableclone Clone self and divide by scalar Param scalar - value to divide by Return: Cloned Vector chainablechangeSelf Clamp vector values into range Note: this [chainable][clone] 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. .applySelf (func) [chainable][changeSelf] 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. .apply (func) [chainable][clone] 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. .absSelf () [chainable][changeSelf] Turn self to absolute values (eg turn x, y positive). Return: Self. .abs () [chainable][clone] Clone and turn to absolute values (eg turn x, y positive). Return: Cloned Vector. .roundSelf () [chainable][changeSelf] Turn self to round values (eg turn x, y positive). Return: Self. .round () [chainable][clone] Clone and turn to round values (eg turn x, y positive). Return: Cloned Vector. .dot (other) Calculate dot-product of this vector with another vector. Param other - other vector to calculate dot-product with. Return: Dot Product. .cross (other) Calculate cross-product of this vector with another vector. Param other - other vector to calculate cross-product with. Return: Dot Product. .repairSelf (x, y) [chainable][changeSelf] 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. .repair (x, y) [chainable][clone] 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. .toString () Convert to string in the form of "x,y". Return: String Representation Of The Vector. .format (format) 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.
Consts
Vector2js comes with few useful constant vectors you can use:
Vectorzero = 0 0; Vectorone = 1 1; Vectorup = 0 -1; Vectordown = 0 1; Vectorleft = -1 0; Vectorright = 1 0; VectorupLeft = -1 -1; VectordownLeft = -1 1; VectorupRight = 1 -1; VectordownRight = 1 1;
License
Vector2js is distributed under the MIT license and is free for any personal or commercial purpose.