JavaScript Vector Classes for Creative Coding

8th Jul 2013

JavaScript Vector Classes for Creative Coding

I have started a small collection of JavaScript Euclidean Vector classes. These are commonly used in creative programming. I wanted a quick way of adding Vectors in to any project that may need them so they're installable from bower and have a basic set of methods available.

Set up

Currently there is 2 dimensional and 3 dimensional Vectors available. Add references to the files to your project.


You can install these as a bower dependency

$ bower install js-vector-classes --save


This can be used as a RequireJs dependency. The Vector files will return the JavaScript constructor class. You will just need to point the module name to the corrent location of each file.

require(['Vec2'], function(Vec2){ var velocity = new Vec2(10, 5); });


You can start using this vector by creating a new instance:

var v = new Vec2(1, 5), velocity = new Vec2(10, 5), location = new Vec2(250, 100) ;

You can set the properties again using

v.set(x, y)


You have the following methods available to you.

// Maths v.minus(Vec2) v.mult(Vec2) // multiply v.div(Vec2) // divide v.mod(Vec2) // modulus // helpers v.inv() // invert

New Instances

Each of these also have a version which returns a new instance just change the first letter to be uppercase.

v.Plus(Vec2) v.Minus(Vec2) // => etc


You also have a few helper functions


This copies the properties of another vector in to the current vector.


This returns a new instance of the current vector with cloned values


This will return true if the properties of the two vectors are equal


This will return the distance between two vectors.


Feel free to send pull requests and I'll review them as soon as I can.