{"id":13534283,"url":"https://github.com/mapbox/earcut","last_synced_at":"2025-05-14T13:00:18.656Z","repository":{"id":25912140,"uuid":"29352975","full_name":"mapbox/earcut","owner":"mapbox","description":"The fastest and smallest JavaScript polygon triangulation library for your WebGL apps","archived":false,"fork":false,"pushed_at":"2025-04-01T20:38:44.000Z","size":725,"stargazers_count":2294,"open_issues_count":24,"forks_count":210,"subscribers_count":174,"default_branch":"main","last_synced_at":"2025-05-07T12:45:09.371Z","etag":null,"topics":["algorithm","computational-geometry","javascript","polygon","tessellation","triangulation"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mapbox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-01-16T15:00:29.000Z","updated_at":"2025-05-04T02:19:48.000Z","dependencies_parsed_at":"2024-11-25T16:04:08.789Z","dependency_job_id":"b4c127aa-975b-4410-97f2-87accaa196f7","html_url":"https://github.com/mapbox/earcut","commit_stats":{"total_commits":240,"total_committers":17,"mean_commits":"14.117647058823529","dds":0.09166666666666667,"last_synced_commit":"32493e1686d4d988b07812bbecad8a1f92014ef1"},"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fearcut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fearcut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fearcut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mapbox%2Fearcut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mapbox","download_url":"https://codeload.github.com/mapbox/earcut/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253102628,"owners_count":21854501,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","computational-geometry","javascript","polygon","tessellation","triangulation"],"created_at":"2024-08-01T07:01:29.565Z","updated_at":"2025-05-14T13:00:18.541Z","avatar_url":"https://github.com/mapbox.png","language":"JavaScript","readme":"## Earcut\n\nThe fastest and smallest JavaScript polygon triangulation library. 3KB gzipped.\n\n[![Node](https://github.com/mapbox/earcut/actions/workflows/node.yml/badge.svg)](https://github.com/mapbox/earcut/actions/workflows/node.yml)\n[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/mapbox/earcut.svg)](http://isitmaintained.com/project/mapbox/earcut \"Average time to resolve an issue\")\n[![Percentage of issues still open](http://isitmaintained.com/badge/open/mapbox/earcut.svg)](http://isitmaintained.com/project/mapbox/earcut \"Percentage of issues still open\")\n[![](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)\n\n#### The algorithm\n\nThe library implements a modified ear slicing algorithm,\noptimized by [z-order curve](http://en.wikipedia.org/wiki/Z-order_curve) 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](http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html) by Martin Held\nand [Triangulation by Ear Clipping](http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) by David Eberly.\n\n#### Why another triangulation library?\n\nThe aim of this project is to create a JS triangulation library\nthat is **fast enough for real-time triangulation in the browser**,\nsacrificing triangulation quality for raw speed and simplicity,\nwhile being robust enough to handle most practical datasets without crashing or producing garbage.\nSome benchmarks using Node 0.12:\n\n(ops/sec)         | pts  | earcut    | libtess  | poly2tri | pnltri    | polyk\n------------------| ---- | --------- | -------- | -------- | --------- | ------\nOSM building      | 15   | _795,935_ | _50,640_ | _61,501_ | _122,966_ | _175,570_\ndude shape        | 94   | _35,658_  | _10,339_ | _8,784_  | _11,172_  | _13,557_\nholed dude shape  | 104  | _28,319_  | _8,883_  | _7,494_  | _2,130_   | n/a\ncomplex OSM water | 2523 | _543_     | _77.54_  | failure  | failure   | n/a\nhuge OSM water    | 5667 | _95_      | _29.30_  | failure  | failure   | n/a\n\nThe original use case it was created for is [Mapbox GL](https://www.mapbox.com/mapbox-gl), WebGL-based interactive maps.\n\nIf you want to get correct triangulation even on very bad data with lots of self-intersections\nand earcut is not precise enough, take a look at [libtess.js](https://github.com/brendankenny/libtess.js).\n\n#### Usage\n\n```js\nconst triangles = earcut([10,0, 0,50, 60,60, 70,10]); // returns [1,0,3, 3,2,1]\n```\n\nSignature: `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\u0026ndash;7 and another with 8\u0026ndash;11).\n* `dimensions` is the number of coordinates per vertex in the input array (`2` by default). Only two are used for triangulation (`x` and `y`), and the rest are ignored.\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// [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]\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, 3,2,1]\n```\n\nIf you pass a single vertex as a hole, Earcut treats it as a Steiner point.\n\nNote that Earcut is a **2D** triangulation algorithm, and handles 3D data as if it was projected onto the XY plane (with Z component ignored).\n\nIf your input is a multi-dimensional array (e.g. [GeoJSON Polygon](http://geojson.org/geojson-spec.html#polygon)),\nyou can convert it to the format expected by Earcut with `earcut.flatten`:\n\n```js\nconst data = earcut.flatten(geojson.geometry.coordinates);\nconst triangles = earcut(data.vertices, data.holes, data.dimensions);\n```\n\nAfter getting a triangulation, you can verify its correctness with `earcut.deviation`:\n\n```js\nconst deviation = earcut.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#### Install\n\nInstall with NPM: `npm install earcut`, then import as a module:\n\n```js\nimport earcut from 'earcut';\n```\n\nOr use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):\n\n```html\n\u003cscript type=\"module\"\u003e\n    import earcut from 'https://cdn.jsdelivr.net/npm/earcut/+esm';\n\u003c/script\u003e\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\u003cscript src=\"https://cdn.jsdelivr.net/npm/earcut/dist/earcut.min.js\"\u003e\u003c/script\u003e\n```\n\n![](https://cloud.githubusercontent.com/assets/25395/5778431/e8ec0c10-9da3-11e4-8d4e-a2ced6a7d2b7.png)\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- [the3deers/earcut-java](https://github.com/the3deers/earcut-java) (Java)\n- [Larpon/earcut](https://github.com/Larpon/earcut) (V)\n- [Cawfree/earcut-j](https://github.com/Cawfree/earcut-j) (Java, outdated)\n- [measuredweighed/SwiftEarcut](https://github.com/measuredweighed/SwiftEarcut) (Swift)\n","funding_links":[],"categories":["JavaScript","\u003e 1K ⭐️","Libraries"],"sub_categories":["JavaScript"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapbox%2Fearcut","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmapbox%2Fearcut","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmapbox%2Fearcut/lists"}