Skip to content

leodeslf/vec.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Vec.js

version license

            .                 .
    <--------\               /-------->
      <-------\.           ./------->
        <------\\         //------>
          <-----\\.     .//----->
            <----\\\   ///---->
              <---\\\ ///--->
                <--\\Y//-->
                  <-\|/->
                     V

Table of contents

About

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.

Installation

npm i @leodeslf/vec.js
pnpm i @leodeslf/vec.js
yarn add @leodeslf/vec.js
bun i @leodeslf/vec.js

CDNs

https://unpkg.com/@leodeslf/vec.js
https://cdn.jsdelivr.net/npm/@leodeslf/vec.js

Usage

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 }

Properties

Name Vec2 Vec3 Vec4
angleW βœ“*
angleX βœ“ βœ“* βœ“*
angleY βœ“ βœ“* βœ“*
angleZ βœ“* βœ“*
magnitude βœ“ βœ“ βœ“
magnitudeSq* βœ“ βœ“ βœ“
w βœ“
x βœ“ βœ“ βœ“
y βœ“ βœ“ βœ“
z βœ“ βœ“

*Property is readonly.

Shortcuts

To set/get components straight as/from arrays.

Name Vec2 Vec3 Vec4
xy βœ“
xyz βœ“
xyzw βœ“

Alias

For 3D and 4D vector components and shortcuts.

(Name) Alias Vec3 Vec4
(w) a βœ“
(x) r βœ“ βœ“
(y) g βœ“ βœ“
(z) b βœ“ βœ“
(xyz) rgb βœ“
(xyzw) rgba βœ“

Linear Algebra

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.

Copying & Creating

Name Vec2 Vec3 Vec4
clone βœ“ βœ“ βœ“
copy βœ“ βœ“ βœ“
fromCylindricalCoords* βœ“
fromPolarCoords* βœ“
fromSphericalCoords* βœ“
random βœ“ βœ“ βœ“

*Static method only.

Boolean Conditions

Name Vec2 Vec3 Vec4
satisfyEquality βœ“ βœ“ βœ“
satisfyOpposition βœ“ βœ“ βœ“
isInfinite βœ“ βœ“ βœ“
isNaN βœ“ βœ“ βœ“
isZero βœ“ βœ“ βœ“

Magnitude Manipulation

Name Vec2 Vec3 Vec4
limitMax βœ“ βœ“ βœ“
limitMin βœ“ βœ“ βœ“
clamp βœ“ βœ“ βœ“

Miscellaneous

Name Vec2 Vec3 Vec4
lookAt βœ“ βœ“ βœ“
turnLeft βœ“
turnRight βœ“

Author

Copyright (c) Leonardo de S. Leal F., 2018-present.

License

MIT License.