Earcut is a JavaScript library designed for fast and lightweight polygon triangulation, primarily optimized for WebGL applications. The current stable version is 3.0.2. It aims to provide real-time triangulation performance in browsers by prioritizing raw speed and simplicity over guaranteed correctness for highly degenerate or self-intersecting polygons. It implements a modified ear slicing algorithm using z-order curve hashing, capable of handling holes, twisted polygons, degeneracies, and self-intersections. While it doesn't guarantee perfectly correct triangulation for all edge cases, it typically produces acceptable results for practical datasets. It differentiates itself from alternatives by its significant speed advantage, as evidenced by benchmarks, and was originally developed for Mapbox GL. Earcut is a 2D algorithm, and for 3D inputs, it processes data as if projected onto the XY plane, ignoring the Z-component.
npm install earcutVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic polygon triangulation for both simple shapes and those with holes, including how 3D vertex data is handled (Z-component ignored).
Migrate to ES module import syntax (`import earcut from 'earcut';`). If CommonJS compatibility is strictly required, pin to version 2.2.4 or earlier, or use a build tool that handles ESM to CJS transpilation.
If IE11 compatibility is necessary, you must transpile Earcut in your build process (e.g., using Babel) to an older ES target.
When using the UMD bundle in a browser, access the triangulation function via `earcut.default()` instead of `earcut()`.
Ensure your input data is suitable for 2D projection, or if the Z-axis is critical, consider alternative 3D triangulation or meshing libraries.
For applications requiring absolute geometric correctness on all inputs, especially highly pathological ones, consider more robust libraries like `libtess.js`, which implement different algorithms with stronger guarantees.
Update your import statement to `import earcut from 'earcut';` if using an ES module-aware environment, or revert to Earcut v2.2.4 for CommonJS compatibility.
Access the main function via `earcut.default()` for UMD bundles of version 3.0.0 and later.
Verify that `vertices` is a flat array of numbers `[x0, y0, x1, y1, ...]` and `holes` contains valid start indices within the `vertices` array for each inner ring.
No dependency data recorded yet.