Skip to content

Instantly share code, notes, and snippets.

@Yumshot
Created March 27, 2023 15:59
Show Gist options
  • Save Yumshot/95465c12219358745b7399db140ce8c3 to your computer and use it in GitHub Desktop.
Save Yumshot/95465c12219358745b7399db140ce8c3 to your computer and use it in GitHub Desktop.
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