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 // This should improve performance
voronoi.recycle(diagram); voronoi.recycle(diagram);
} }
try {
diagram = voronoi.compute(sites, vbbox); diagram = voronoi.compute(sites, vbbox);
cachedVoronoiDiagram = diagram; cachedVoronoiDiagram = diagram;
return diagram; return diagram;
} catch {
cachedVoronoiDiagram = null;
return null;
}
} else { } else {
return cachedVoronoiDiagram; return cachedVoronoiDiagram;
} }
@ -743,11 +748,29 @@ function draw(recalculate) {
updateAltitudeText(); updateAltitudeText();
return true; return true;
} }
drawError = false;
updateAltitudeText(); 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); let diagram = getVoronoiDiagram(points, recalculate);
if (!diagram) {
voronoiError();
drawError = true;
return true;
}
drawError = false;
let createHalfedgesPath = function(context, cell) { let createHalfedgesPath = function(context, cell) {
context.beginPath(); context.beginPath();
for (let h=0; h<cell.halfedges.length; h++) { for (let h=0; h<cell.halfedges.length; h++) {
@ -1242,7 +1265,7 @@ voronoiCanvas.onmousedown = function(event) {
// select point by clicking. // select point by clicking.
// initiate drag-n-drop if already selected. // initiate drag-n-drop if already selected.
mouseIsDown = true; mouseIsDown = true;
if (!showPoints) { if (drawError || !showPoints) {
// Points need to be shown for drag-n-drop to work // Points need to be shown for drag-n-drop to work
return; return;
} }