@zakkster/lite-vec
v1.0.0
Published
Zero-GC 2D vector math using the gl-matrix out-parameter pattern. Float32Array-backed, alias-safe, 35+ operations.
Maintainers
Readme
@zakkster/lite-vec
Zero-GC 2D vector math. Float32Array-backed, alias-safe, 35+ operations.
Pre-allocate once. Compute forever. Zero garbage collection.
Why lite-vec?
| Feature | lite-vec | gl-matrix | three.js Vector2 | victor.js | |---|---|---|---|---| | Zero-GC operations | Yes (out param) | Yes | No (returns new) | No | | Alias-safe rotate | Yes | Yes | N/A | N/A | | rotateAround | Yes | No | No | No | | Float32Array backed | Yes | Yes | No (object) | No | | 2D focused | Yes | 2D+3D+4D | 2D+3D | 2D | | Steering-ready | Yes | No | No | No | | Bundle size | < 2KB | ~8KB | ~150KB (full) | ~4KB |
Installation
npm install @zakkster/lite-vecQuick Start
import { vec2 } from '@zakkster/lite-vec';
// Allocate once (this is the only allocation)
const pos = vec2.create(100, 200);
const vel = vec2.create(5, -3);
const gravity = vec2.create(0, 9.8);
// In your 60fps loop — zero allocations:
vec2.scaleAndAdd(vel, vel, gravity, dt); // vel += gravity * dt
vec2.add(pos, pos, vel); // pos += velRecipes
Particle Physics Loop
const pos = vec2.create(), vel = vec2.create(), acc = vec2.create();
vec2.set(acc, 0, 400); // gravity
function update(dt) {
vec2.scaleAndAdd(vel, vel, acc, dt);
vec2.add(pos, pos, vel);
vec2.scale(vel, vel, 0.99); // drag
}Wall Bounce
const normal = vec2.create(1, 0); // left wall normal
if (pos[0] < 0) {
vec2.reflect(vel, vel, normal); // alias-safe
pos[0] = 0;
}Orbit Around Point
const center = vec2.create(400, 300);
// One call — alias-safe even when out === pos
vec2.rotateAround(pos, pos, center, angularSpeed * dt);Steering: Move Toward Target
const target = vec2.create(mouseX, mouseY);
vec2.moveToward(pos, pos, target, maxSpeed * dt);Speed Limit
vec2.clampMag(vel, vel, 0, MAX_SPEED);All 35 Operations
Create: create, set, copy, zero
Arithmetic: add, sub, scale, scaleAndAdd, mul, negate, abs
Compare: min, max, floor, round
Length: mag, magSq, distance, distSq
Normalize: normalize, clampMag
Products: dot, cross
Rotation: rotate, rotateAround, angle, angleBetween, fromAngle, perp
Interpolation: lerp, moveToward
Reflection: reflect, project
Comparison: equals, approxEquals, isZero
Debug: str
License
MIT
