From 321f8087b5d90fabc9f74c62dc99fa0b216cfd56 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 24 Oct 2023 19:11:46 +0200 Subject: [PATCH] Catch errors in Javaccript-Voronoi --- mibpov.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/mibpov.js b/mibpov.js index 89fa737..d4342d0 100644 --- a/mibpov.js +++ b/mibpov.js @@ -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