Skip to content

Repository files navigation

math

math is a collection of math helpers for graphics and simulations.

  • High performance: allocation-free, monomorphic, benchmarked
  • Tiny: mean and lean, tree-shakable, only pay for what you use
  • Portable: interops with WebGL, WebGPU, Wasm, your favourite renderer, more.
  • Data-oriented: data-in, data-out functions over caller-owned data, without owning the data lifecycle.
> npm install math@canary

What's inside

Import Description Contents
math Vectors, quaternions, euler angles & matrices vec2 vec3 vec4 euler quat quat2 mat2 mat2d mat3 mat4 spherical polar EPSILON round equals fade lerp lagrange binomial clamp repeat remap remapClamp DEGREES_TO_RADIANS RADIANS_TO_DEGREES degreesToRadians radiansToDegrees wrapAngle deltaAngle
math/shapes Shape primitives & spatial queries box2 box3 obb3 plane3 sphere circle segment2 polygon2 triangle2 triangle3 raycast3 frustum
math/geometry Geometric algorithms circumcircle decomposePolygon2Quick decomposePolygon2Quality triangulatePolygon2 quickhull2 quickhull3
math/time Easing & spring animation easing spring spring2 spring3 spring4
math/random Seeded random number generators isaac32 isaac64 mulberry32 random
math/noise Perlin, simplex & worley noise, plus fractal helpers perlin2d perlin3d simplex2d simplex3d simplex4d worley2d worley3d fbm ridged billow domainWarp2 domainWarp3 curl2 curl3
math/color Color & colorspace utilities color colorspace hsl
math/ik Inverse kinematics fabrik2 fabrik3

Quick Start

import { type Vec3, vec3 } from 'math';

// math types are plain arrays — the constructors just return literals:
const a: Vec3 = [1, 2, 3]; // a plain-array literal
const b = vec3.fromValues(1, 2, 3); // the same as `a`
const out = vec3.create(); // returns [0, 0, 0]

// functions write into their first argument, so nothing is allocated:
vec3.add(out, a, b); // out = [2, 4, 6]
vec3.normalize(out, out); // aliasing an argument is fine

The library is grouped by domain behind subpath entrypoints. All APIs are highly tree-shakeable, only pay for what you use:

import { mat4, quat, vec3 } from 'math'; // core: vectors, quats, matrices
import { simplex3d } from 'math/noise'; // perlin / simplex noise
import { mulberry32 } from 'math/random'; // seeded rng
import { easing, spring } from 'math/time'; // easings & springs
import { fabrik2, fabrik3 } from 'math/ik'; // inverse kinematics
// also: math/color, math/geometry, math/shapes — import only what you use

Examples

Image
Look At
Image
Fibonacci Sphere
Image
Contains Point
Image
Convex Hull 3D
Image
Convex Hull 2D
Image
Polygon2 Decomposition
Image
Polygon2 Triangulation
Image
Circumcircle
Image
FABRIK 2D
Image
FABRIK 2D Snek
Image
FABRIK 3D
Image
Flow Field
Image
Color Wheel
Image
Ridged Noise Voxel Terrain
Image
Simplex 4D Looping Noise
Image
Simplex 2D Noise Terrain
Image
Quaternion Slerp
Image
Spring
Image
Easing

Documentation

  • API.md — every export with its signature and a one-line description, grouped by module. Flat and greppable, so it's easy to search or hand to an AI coding assistant.
  • Online API docs — the full typedoc reference, with search and cross-links.
  • What's inside — jump straight to a module or namespace.

Acknowledgements:

About

🪶 Math helpers for the rest of us

Resources

Stars

998 stars

Watchers

16 watching

Forks

Releases

Packages

Used by

Contributors

Languages