Drawing Algorithms
Point simplification, pressure mapping, and stroke smoothing
The @hfu.digital/boardkit-core drawing module provides algorithms for processing raw pointer input into smooth, optimized strokes.
Point Simplification
Reduces the number of points in a stroke while preserving its visual shape. Uses the Ramer-Douglas-Peucker algorithm.
import { simplifyPoints } from '@hfu.digital/boardkit-core';
const simplified = simplifyPoints(rawPoints, tolerance);| Parameter | Type | Description |
|---|---|---|
points | Point[] | Raw input points |
tolerance | number | Distance tolerance for point removal (default: 1.0) |
Higher tolerance values produce fewer points (more simplified), while lower values preserve more detail.
Pressure Mapping
Maps raw stylus pressure values to actual stroke width.
import { pressureToWidth } from '@hfu.digital/boardkit-core';
const width = pressureToWidth(pressure, baseWidth);| Parameter | Type | Description |
|---|---|---|
pressure | number | Raw pressure value (0–1) |
baseWidth | number | Base stroke width in pixels |
minMultiplier | number | Minimum width multiplier (default: 0.3) |
maxMultiplier | number | Maximum width multiplier (default: 1.5) |
Pressure values range from 0.0 to 1.0. The function returns baseWidth * multiplier, where the multiplier is linearly interpolated between minMultiplier and maxMultiplier based on pressure.
Stroke Smoothing
Applies Catmull-Rom spline interpolation to smooth jagged strokes.
import { smoothPoints } from '@hfu.digital/boardkit-core';
const smoothed = smoothPoints(points, tension);| Parameter | Type | Description |
|---|---|---|
points | Point[] | Input points to smooth |
tension | number | Spline tension (0.0 = straight lines, 1.0 = maximum smoothing) |
Pipeline
These algorithms are typically chained in the input pipeline:
Raw PointerEvents → Collect points
→ smoothPoints() (reduce jitter)
→ simplifyPoints() (reduce point count)
→ pressureToWidth() (apply pressure sensitivity)
→ Create StrokeElement