Catch errors in Javaccript-Voronoi

This commit is contained in:
Wuzzy 2023-10-24 19:11:46 +02:00
parent 5e9e78fb57
commit 321f8087b5

View File

@ -614,9 +614,14 @@ function getVoronoiDiagram(points, recalculate) {
// This should improve performance
voronoi.recycle(diagram);
}
diagram = voronoi.compute(sites, vbbox);
cachedVoronoiDiagram = diagram;
return diagram;
try {
diagram = voronoi.compute(sites, vbbox);
cachedVoronoiDiagram = diagram;
return diagram;
} catch {
cachedVoronoiDiagram = null;
return null;
}
} else {
return cachedVoronoiDiagram;
}
@ -743,11 +748,29 @@ function draw(recalculate) {
updateAltitudeText();
return true;
}
drawError = false;
updateAltitudeText();
let voronoiError = function() {
context.textAlign = "center";
context.fillStyle = "black";
context.textBaseline = "middle";
context.font = "200% sans-serif";
let msg = "Error in Javascript-Voronoi!";
context.fillText(msg, w/2, h/2);
drawError = true;
updateAltitudeText();
}
let diagram = getVoronoiDiagram(points, recalculate);
if (!diagram) {
voronoiError();
drawError = true;
return true;
}
drawError = false;
let createHalfedgesPath = function(context, cell) {
context.beginPath();
for (let h=0; h<cell.halfedges.length; h++) {
@ -1242,7 +1265,7 @@ voronoiCanvas.onmousedown = function(event) {
// select point by clicking.
// initiate drag-n-drop if already selected.
mouseIsDown = true;
if (!showPoints) {
if (drawError || !showPoints) {
// Points need to be shown for drag-n-drop to work
return;
}