{"_id":"earcut@3.2.4","bugs":{"url":"https://github.com/mapbox/earcut/issues"},"dist":{"shasum":"a030635fbb9a639984ce0e319b0f16e17e6e15ad","tarball":"http://registry-npm.ywwl.com/earcut/download/earcut-3.2.4.tgz","fileCount":7,"integrity":"sha512-FHc6yNpnTORB0tM5kmZ03iD2mDuNakDE2lvf4Hw1XkfxtEHIGVi01B/GqUSwx+NcC1WGjidAfpTXzeALtPtfpg==","signatures":[{"sig":"MEQCIF1HGpN0O9uFUwmj5ucCxlcR5ac98EdPuIuZx1qnfJzlAiBuoI6viUwmGgYz7NJ8a3frHpspBqiO6CKLZ5QfGuz2Zg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCYYSpcyKGfsETOn4nucSHmBdaTp7a99xuSOnfi9A7FDwIgVljmRc0SJSvy3NVcJY8/lw7owrUYf6Vfwjl+dcz9zAw="}],"unpackedSize":109815,"size":34818,"noattachment":false,"key":"/earcut/-/earcut-3.2.4.tgz"},"main":"src/earcut.js","name":"earcut","type":"module","types":"src/earcut.d.ts","author":{"name":"Volodymyr Agafonkin"},"exports":"./src/earcut.js","gitHead":"9e70986eb94054c621c926888dd05bc26bffb902","license":"ISC","scripts":{"cov":"node --test --experimental-test-coverage","test":"node --test","bench":"node bench/bench-tiles.js","build":"rollup -c","pretest":"eslint src test/test.js bench/*.js viz/viz.js && tsc","prepublishOnly":"npm test && npm run build"},"version":"3.2.4","_npmUser":{"name":"mourner","email":"agafonkin@gmail.com"},"homepage":"https://github.com/mapbox/earcut#readme","repository":{"url":"git://github.com/mapbox/earcut.git","type":"git"},"_npmVersion":"12.0.2","description":"The fastest and smallest JavaScript polygon triangulation library for your WebGL apps","directories":{},"maintainers":[{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-07","email":"accounts+npmjs-07@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-05","email":"accounts+npmjs-05@mapbox.com"},{"name":"mapbox-npm-06","email":"accounts+npmjs-06@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-advanced-actions","email":"accounts+npmjs-advanced-actions@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-admin","email":"accounts@mapbox.com"},{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mbx-npm-ci-staging","email":"yannick.meeus+npm+staging+ci@mapbox.com"},{"name":"mbx-npm-ci-production","email":"yannick.meeus+npm+production+ci@mapbox.com"},{"name":"mbx-npm-01-production","email":"yannick.meeus+npm+prod+01@mapbox.com"},{"name":"mbx-npm-02-production","email":"yannick.meeus+npm+prod+02@mapbox.com"},{"name":"mbx-npm-03-production","email":"yannick.meeus+npm+prod+03@mapbox.com"},{"name":"mbx-npm-04-production","email":"yannick.meeus+npm+prod+04@mapbox.com"},{"name":"mbx-npm-05-production","email":"yannick.meeus+npm+prod+05@mapbox.com"},{"name":"mbx-npm-06-production","email":"yannick.meeus+npm+prod+06@mapbox.com"},{"name":"mbx-npm-07-production","email":"yannick.meeus+npm+prod+07@mapbox.com"},{"name":"mbx-npm-08-production","email":"yannick.meeus+npm+prod+08@mapbox.com"},{"name":"mbx-npm-09-production","email":"yannick.meeus+npm+prod+09@mapbox.com"},{"name":"mbx-npm-02-staging","email":"yannick.meeus@mapbox.com"},{"name":"mbx-npm-advanced-actions-staging","email":"yannick.meeus+npm+staging+advanced+actions@mapbox.com"},{"name":"mbx-npm-advanced-actions-production","email":"yannick.meeus+npm+prod+advanced+actions@mapbox.com"},{"name":"mourner","email":"agafonkin@gmail.com"}],"sideEffects":false,"_nodeVersion":"24.18.1","allowScripts":{"fsevents@2.3.3":true},"_hasShrinkwrap":false,"devDependencies":{"eslint":"^10.11.0","rollup":"^4.63.4","typescript":"^7.0.2","@rollup/plugin-terser":"^1.0.0","eslint-config-mourner":"^5.0.1"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/earcut_3.2.4_1790588302004_0.3069708286824242"},"readme":"# Earcut\n\nThe fastest and smallest JavaScript **polygon triangulation** library. 4KB gzipped.\n[![Node](https://github.com/mapbox/earcut/actions/workflows/node.yml/badge.svg)](https://github.com/mapbox/earcut/actions/workflows/node.yml)\n[![](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)\n\n![Earcut triangulation example](earcut.png)\n\nEarcut is designed to be fast enough for real-time [triangulation](https://en.wikipedia.org/wiki/Polygon_triangulation) in the browser,\nfavoring raw speed and simplicity over triangulation quality,\nwhile being robust enough to handle most practical datasets without crashing or producing garbage,\nwith an option to refine the result to [Delaunay](https://en.wikipedia.org/wiki/Delaunay_triangulation) quality at a small cost.\nOriginally built for [Mapbox GL JS](https://www.mapbox.com/mapbox-gljs) (WebGL-based interactive maps),\nit's now also used by [Three.js](https://threejs.org/) and many other projects.\n\n## [Demo](https://mapbox.github.io/earcut/viz/)\n\n## Usage\n\n```js\nimport earcut from 'earcut';\nconst triangles = earcut([10,0, 0,50, 60,60, 70,10]); // returns [1,0,3, 1,3,2]\n```\n\n## Algorithm\n\nThe library implements a modified ear slicing algorithm,\noptimized by [z-order curve](https://en.wikipedia.org/wiki/Z-order_curve) and spatial hashing\nand extended to handle holes, twisted polygons, degeneracies and self-intersections\nin a way that doesn't _guarantee_ correctness of triangulation,\nbut attempts to always produce acceptable results for practical data.\n\nIt's based on ideas from\n[FIST: Fast Industrial-Strength Triangulation of Polygons](https://www.cosy.sbg.ac.at/~held/projects/triang/triang.html) by Martin Held\nand [Triangulation by Ear Clipping](https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) by David Eberly.\n\n## Performance\n\nEarcut is heavily optimized for its primary workload — triangulating polygons from\n[Mapbox Vector Tiles](https://github.com/mapbox/vector-tile-spec). On a representative\nbenchmark of **119,680 real-world polygons** (1.9M vertices) drawn from a window of map tiles\nthrough zooms 4–16, it triangulates the whole set in **~445 ms** on a MacBook Pro M1 Pro (2021),\nwith optional Delaunay refinement taking additional **~168 ms**.\nYou can run the benchmark yourself with `npm run bench`.\n\n## Robustness\n\nEarcut does **not** guarantee a correct triangulation on arbitrary input — it trades quality\nfor speed, aiming to always produce an acceptable result on practical data without crashing or\nemitting garbage. The input is assumed to be a valid polygon: rings that don't self-cross or\noverlap, holes that stay inside the outer ring, and no duplicate or zero-length edges. On input\nthat breaks these assumptions, the result can be noticeably wrong — overlapping triangles, gaps,\nor triangles outside the polygon. If correctness matters, clean your input first; for a\nguaranteed-correct triangulation even on bad data, see\n[libtess.js](https://github.com/brendankenny/libtess.js) (slower and larger).\n\nThe output is also not _conforming_ — a vertex may land in the middle of another triangle's edge\n(a T-junction). This is harmless for rendering but can break navmesh or FEM use; if you need a\nconforming mesh, [remove T-junctions in a post-process](https://github.com/mapbox/earcut/issues/74#issuecomment-4826113682).\n\n## Install\n\nInstall with NPM: `npm install earcut`, then import as a module:\n\n```js\nimport earcut, {flatten, deviation, refine} from 'earcut';\n```\n\nOr use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):\n\n```html\n<script type=\"module\">\n    import earcut from 'https://cdn.jsdelivr.net/npm/earcut/+esm';\n</script>\n```\n\nAlternatively, there's a UMD browser bundle with an `earcut` global variable (exposing the main function as `earcut.default`):\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/earcut/dist/earcut.min.js\"></script>\n```\n\n## API\n\n### `earcut(vertices[, holes, dimensions = 2])`\n\n* `vertices` is a flat array of vertex coordinates like `[x0,y0, x1,y1, x2,y2, ...]`.\n* `holes` is an array of hole _indices_ if any\n  (e.g. `[5, 8]` for a 12-vertex input would mean one hole with vertices 5&ndash;7 and another with 8&ndash;11).\n* `dimensions` is the number of coordinates per vertex in the input array (`2` by default). Earcut is a **2D** algorithm: only `x` and `y` are used for triangulation, and any extra coordinates are ignored (3D data is treated as projected onto the XY plane).\n\nEach group of three vertex indices in the resulting array forms a triangle.\n\n```js\n// triangulating a polygon with a hole\nearcut([0,0, 100,0, 100,100, 0,100,  20,20, 80,20, 80,80, 20,80], [4]);\n// [0,4,7, 5,4,0, 5,0,1, 5,1,2, 3,0,7, 3,7,6, 6,5,2, 6,2,3]\n\n// triangulating a polygon with 3d coords\nearcut([10,0,1, 0,50,2, 60,60,3, 70,10,4], null, 3);\n// [1,0,3, 1,3,2]\n```\n\nIf you pass a single vertex as a hole, Earcut treats it as a Steiner point.\n\nOutput triangles always have a consistent **winding order** regardless of the input polygon's winding &mdash; counter-clockwise in a y-up coordinate system (clockwise in y-down/screen space). If you need the opposite orientation (e.g. for back-face culling or normals in 3D), call `.reverse()` on the result.\n\n### `refine(triangles, vertices[, dimensions = 2])`\n\nIf triangle quality matters, you can run an optional refinement pass after triangulation:\n\n```js\nconst triangles = earcut(vertices, holes, dimensions);\nrefine(triangles, vertices, dimensions);\n```\n\nThis mutates `triangles` in place, legalizing interior edges with Delaunay flips while preserving\nthe polygon boundary and holes. It keeps the same number of triangles and the same index format,\nbut usually removes many skinny triangles and reduces total triangle edge length.\n\nRefinement is a post-process, so it doesn't affect the speed of normal `earcut` calls unless you\nexplicitly call it. It assumes a valid manifold triangulation, such as the output of `earcut`, and\ndoesn't repair invalid polygon input or make the mesh conforming.\n\n### `flatten(data)`\n\nIf your input is a multi-dimensional array (e.g. [GeoJSON Polygon](https://geojson.org/geojson-spec.html#polygon)),\nyou can convert it to the format expected by Earcut with `flatten`:\n\n```js\nconst data = flatten(geojson.geometry.coordinates);\nconst triangles = earcut(data.vertices, data.holes, data.dimensions);\n```\n\n### `deviation(vertices, holes, dimensions, triangles)`\n\nAfter getting a triangulation, you can verify its correctness with `deviation`:\n\n```js\nconst d = deviation(vertices, holes, dimensions, triangles);\n```\n\nReturns the relative difference between the total area of triangles and the area of the input polygon.\n`0` means the triangulation is fully correct.\n\n## Ports to other languages\n\n- [mapbox/earcut.hpp](https://github.com/mapbox/earcut.hpp) (C++11)\n- [JaffaKetchup/dart_earcut](https://github.com/JaffaKetchup/dart_earcut) (Dart)\n- [earcut4j/earcut4j](https://github.com/earcut4j/earcut4j) (Java)\n- [Larpon/earcut](https://github.com/Larpon/earcut) (V)\n- [measuredweighed/SwiftEarcut](https://github.com/measuredweighed/SwiftEarcut) (Swift)\n- [goswinr/Earcut](https://github.com/goswinr/Earcut/) (F# / .NET)\n- [tenyoru/earcut.zig](https://github.com/tenyoru/earcut.zig) (Zig)\n- [MichaCo/Earcut](https://github.com/MichaCo/Earcut) (C# / .NET)\n","publish_time":1790588302091,"_cnpm_publish_time":1790588302091,"dist-tags":{"latest":"3.2.4"}}