@nextgensoftwares/folio-ink
The pure ink maths of Folio's boards: pressure-aware stroke outlines, simplification, hit-testing, the partial eraser and a compact binary codec. DOM-free, no dependencies on the rest of Folio. The scene model built on it is folio-canvas. Guide: Boards.
ts
import { simplifyPoints, strokeOutline, outlineToSvgPath } from '@nextgensoftwares/folio-ink';
const points = simplifyPoints(raw, 0.3 / zoom); // x, y, pressure, t per point
const d = outlineToSvgPath(strokeOutline({ style, points, pressure: true }));Points and styles
| Export | Description |
|---|---|
InkPoints, POINT_STRIDE | a flat number list, POINT_STRIDE (4) numbers per point: x, y, pressure (0..1), time (ms since the stroke began) |
InkStyle, InkTool | { tool: 'pen' | 'highlighter', color, size, opacity? } |
InkDevice | 'pen' | 'touch' | 'mouse' | 'external': what drew a stroke |
PressureCurve, DEFAULT_PRESSURE, mapPressure(raw, curve?), smoothPressure(prev, next, curve?) | how raw stylus pressure becomes width: gamma, floor and smoothing |
Outlines
| Export | Description |
|---|---|
strokeOutline(input, complete?) | the filled polygon that is the visible ink (OutlineInput: style, points, pressure). Real pressure drives width; without it, width follows speed; highlighters are flat. complete: false for a stroke still being drawn (no end taper). Cached per points array |
Outline | the polygon, a flat x, y list |
strokeOpacity(style) | 0.35 for highlighters, else the style's opacity (default 1) |
outlineToSvgPath(outline) | SVG path data, smoothed through edge midpoints |
traceOutline(outline, sink), PathSink | trace an outline into a canvas context or Path2D |
Geometry
| Export | Description |
|---|---|
Bounds, emptyBounds(), isEmptyBounds(b), unionBounds(a, b), intersects(a, b) | axis-aligned bounds |
pointsBounds(points, pad?) | bounds of a point list |
segmentDistance2(px, py, ax, ay, bx, by) | squared distance from a point to a segment |
pointsHit(points, size, x, y, radius) | whether a stroke passes within radius of a point |
pointsHitAlong(points, size, ax, ay, bx, by, radius) | whether an eraser moving from a to b touches a stroke |
simplifyPoints(points, tolerance) | Ramer-Douglas-Peucker, keeping pressure and time |
eraseFromPoints(points, size, eraser) | the partial eraser: the runs of a stroke that survive a sweep (null when untouched) |
Codec
| Export | Description |
|---|---|
encodeInk(value), decodeInk(bytes) | compact binary for documents and recordings: a JSON header with every stroke's points moved to a blob, delta-coded (x, y at ⅛ unit, pressure one byte, time in ms): about 4 bytes per point against ~40 in JSON |