MCPcopy Create free account
hub / github.com/melonjs/melonJS / triangulatePath

Method triangulatePath

packages/melonjs/src/geometries/path2d.js:255–287  ·  view source on GitHub ↗

* triangulate the shape defined by this path into an array of triangles * @returns {Point[]} an array of vertices representing the triangulated path or shape

()

Source from the content-addressed store, hash-verified

253 * @returns {Point[]} an array of vertices representing the triangulated path or shape
254 */
255 triangulatePath() {
256 const vertices = this.vertices;
257
258 if (this.isDirty) {
259 const points = this.points;
260 const indices = earcut(
261 points.flatMap((p) => {
262 return [p.x, p.y];
263 }),
264 );
265 const indicesLength = indices.length;
266
267 // pre-allocate vertices if necessary
268 while (vertices.length < indicesLength) {
269 vertices.push(pointPool.get());
270 }
271
272 // calculate all vertices
273 for (let i = 0; i < indicesLength; i++) {
274 const point = points[indices[i]];
275 vertices[i].set(point.x, point.y);
276 }
277
278 // recycle overhead from a previous triangulation
279 while (vertices.length > indicesLength) {
280 pointPool.release(vertices[vertices.length - 1]);
281 vertices.length -= 1;
282 }
283 this.isDirty = false;
284 }
285
286 return vertices;
287 }
288
289 /**
290 * moves the starting point of the current path to the (x, y) coordinates.

Callers 2

strokeMethod · 0.80
path2d.spec.jsFile · 0.80

Calls 5

earcutFunction · 0.90
getMethod · 0.65
releaseMethod · 0.65
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected