From a685ad3befe634dd1e4d7f11da5a2f7943e4d932 Mon Sep 17 00:00:00 2001 From: EricKnudsen256 Date: Sat, 27 Apr 2024 23:31:11 -0400 Subject: [PATCH] Added check on zoom/move to hide any burg labels that are not on the screen. Results in noticable FPS increase on maps with high burg count (tested with 800+ burgs) --- main.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index efc115cf..1f92ddc9 100644 --- a/main.js +++ b/main.js @@ -429,7 +429,11 @@ function findBurgForMFCG(params) { function handleZoom(isScaleChanged, isPositionChanged) { viewbox.attr("transform", `translate(${viewX} ${viewY}) scale(${scale})`); - if (isPositionChanged) drawCoordinates(); + if (isPositionChanged) { + drawCoordinates(); + hideOutOfViewLabels(); + } + if (isScaleChanged) { invokeActiveZooming(); @@ -452,6 +456,35 @@ function handleZoom(isScaleChanged, isPositionChanged) { } } +function hideOutOfViewLabels() { + const towns = pack.burgs.filter(b => b.i && !b.capital && !b.removed); + const townLabels = burgLabels.select("#towns"); + + + const bounds = getViewBoxExtent() + + const minX = bounds[0][0]; + const maxX = bounds[1][0]; + + const minY = bounds[0][1]; + const maxY = bounds[1][1]; + + console.log("TownLabels:", townLabels.selectAll("text")); + + townLabels.selectAll("text").each(function (burg, i, z) { + burg = towns[i]; + if(!burg) return; + + if(burg.x < minX || burg.x > maxX || burg.y < minY || burg.y > maxY) { + d3.select(this).classed("hidden", true); + } + else { + d3.select(this).classed("hidden", false); + } + }) +} + + // Zoom to a specific point function zoomTo(x, y, z = 8, d = 2000) { const transform = d3.zoomIdentity.translate(x * -z + graphWidth / 2, y * -z + graphHeight / 2).scale(z);