SVG Path Simplification Algorithms in Digital Whiteboards
Digital whiteboards rely on geometric and mathematical algorithms to transform erratic, high-frequency pointer input into clean, performant SVG vector paths. When a user draws on an interactive canvas, input hardware produces hundreds of raw coordinate points per second that are noisy, visually jagged, and computationally expensive to render. To produce clean vector strokes without sacrificing visual fidelity, modern whiteboards implement a combination of point decimation, noise filtering, and parametric curve fitting algorithms.
1. Point Decimation: Ramer-Douglas-Peucker (RDP)
The Ramer-Douglas-Peucker algorithm is the most widely adopted method for reducing the number of points in a polyline.
- How it works: It takes a polyline and a distance threshold parameter (\(\epsilon\)). It draws a line between the first and last points of the stroke and finds the point farthest from this line. If the maximum distance is greater than \(\epsilon\), that point is marked to be kept, and the algorithm recursively splits the line into two sub-segments. If the maximum distance is less than \(\epsilon\), all intermediate points are discarded.
- Why it is used: RDP drastically reduces SVG DOM complexity (often removing 70% to 90% of points) while preserving the overall visual geometry and sharp corners of the stroke.
2. Area-Based Simplification: Visvalingam-Whyatt
While RDP focuses on perpendicular distance, the Visvalingam-Whyatt algorithm simplifies paths based on triangular area.
- How it works: It computes the area of the triangle formed by every set of three consecutive points along the path. The point associated with the smallest triangular area is repeatedly removed, and the areas of adjacent triangles are recalculated. The process stops when all remaining triangles exceed a predefined area threshold or when a target vertex count is reached.
- Why it is used: It excels at retaining the perceived visual weight of a stroke and avoids the harsh, localized flattening that RDP can sometimes introduce on gradual curves.
3. Real-Time Stream Smoothing: Moving Averages and Kalman Filters
Before points are permanently recorded to an SVG path, real-time smoothing is applied during the active drawing event stream.
- Exponential Moving Average (EMA): Computes a weighted average between the current hardware point and previous smoothed points. This dampens hardware micro-jitter without adding noticeable input lag.
- Kalman Filtering: Predicts the next expected position based on velocity and acceleration, effectively filtering out sensor noise from styluses or low-polling touchscreens.
4. Spline and Bézier Curve Fitting: Schneider’s Algorithm
Raw simplified points still result in segmented lines rather than
organic curves. Whiteboards use curve-fitting algorithms to generate
native SVG cubic Bézier paths
(<path d="M... C...">).
- Schneider’s Algorithm (Philip J. Schneider): Fits a sequence of piecewise cubic Bézier curves to a set of digitized points. It estimates tangents at the endpoints, generates control points using least-squares optimization, and measures the maximum deviation error between the original points and the curve. If the error exceeds an acceptable tolerance, the curve is split at the point of maximum error, and the process repeats recursively.
- Catmull-Rom to Cubic Bézier Conversion: Some systems first interpolate points using a Catmull-Rom spline (which naturally passes through all control points) and mathematically map the tangent vectors directly to standard SVG cubic Bézier control points for deterministic, smooth paths.
5. Iterative Subdivision: Chaikin’s Algorithm
For lightweight implementations where high-order polynomial math is too expensive, Chaikin’s corner-cutting algorithm is used.
- How it works: Chaikin’s algorithm takes a rough polygon and generates a smoother one by cutting off each corner. For every line segment, it creates two new points at 25% and 75% along the length and discards the original vertices.
- Why it is used: It is computationally inexpensive and converges rapidly to a smooth B-spline curve after only two or three iterations.
Practical Implementation Pipeline
Modern digital whiteboards typically execute these algorithms in a unified pipeline:
- Capture Phase: Hardware input is captured and smoothed in real time via an exponential moving average.
- Post-Draw Simplification: Once the user releases the pointer, the stroke undergoes RDP or Visvalingam-Whyatt reduction to remove redundant data.
- Curve Generation: The decimated polyline is converted into optimized cubic Bézier segments via Schneider’s algorithm, producing the final clean SVG string.