Vecta
An immutable JavaScript/TypeScript 2D vector library.
;;; // 40 console.logvec; // Vecta { x: 5, y: 10 }
Installation
Install the package using npm:
npm install vecta
Import the package
;
or for javascript:
const Vecta = ;
Features
TypeScript support
The library is created with TypeScript, so native code completion is supported and well-documented.
Immutable
Other great 2D vector libraries exist - but tend to not be immutable. Vecta is designed to be entirely immutable - x and y values cannot be changed.
Without immutability, you get situations like this:
;; vec === vec1; // true (is a Vector { x: 7, y: 13 })
Operators modify the source object instead of creating a copy.
I would expect this to work like primitive mathematics:
;; a === b; // false. a = 5, b = 7
And so Vecta tries to accomplish this:
;; vec === vec1; // falseconsole.logvec; // Vecta { x: 5, y: 10 } console.logvec1; // Vecta { x: 7, y: 13 }
Function chaining
All methods that don't return a scalar (number) result are chainable:
.addnew Vecta2, 3 .addScalar2 .divScalar1, 4 .sub Vecta.random new Vecta-2, 4, new Vecta3, -5 .invert .dotProduct-2, -9; // returns a scalar
Performance
Because a new object is instantiated every time a method is called, performance is going to be worse than other libraries; however, the fact that other libraries change the object's properties means that you will likely be cloning the object several times in your calculations anyways.
All functions have been implemented with simplicity and performance in mind; they attempt cause as little function calls as possible.