vectory
v1.2.3
Published
High performant, DevTools friendly, Crankshaft tolerant 2d vectors
Downloads
37
Readme
Yet another 2d vector implementation for basic motion
this.velocity.iadd(this.acceleration)
this.position.iadd(this.velocity)
this.acceleration.zero()
with rare methods such as lerp
var prev = new Vector(1, 2)
var next = new Vector(3, 4)
prev.lerp(next, 0.5) // Vector(2, 3)
and swizzling
var vector = new Vector(1 ,2)
vector.yx // Vector(2, 1)
with some ES6 support
var vector = new Vector(0, 0)
var vec3 = [...vector, 0] // [0, 0, 0]
and FP ability
// create vectors from raw data and scale them twice
var data = [[1, 2], [3, 4]/*, ... */]
var vectors = data.map(Vector.from).map(Vector.mul.bind(null, 2))
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
import Vector from 'vectory'
var position = new Vector(0, 0)
or
<script src="https://rawgit.com/broadsw0rd/vectory/master/dist/vectory.min.js"></script>
var position = new Vector(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
- [x]
new Vector(x, y)
- [x]
Vector.from([x, y])
- [x]
Vector.fromAngle(angle, magnitude)
- [x]
Vector.parse(string)
Addition
- [x]
Vector.add(one, another)
- [x]
Vector.prototype.add(vector)
- [x]
Vector.iadd(one, another)
- [x]
Vector.prototype.iadd(vector)
Substraction
- [x]
Vector.sub(one, another)
- [x]
Vector.prototype.sub(vector)
- [x]
Vector.isub(one, another)
- [x]
Vector.prototype.isub(vector)
Multiplication
- [x]
Vector.mul(scalar, vector)
- [x]
Vector.prototype.mul(scalar)
- [x]
Vector.imul(scalar, vector)
- [x]
Vector.prototype.imul(scalar)
Division
- [x]
Vector.div(scalar, vector)
- [x]
Vector.prototype.div(scalar)
- [x]
Vector.idiv(scalar, vector)
- [x]
Vector.prototype.idiv(scalar)
Linear interpolation
Normalization
- [x]
Vector.normalized(vector)
- [x]
Vector.prototype.normalized()
- [x]
Vector.normalize(vector)
- [x]
Vector.prototype.normalize()
Magnitude
Dot product
Distance
Angle computing
- [x]
Vector.angleOf(vector)
- [x]
Vector.prototype.angleOf()
- [x]
Vector.angleTo(one, another)
- [x]
Vector.prototype.angleTo(vector)
Rotation
- [x]
Vector.rotate(theta, vector)
- [x]
Vector.prototype.rotate(theta)
- [x]
Vector.irotate(theta, vector)
- [x]
Vector.prototype.irotate(theta)
Resetting
- [x]
Vector.reset(one, another)
- [x]
Vector.prototype.reset(vector)
- [x]
Vector.zero(vector)
- [x]
Vector.prototype.zero()
- [x]
Vector.set(x, y, vector)
- [x]
Vector.prototype.set(x, y)
Copy
- [x]
Vector.copy(vector)
(aliasVector.clone(vector)
) - [x]
Vector.prototype.copy()
(aliasVector.prototype.clone(vector)
)
Convertion
- [x]
Vector.toJSON(vector)
- [x]
Vector.prototype.toJSON()
- [x]
Vector.toString(vector)
- [x]
Vector.prototype.toString()
- [x]
Vector.prototype[Symbol.toStringTag]
(Latest Chrome and FF or shim) - [x]
Vector.toArray(vector)
- [x]
Vector.prototype.toArray()
Equality
- [x]
Vector.equals(one, another)
- [x]
Vector.protototype.equals(vector)
- [x]
Vector.compare(one, another)
- [x]
Vector.protototype.compare(vector)
Swizzling
Iterator
- [x]
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
> [email protected] 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