. .
<--------\ /-------->
<-------\. ./------->
<------\\ //------>
<-----\\. .//----->
<----\\\ ///---->
<---\\\ ///--->
<--\\Y//-->
<-\|/->
V
JavaScript vector library.
A set of classes that provide functionality related to basic linear-algebra, geometry, and more, for 2, 3, and 4-dimensional vectors.
npm i @leodeslf/vec.js
pnpm i @leodeslf/vec.js
yarn add @leodeslf/vec.js
bun i @leodeslf/vec.js
https://unpkg.com/@leodeslf/vec.js
https://cdn.jsdelivr.net/npm/@leodeslf/vec.js
Most methods are available as both instance and static
members. The ones that modify the current vector return this
, thus we can chain method calls. Other than e.g.: vector.x
, vector.y
, and vector.xy
(as an array), vectors are iterable, so it's possible to iterate trough their components using for-of
or spread operator (e.g.: ...vector
).
// E.g.:
import { Vec2 } from '@leodeslf/vec.js';
const position = new Vec2(1, 1.8);
const target = new Vec2(1, 10);
const distance = position.distance(target); // 8.2
const direction = Vec2.subtract(target, position).normalize(); // { x: 0, y: 1 }
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
angleW |
β* | ||
angleX |
β | β* | β* |
angleY |
β | β* | β* |
angleZ |
β* | β* | |
magnitude |
β | β | β |
magnitudeSq * |
β | β | β |
w |
β | ||
x |
β | β | β |
y |
β | β | β |
z |
β | β |
*Property is readonly
.
To set/get components straight as/from arrays.
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
xy |
β | ||
xyz |
β | ||
xyzw |
β |
For 3D and 4D vector components and shortcuts.
(Name) Alias | Vec3 |
Vec4 |
---|---|---|
(w ) a |
β | |
(x ) r |
β | β |
(y ) g |
β | β |
(z ) b |
β | β |
(xyz ) rgb |
β | |
(xyzw ) rgba |
β |
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
add |
β | β | β |
angleBetween |
β | β | β |
cross |
β | ||
distance |
β | β | β |
distanceChebyshev * |
β | β | β |
distanceManhattan * |
β | β | β |
distanceMinkowski * |
β | β | β |
distanceSq |
β | β | β |
dot |
β | β | β |
immutable * |
β | β | β |
lerp * |
β | β | β |
negate |
β | β | β |
normalize |
β | β | β |
project |
β | β | β |
rotateX |
β | ||
rotateY |
β | ||
rotateZ |
β | β | |
scale |
β | β | β |
subtract |
β | β | β |
zero |
β | β | β |
*Static method only.
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
clone |
β | β | β |
copy |
β | β | β |
fromCylindricalCoords * |
β | ||
fromPolarCoords * |
β | ||
fromSphericalCoords * |
β | ||
random |
β | β | β |
*Static method only.
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
satisfyEquality |
β | β | β |
satisfyOpposition |
β | β | β |
isInfinite |
β | β | β |
isNaN |
β | β | β |
isZero |
β | β | β |
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
limitMax |
β | β | β |
limitMin |
β | β | β |
clamp |
β | β | β |
Name | Vec2 |
Vec3 |
Vec4 |
---|---|---|---|
lookAt |
β | β | β |
turnLeft |
β | ||
turnRight |
β |
Copyright (c) Leonardo de S. Leal F., 2018-present.
MIT License.