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 }

Linear Algebra

Name Vec2 Vec3 Vec4
add βœ“ βœ“ βœ“
angleBetween βœ“ βœ“ βœ“
cross βœ“
distance βœ“ βœ“ βœ“
distanceChebyshev βœ“ βœ“ βœ“
distanceManhattan βœ“ βœ“ βœ“
distanceMinkowski βœ“ βœ“ βœ“
distanceSq βœ“ βœ“ βœ“
dot βœ“ βœ“ βœ“
lerp βœ“ βœ“ βœ“
negate βœ“ βœ“ βœ“
normalize βœ“ βœ“ βœ“
project βœ“ βœ“ βœ“
rotateX βœ“
rotateY βœ“
rotateZ βœ“ βœ“
scale βœ“ βœ“ βœ“
subtract βœ“ βœ“ βœ“

Copying & Creating

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

Boolean Conditions

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

Magnitude Manipulation

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

Miscellaneous

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

Getters & Setters

Name Vec2 Vec3 Vec4
angleW βœ“*
angleX βœ“ βœ“* βœ“*
angleY βœ“ βœ“* βœ“*
angleZ βœ“* βœ“*
magnitude βœ“ βœ“ βœ“
magnitudeSq βœ“* βœ“* βœ“*
a (alias for w) βœ“
r (alias for x) βœ“ βœ“ βœ“
g (alias for y) βœ“ βœ“ βœ“
b (alias for z) βœ“ βœ“

*read only.

Shortcuts

Use them to get/set components as/from arrays.

Name Vec2 Vec3 Vec4
xy βœ“
xyz (or alias rgb) βœ“
xyzw (or alias rgba) βœ“

Author

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

License

MIT License.