Simplify.js (License: BSD, GitHub: mourner / simplify-js, npm: simplify-js) is a tiny high-performance JavaScript 2D/3D polyline simplification library by Vladimir Agafonkin, extracted from Leaflet, a JS interactive maps library of the same author. It uses a combination of Douglas-Peucker and Radial Distance algorithms. Works both on browser and server platforms.
Polyline simplification dramatically reduces the number of points in a polyline while retaining its shape, giving a huge performance boost when processing it and also reducing visual noise. For example, it's essential when rendering a 70k-points line chart or a map route in the browser using Canvas or SVG.
This on the other hand is Calvin Metcalf's fork which is async in nature.
points,
simplified with tolerance: px
After simplification: points (~ times less)
Performed in ms
The test data for the example is actually a ~10700 mile car route from Lisboa, Portugal to Singapore on a world scale, generated by the CloudMade Navigation service based on OpenStreetMap data.
simplify(points, tolerance, highQuality, callback)
invokes callback with array of simplified points.
points
[Array]An array of points of [Number, Number]
format.
Configurability of point format would draw a significant performance overhead, so if you use a different format, e.g. {x: Number, y: Number}
, just run search/replace of .x
and .y
through the source of the library to suite it to your application. Similarly, to make it work with 3D points, go through the commented lines at the top of the source.
tolerance
[Number] (optional, 1 by default)Affects the amount of simplification (in the same metric as the point coordinates).
highQuality
[Boolean] (optional, false by default)Excludes distance-based preprocessing step which leads to highest quality simplification but runs ~10-20 times slower.
callback
[Function]Function called with two argument,errer argument and array of simplified points.