* triangulate the shape defined by this path into an array of triangles * @returns {Point[]} an array of vertices representing the triangulated path or shape
()
| 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. |