Skip to content

Instantly share code, notes, and snippets.

@wottpal
Last active February 10, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wottpal/2e33941c546be3b02fabe79d1634cd9c to your computer and use it in GitHub Desktop.
Save wottpal/2e33941c546be3b02fabe79d1634cd9c to your computer and use it in GitHub Desktop.
* numberOfPoints defines amount of points * respects frameWidth * returns array of triangles in the format [ [[x1, y1],[x2, y2],[x3, y3]], ... ]
function triangulateCanvas() {
let points = [], triangles = []
for (let i = 0; i < numberOfPoints; i++) {
const randomX = random(frameWidth, WIDTH-frameWidth)
const randomY = random(frameWidth, HEIGHT-frameWidth)
points.push([
constrain(randomX, frameWidth, WIDTH-frameWidth),
constrain(randomY, frameWidth, HEIGHT-frameWidth)
])
}
const d = new Delaunator(points)
for (var i = 0; i < d.triangles.length; i += 3) {
triangles.push([
points[d.triangles[i]],
points[d.triangles[i + 1]],
points[d.triangles[i + 2]]
])
}
return triangles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment