LinearlyLinearly
Home
Guide
API
  • English
  • 日本語
GitHub
Home
Guide
API
  • English
  • 日本語
GitHub
Linearly

Linearly

A collection of utility functions related to linear algebra and graphics programming

API →

Immutable Data Structure

All data types are immutable and readonly.

TypeScript-friendly

The library is fully typed and compatible with TypeScript.

Compatibility with various environments

The API is a superset of glMatrix, GLSL, Unity, and Vex in Houdini.

npm version   npm license   CI test result

Linearly is a library providing a collection of utility functions related to linear algebra and graphics programming. It is deeply inspired by glMatrix, but adopts a functional programming style and immutable data structures.

import {mat2d, vec3} from 'linearly'

// Unlike glMatrix, you don't need to specify a receiver matrix as the first argument.
const p0 = vec2.add([1, 2], [3, 4])
const p1 = vec2.transformMat2d(p0, mat2d.fromRotation(scalar.rad(45)))

const dir = vec3.normalize([2, 1, 3])
const right = vec3.cross(dir, [0, 1, 0])
const c = vec3.scale(right, 3)

// As the values of Linearly are plain 1D arrays,
// you can initialize a vector either way.
const a: vec2 = [1, 2]
const b = vec2.of(1, 2)

// But since vectors and matrices are immutable
// and annotated with readonly flags,
// a mutation such as below is handled as an error in TypeScript.
a[0] = 3
// ^
// Cannot assign to '0' because it is a read-only property.

// Some constants such as mat2d.ident are also readonly
// and defined as frozen arrays (via Object.freeze).
// You can use `clone` to mutate them.
const m = mat2d.clone(mat2d.ident)
m[4] *= 2.0
m[5] = -4.5

The library provides functions inspired by the following sources:

  • glMatrix - add, subtract, scale, normalize, dot, cross, lerp, distance, transformMat*
  • GLSL - step, mix, smoothstep, reflect, refract, faceforward
  • Vex in Houdini - fit, efit, invlerp, degrees, radians
  • Unity - inverseLerp, oneMinus, saturate

Modules

The names of modules are derived from glMatrix.

  • scalar: A single number
  • vec2: 2D vector
  • vec3: 3D vector
  • vec4: 4D vector
  • mat2: 2D linear transformation (rotation, scaling, skewing)
  • mat2d: 2D affine transformation, omitting redundant third rows, which is always set to [0, 0, 1] (translation, rotation, scaling, skewing)
  • mat3: 2D affine transformation (translation, rotation, scaling, skewing)
  • mat4: 3D transformation
  • quat: 3D rotation