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)

This commit is contained in:
EricKnudsen256 2024-04-27 23:31:11 -04:00
parent bf1ff2db63
commit a685ad3bef

35
main.js
View file

@ -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);