A high-performance TypeScript vector math library providing 2D, 3D, and 4D vector operations with a focus on performance and type safety.
- 🚀 Optimized Performance: Uses
Float64Array
with magnitude caching and private fields - 🛡️ Type Safety: Full TypeScript support with strict typing and readonly options
- 📏 Multiple Distance Metrics: Euclidean, Manhattan, Chebyshev, and Minkowski
- 🔒 Immutability Support: Both mutable and immutable operation modes
- 🎮 Graphics Ready: Homogeneous coordinates and transformation support
- 🧮 Math Features: Comprehensive geometric and arithmetic operations
- ⚡ Memory Efficient: Zero-allocation options for performance-critical code
npm install @fimbul-works/vec
import { Vec2, Vec3, Vec4 } from "@fimbul-works/vec";
// Create and manipulate vectors
const position = new Vec2(100, 200);
const direction = new Vec2(1, 0).rotate(Math.PI / 4);
const movement = Vec2.scale(direction, 5);
position.add(movement);
// 3D graphics with homogeneous coordinates
const point = new Vec4(x, y, z, 1); // Point in 3D space
const vector = new Vec4(dx, dy, dz, 0); // Direction in 3D space
// Reuse vectors to avoid garbage collection
const result = new Vec2();
const temp = new Vec2();
// Chain operations without creating intermediates
position
.add(velocity.scale(deltaTime, temp))
.clamp(0, 100);
// Static operations (immutable)
const sum = Vec2.add(v1, v2);
const dot = Vec3.dot(v1, v2);
const cross = Vec3.cross(v1, v2);
// Method chaining (mutable)
const result = new Vec3(1, 0, 0)
.rotate(angle)
.scale(2)
.normalize();
// Distance calculations
const dist = v1.distance(v2);
const manhattan = v1.distanceManhattan(v2);
MIT License - See LICENSE file for details.
Built with ⚡ by FimbulWorks