Modern modular Bezier curve library for 2D and 3D geometric operations
- π― 2D & 3D Support - Full support for 2D and 3D Bezier curves
- π¦ Modular Architecture - 15 focused modules, all under 300 lines
- π Modern ESM - ES Module first with CommonJS support
- π· TypeScript Support - Full type definitions included
- π§ Comprehensive API - Everything you need for Bezier curve operations
- π Geometric Operations - Normals, curvatures, intersections, projections
- π¨ Offset & Outline - Generate parallel curves and outlines
- β‘ Fast & Lightweight - Only 22KB minified
npm install kirbpnpm add kirbyarn add kirbimport { Bezier } from 'kirb';
// Create a cubic Bezier curve
const curve = new Bezier(0, 0, 100, 25, 200, 75, 300, 100);
// Get a point at t=0.5
const point = curve.get(0.5);
// Get curve length
const length = curve.length();
// Split curve at t=0.5
const { left, right } = curve.split(0.5);
// Get curve normal at t=0.5
const normal = curve.normal(0.5);
// Find extrema
const extrema = curve.extrema();
// Get bounding box
const bbox = curve.bbox();import { Bezier, KirbError, ErrorCodes, type Point, type BoundingBox } from 'kirb';
// Full type safety
const curve = new Bezier(0, 0, 100, 25, 200, 75, 300, 100);
// Type inference
const point: Point = curve.get(0.5);
const bbox: BoundingBox = curve.bbox();
// Error handling with types
try {
curve.offsetPoint(2, 10);
} catch (e) {
if (e instanceof KirbError) {
console.log(e.code); // Typed!
console.log(e.details); // Typed!
}
}// Clearer offset API
const offsetPt = curve.offsetPoint(0.5, 10);
console.log(offsetPt.point); // { x, y }
console.log(offsetPt.normal); // Normal vector
console.log(offsetPt.t); // 0.5
const offsetCurves = curve.offsetCurve(10); // Array of curves
// Better search
const result = curve.findParameter(point, { tolerance: 5 });
if (result) {
console.log(result.t); // Parameter value
console.log(result.hits); // Matching points
}
// Utility methods
const points = curve.sample(20); // 20 evenly spaced points
const closest = curve.closestPoint(point); // Closest point on curve
const info = curve.getInfo(); // Curve metadata
// Check if point is on curve
if (curve.contains(point, { tolerance: 1 })) {
console.log('Point is on curve!');
}get(t)- Get point at position t (0-1)compute(t)- Compute point on curvederivative(t)- Get first derivativenormal(t)- Get normal vectorlength()- Calculate curve lengthbbox()- Get bounding boxextrema()- Find extreme points
offsetPoint(t, distance)- Get offset point at t β¨offsetCurve(distance)- Create offset curve β¨offset(t, d?)- Legacy method (still works)
findParameter(point, options)- Find parameter for point β¨closestPoint(point)- Find closest point on curve β¨contains(point, options)- Check if point is on curve β¨on(point, error)- Legacy method (still works)
sample(count)- Sample n points evenly β¨getInfo()- Get curve metadata β¨
split(t1, t2)- Split curve into segmentsoutline(d1, d2)- Generate curve outlineproject(point)- Project point onto curveintersects(curve)- Find intersections
quadraticFromPoints(p1, p2, p3, t)- Fit quadratic curvecubicFromPoints(S, B, E, t)- Fit cubic curve
curvature(t)- Get curvature at tinflections()- Find inflection pointsarcs(threshold)- Approximate with circular arcsreduce()- Reduce to simple segments
import { Bezier, KirbError, ErrorCodes } from 'kirb';
try {
curve.offsetPoint(2, 10); // Out of range!
} catch (e) {
if (e instanceof KirbError) {
console.log(e.code); // 'OUT_OF_RANGE'
console.log(e.details); // { t: 2, validRange: [0, 1] }
}
}src/
βββ bezier.js # Main export
βββ utils.js # Utility functions
βββ bezier/
β βββ core.js # Core Bezier class
β βββ poly-bezier.js # Multiple curves
β βββ lookup.js # LUT and search
β βββ geometry.js # Geometric operations
β βββ offset.js # Offset & scaling
β βββ intersection.js # Intersection detection
β βββ arcs.js # Arc approximation
βββ utils/
βββ constants.js # Mathematical constants
βββ compute.js # Curve computation
βββ geometry.js # Geometric utilities
βββ roots.js # Root finding
βββ intersection.js # Intersection helpers
βββ shape.js # Shape utilities
- Graphics & Animation - SVG paths, canvas drawing, animations
- Game Development - Smooth trajectories, camera paths
- UI/UX - Custom easing, path morphing
- CAD/CAM - Technical drawing, manufacturing
- Data Visualization - Smooth curve interpolation
For detailed documentation, visit the API Reference.
Based on the comprehensive Bezier Curve Primer.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT Β© jiwonMe
Credits: Built on the foundations of bezier-js by Pomax, modernized with a modular architecture.