Created
March 27, 2023 15:59
-
-
Save Yumshot/95465c12219358745b7399db140ce8c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function drawRandomRoadPattern(ctx: CanvasRenderingContext2D, width: number, height: number) { | |
// Generate a random set of points | |
const numPoints = Math.floor(Math.random() * 20) + 10; | |
const points = range(numPoints).map(() => [Math.random() * width, Math.random() * height] as [number, number]); | |
// Create a Voronoi diagram from the points | |
const voronoiDiagram = voronoi().extent([[0, 0], [width, height]])(points); | |
// Draw the Voronoi diagram | |
ctx.strokeStyle = "white"; | |
ctx.lineWidth = 0.5; | |
ctx.beginPath(); | |
voronoiDiagram.edges.forEach((edge: { left: any; right: any; }) => { | |
const start = edge.left; | |
const end = edge.right; | |
if (!start || !end) return; | |
ctx.moveTo(start[0], start[1]); | |
ctx.lineTo(end[0], end[1]); | |
}); | |
ctx.stroke(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment