0.2.2 • Published 1 year ago

gl-vectors v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

gl-vectors

A javascript library that imitates glsl vectors

CI workflow Quality Gate Status License npm version Types Bundle size

How to use

Install the library with npm install gl-vectors and import the classes that you need: import { vec2, vec3 } from "gl-vectors"

Note: All operations are immutable, except assigning vector properties x, y, z, w.

Features

Instantiating vectors from other vectors (or any iterables) similar to glsl, the "new" keyword is optional:

const v1 = vec2(1, 2);
const v2 = vec3(v1, 3);

Performing vector operations

const v1 = vec2(1, 2);
const v2 = vec2(2, 2);
const dot = v1.multiply(2).dot(v2); // 12

Swizzling

import { vec3 } from "gl-vectors/swizzling";

const black = vec3(0, 0, 0);
const transparentRed = vec4(1, black.yz, 0.5);
console.log(transparentRed.toArray()); // [1, 0, 0, 0.5]

List of methods:

  • length (getter)
  • dimension (getter)
  • add
  • subtract
  • multiply
  • divide
  • normalize
  • dot
  • cross
  • distanceTo
  • lerp
  • clamp
  • applyMatrix

Issues

Typescript doesn't like instantiating from an object without the "new" keyword, ex:

const v1 = vec2({ x: 1, y: 2 }); // ts error
const v2 = new vec2({ x: 1, y: 2 }); // no errors