Version v26.11.26 Size 3.1 KB gzip Dependencies Zero dependencies
Why Ink?
Raw pointer samples are jagged, and a good ink line needs stabilization, speed- and pressure-derived widths, a closed outline polygon, and a compact SVG path on top of them. Ink computes all four as pure, deterministic functions: identical samples always produce identical geometry, so strokes are replayable, testable, and server-renderable.
ts
// Before
// Hand-rolled filter math, per-point width heuristics, and manual path assembly
// in every drawing surface, each with its own jitter and nondeterminism.
// After
import { polylineToSvgPath, stabilizeStroke, strokeOutline, strokeWidths } from '@vielzeug/ink';
const stabilized = stabilizeStroke(samples);
const d = polylineToSvgPath(strokeOutline(stabilized, strokeWidths(stabilized, { width: 4 })), {
close: true,
});| Feature | Ink | Ad-hoc smoothing | Sketch libraries |
|---|---|---|---|
| Bundle size | 3.1 KB | n/a | Typically 10–50 kB |
| External dependencies | n/a | Varies | |
| Deterministic pure functions | Rarely | Often randomized | |
| Variable-width outline polygon | Built in | Manual | Rare |
| Runs without a DOM | Varies | Usually browser-only |
Use Ink when a drawing or annotation surface needs reusable stroke geometry: stabilization, smoothing, width profiling, outlines, decimation, or SVG serialization without owning the math.
Consider direct polyline rendering when the input is already clean (vector tools with their own smoothing) and a plain segment path is visually sufficient.
Installation
sh
pnpm add @vielzeug/inksh
npm install @vielzeug/inksh
yarn add @vielzeug/inkQuick Start
ts
import { polylineToSvgPath, stabilizeStroke, strokeOutline, strokeWidths } from '@vielzeug/ink';
// Raw samples from a pointer stream (see @vielzeug/gesture for capture),
// timestamped with PointerEvent.timeStamp.
const samples = [
{ time: 0, x: 0, y: 0 },
{ time: 16, x: 12, y: 3 },
{ time: 32, x: 25, y: -2 },
{ time: 48, x: 40, y: 1 },
];
const stabilized = stabilizeStroke(samples); // causal jitter removal
const widths = strokeWidths(stabilized, { width: 6 }); // speed-derived widths
const ring = strokeOutline(stabilized, widths); // closed variable-width polygon
const d = polylineToSvgPath(ring, { close: true }); // SVG `d` stringFeatures
stabilizeStroke(): causal Streamline-style averaging for live drawing; the stroke ends exactly where the pen liftedsmoothStroke(): deterministic Catmull-Rom resampling after capturestrokeWidths(): real-speed-derived widths (distance / Δtime) with pressure modulation and clamped scalingstrokeOutline(): closed variable-width polygon offset along local normals with capped ends; constant width with a single numbersimplifyStroke(): iterative Ramer-Douglas-Peucker decimation preserving endpointspolylineToSvgPath(): SVGdstrings for open strokes and closed outline rings, with configurable precisionInkError/InkConfigError: typed configuration errors thrown at call time- Determinism: identical input always yields identical output; no randomness, no clocks
- Zero runtime dependencies and no DOM requirement: works in Node, Workers, and SSR