@rasenjs/math
Rasen math library - Vector, Matrix, and Quaternion operations for 2D/3D graphics.
Installation
bash
npm install @rasenjs/math
# or
yarn add @rasenjs/mathFeatures
- Vector Types:
Vec2f,Vec3f,Vec4f - Matrix Types:
Mat3x3f,Mat4x4f - Quaternion:
Quatf - Immutable: All operations return new instances
- Type-safe: Full TypeScript support with strict typing
- Zero dependencies: Pure TypeScript implementation
Usage
Vectors
typescript
import { Vec2f, Vec3f, vec2f, vec3f, addVec2, normalizeVec3 } from '@rasenjs/math'
// Create vectors
const v1 = vec2f(1, 2)
const v2 = new Vec2f(3, 4)
// Operations
const sum = addVec2(v1, v2)
const normalized = normalizeVec3(vec3f(1, 2, 3))
// Access components
console.log(v1.x, v1.y) // 1, 2Matrices
typescript
import { Mat4x4f, mat4x4f, multiplyMat4x4 } from '@rasenjs/math'
// Create identity matrix
const identity = mat4x4f()
// Create transformation matrices
const translation = Mat4x4f.translate(10, 20, 0)
const rotation = Mat4x4f.rotateZ(Math.PI / 4)
const scale = Mat4x4f.scale(2, 2, 1)
// Combine transformations
const transform = multiplyMat4x4(translation, multiplyMat4x4(rotation, scale))Quaternions
typescript
import { Quatf, quatf, slerp } from '@rasenjs/math'
// Create quaternions
const q1 = quatf(0, 0, 0, 1) // identity
const q2 = Quatf.fromEuler(Math.PI / 4, 0, 0) // rotation around X
// Interpolate
const result = slerp(q1, q2, 0.5)API Reference
Vec2f
vec2f(x, y)- Create a 2D vectoraddVec2(a, b)- Add two vectorssubtractVec2(a, b)- Subtract vectorsmultiplyVec2(v, s)- Multiply by scalardotVec2(a, b)- Dot productlengthVec2(v)- Vector lengthnormalizeVec2(v)- Normalize vectordistanceVec2(a, b)- Distance between vectors
Vec3f
vec3f(x, y, z)- Create a 3D vectoraddVec3(a, b)- Add two vectorscross(a, b)- Cross productreflectVec3(v, normal)- Reflect vector
Mat4x4f
mat4x4f()- Create identity matrixMat4x4f.translate(x, y, z)- Translation matrixMat4x4f.rotateX(angle)- Rotation around X axisMat4x4f.rotateY(angle)- Rotation around Y axisMat4x4f.rotateZ(angle)- Rotation around Z axisMat4x4f.scale(x, y, z)- Scale matrixmultiplyMat4x4(a, b)- Multiply matricesinverseMat4x4(m)- Inverse matrix
License
MIT