Vectory
High performant, DevTools friendly, Crankshaft tolerant 2d vectors
Yet another 2d vector implementation for basic motion
thisvelocitythispositionthisacceleration
with rare methods such as lerp
var prev = 1 2var next = 3 4prev // Vector(2, 3)
and swizzling
var vector = 1 2vectoryx // Vector(2, 1)
with some ES6 support
var vector = 0 0var vec3 = ...vector 0 // [0, 0, 0]
and FP ability
// create vectors from raw data and scale them twicevar data = 1 2 3 4/*, ... */var vectors = data
Table of Contents
Features
- Designed with performance in mind
- Simple - 371 LOC
- Lightweight - 4.5 Kb
- Well tested - 100% code coverage
- Rich api - 33 methods
Install
npm install --save vectory
var position = 0 0
or
var position = 0 0
Usage
Follow this link to read more about vectors' usage in the Daniel Shiffman's article
Support
- Latest Chrome, FF, Safari
- IE 9+
- Node 4+
API
Creation
Addition
-
Vector.add(one, another)
-
Vector.prototype.add(vector)
-
Vector.iadd(one, another)
-
Vector.prototype.iadd(vector)
Substraction
-
Vector.sub(one, another)
-
Vector.prototype.sub(vector)
-
Vector.isub(one, another)
-
Vector.prototype.isub(vector)
Multiplication
-
Vector.mul(scalar, vector)
-
Vector.prototype.mul(scalar)
-
Vector.imul(scalar, vector)
-
Vector.prototype.imul(scalar)
Division
-
Vector.div(scalar, vector)
-
Vector.prototype.div(scalar)
-
Vector.idiv(scalar, vector)
-
Vector.prototype.idiv(scalar)
Linear interpolation
Normalization
-
Vector.normalized(vector)
-
Vector.prototype.normalized()
-
Vector.normalize(vector)
-
Vector.prototype.normalize()
Magnitude
Dot product
Distance
Angle computing
-
Vector.angleOf(vector)
-
Vector.prototype.angleOf()
-
Vector.angleTo(one, another)
-
Vector.prototype.angleTo(vector)
Rotation
-
Vector.rotate(theta, vector)
-
Vector.prototype.rotate(theta)
-
Vector.irotate(theta, vector)
-
Vector.prototype.irotate(theta)
Resetting
-
Vector.reset(one, another)
-
Vector.prototype.reset(vector)
-
Vector.zero(vector)
-
Vector.prototype.zero()
-
Vector.set(x, y, vector)
-
Vector.prototype.set(x, y)
Copy
-
Vector.copy(vector)
(aliasVector.clone(vector)
) -
Vector.prototype.copy()
(aliasVector.prototype.clone(vector)
)
Convertion
-
Vector.toJSON(vector)
-
Vector.prototype.toJSON()
-
Vector.toString(vector)
-
Vector.prototype.toString()
-
Vector.prototype[Symbol.toStringTag]
(Latest Chrome and FF or shim) -
Vector.toArray(vector)
-
Vector.prototype.toArray()
Equality
-
Vector.equals(one, another)
-
Vector.protototype.equals(vector)
-
Vector.compare(one, another)
-
Vector.protototype.compare(vector)
Swizzling
Iterator
-
Vector.prototype[Symbol.iterator]()
(Latest Chrome and FF or shim)
Benchmark
Intel Core i7-7700HQ @ 2.8 GHz, DDR3 16 Gb, node v12.4.0
> vectory@1.2.3 bench D:\Projects\vectory
> node ./bench/index.js
i start benchmark, please wait a bit...
Cteation
√ Vector(x, y) x 861,542,389 ops/sec ±0.69% (94 runs sampled)
√ Vector.from([x, y]) x 844,695,778 ops/sec ±1.22% (93 runs sampled)
√ Vector.fromAngle(angle, magnitude) x 838,933,413 ops/sec ±1.43% (88 runs sampled)
√ Vector.parse(string) x 1,510,940 ops/sec ±2.42% (86 runs sampled)
Addition
√ Vector.add(one, another) x 782,517,391 ops/sec ±1.27% (85 runs sampled)
√ Vector#add(vector) x 781,789,927 ops/sec ±0.88% (87 runs sampled)
√ Vector.iadd(one, another) x 353,879,756 ops/sec ±0.60% (89 runs sampled)
√ Vector#iadd(vector) x 367,269,195 ops/sec ±0.64% (91 runs sampled)
Substraction
√ Vector.sub(one, another) x 822,718,592 ops/sec ±0.50% (95 runs sampled)
√ Vector#sub(vector) x 808,219,928 ops/sec ±0.71% (91 runs sampled)
√ Vector.isub(one, another) x 360,450,634 ops/sec ±0.60% (92 runs sampled)
√ Vector#isub(vector) x 366,154,868 ops/sec ±0.43% (90 runs sampled)
Multiplication
√ Vector.mul(scalar, vector) x 817,965,124 ops/sec ±0.70% (91 runs sampled)
√ Vector#mul(vector) x 808,311,011 ops/sec ±0.60% (94 runs sampled)
√ Vector.imul(scalar, vector) x 361,688,702 ops/sec ±0.47% (90 runs sampled)
√ Vector#imul(vector) x 357,685,548 ops/sec ±0.51% (92 runs sampled)
Division
√ Vector.div(scalar, vector) x 816,988,130 ops/sec ±0.43% (94 runs sampled)
√ Vector#div(vector) x 615,974,995 ops/sec ±21.62% (69 runs sampled)
√ Vector.idiv(scalar, vector) x 120,962,052 ops/sec ±2.20% (88 runs sampled)
√ Vector#idiv(vector) x 118,041,377 ops/sec ±2.95% (86 runs sampled)
Linear Interpolation
√ Vector.lerp(one, another, t) x 776,561,727 ops/sec ±1.07% (90 runs sampled)
√ Vector#lerp(vector, t) x 804,305,035 ops/sec ±0.92% (88 runs sampled)
Normalization
√ Vector.normalized(vector) x 177,230,218 ops/sec ±1.25% (93 runs sampled)
√ Vector#normalized() x 175,674,151 ops/sec ±0.94% (89 runs sampled)
√ Vector.normalize(vector) x 81,142,672 ops/sec ±0.45% (90 runs sampled)
√ Vector#normalize() x 72,733,658 ops/sec ±0.81% (93 runs sampled)
Magnitude
√ Vector.magnitude(vector) x 862,343,311 ops/sec ±0.96% (95 runs sampled)
√ Vector#magnitude() x 861,499,315 ops/sec ±0.88% (91 runs sampled)
Dot Product
√ Vector.dot(one, another) x 861,820,342 ops/sec ±0.83% (94 runs sampled)
√ Vector#dot(vector) x 693,283,756 ops/sec ±19.13% (77 runs sampled)
Distance
√ Vector.distance(one, another) x 140,160,765 ops/sec ±2.67% (86 runs sampled)
√ Vector#distance(vector) x 846,767,936 ops/sec ±0.95% (88 runs sampled)
Angle computing
√ Vector.angleOf(vector) x 861,986,566 ops/sec ±0.68% (89 runs sampled)
√ Vector#angleOf() x 848,238,376 ops/sec ±0.77% (94 runs sampled)
√ Vector.angleTo(one, another) x 768,939,821 ops/sec ±0.69% (90 runs sampled)
√ Vector#angleTo(vector) x 796,718,879 ops/sec ±0.54% (89 runs sampled)
Rotation
√ Vector.rotate(theta, vector) x 831,731,143 ops/sec ±0.96% (91 runs sampled)
√ Vector#rotate(theta) x 869,785,085 ops/sec ±0.42% (97 runs sampled)
√ Vector.irotate(theta, vector) x 233,852,488 ops/sec ±6.66% (81 runs sampled)
√ Vector#irotate(theta) x 118,719,502 ops/sec ±2.55% (89 runs sampled)
Resetting
√ Vector.reset(one, another) x 824,366,918 ops/sec ±1.40% (87 runs sampled)
√ Vector#reset(vector) x 870,499,543 ops/sec ±0.74% (89 runs sampled)
√ Vector.zero(vector) x 864,045,085 ops/sec ±0.91% (94 runs sampled)
√ Vector#zero() x 872,766,394 ops/sec ±0.68% (89 runs sampled)
√ Vector.set(x, y, vector) x 875,117,874 ops/sec ±0.45% (94 runs sampled)
√ Vector#set(vector) x 879,133,310 ops/sec ±0.41% (95 runs sampled)
Copy
√ Vector.copy(vector) x 857,718,468 ops/sec ±0.59% (94 runs sampled)
√ Vector#copy() x 860,378,542 ops/sec ±0.73% (94 runs sampled)
Convertion
√ Vector.toJSON(vector) x 474,395,813 ops/sec ±27.90% (55 runs sampled)
√ Vector#toJSON() x 142,701,054 ops/sec ±2.75% (85 runs sampled)
√ Vector.toString(vector) x 2,119,735 ops/sec ±0.49% (94 runs sampled)
√ Vector#toString() x 2,194,938 ops/sec ±0.41% (96 runs sampled)
√ Vector.toArray(vector) x 861,049,656 ops/sec ±0.72% (94 runs sampled)
√ Vector#toArray() x 851,493,627 ops/sec ±0.57% (91 runs sampled)
Equality
√ Vector.equals(one, another) x 186,961,843 ops/sec ±0.23% (91 runs sampled)
√ Vector#equals(vector) x 182,172,457 ops/sec ±0.58% (93 runs sampled)
√ Vector.compare(one, another) x 857,370,215 ops/sec ±0.70% (94 runs sampled)
√ Vector#compare(vector) x 879,521,174 ops/sec ±0.41% (95 runs sampled)
Swizzling
√ Vector#xx get x 872,462,026 ops/sec ±0.49% (95 runs sampled)
√ Vector#xx set x 851,739,979 ops/sec ±0.82% (92 runs sampled)
√ Vector#xy get x 796,653,570 ops/sec ±18.91% (89 runs sampled)
√ Vector#xy set x 137,242,036 ops/sec ±2.91% (86 runs sampled)
√ Vector#yx get x 869,334,030 ops/sec ±0.48% (94 runs sampled)
√ Vector#yx set x 857,707,815 ops/sec ±0.62% (91 runs sampled)
√ Vector#yy get x 871,330,173 ops/sec ±0.43% (93 runs sampled)
√ Vector#yy set x 877,637,098 ops/sec ±0.40% (94 runs sampled)
Iterator
√ Spread operator x 7,353,650 ops/sec ±1.96% (90 runs sampled)
√ for..of loop x 49,734,482 ops/sec ±0.63% (91 runs sampled)
Development
Command | Description |
---|---|
npm run check |
Check standard code style by snazzy |
npm run build |
Wrap source code in UMD by rollup |
npm run bench |
Run benchmark |
npm run test |
Run tests by tape and compute code coverage by nyc |
npm run min |
Minify code by UglifyJS |