Dynamically change cursor coords precision

This commit is contained in:
Wuzzy 2023-10-27 18:34:26 +02:00
parent d354078ad1
commit 7c603adbe5

View File

@ -1374,9 +1374,9 @@ function getDistance(x1, y1, x2, y2) {
function canvasPixelCoordsToBiomeCoords(x, y) { function canvasPixelCoordsToBiomeCoords(x, y) {
let w = (voronoiCanvas.width/(limit_heat_max-limit_heat_min)); let w = (voronoiCanvas.width/(limit_heat_max-limit_heat_min));
let h = (voronoiCanvas.height/(limit_humidity_max-limit_humidity_min)); let h = (voronoiCanvas.height/(limit_humidity_max-limit_humidity_min));
let heat = Math.round((x + limit_heat_min * w) / w); let heat = (x + limit_heat_min * w) / w;
// This also flips the Y axis // This also flips the Y axis
let humidity = limit_humidity_min + (limit_humidity_max - (Math.round((y + limit_humidity_min * h) / h))); let humidity = limit_humidity_min + (limit_humidity_max - ((y + limit_humidity_min * h) / h));
return [heat, humidity]; return [heat, humidity];
} }
// Converts heat and humidity coordinates to canvas pixel coordinates; // Converts heat and humidity coordinates to canvas pixel coordinates;
@ -1487,8 +1487,42 @@ function updateCoordinateDisplay(pixelX, pixelY) {
} }
// show coordinates // show coordinates
let [heat, humidity] = canvasPixelCoordsToBiomeCoords(pixelX, pixelY); let [heat, humidity] = canvasPixelCoordsToBiomeCoords(pixelX, pixelY);
let heat_range = limit_heat_max - limit_heat_min;
let humidity_range = limit_humidity_max - limit_humidity_min;
let heat_precision = null;
if (heat_range >= 100) {
heat_precision = 0;
} else if (heat_range >= 10) {
heat_precision = 1;
} else if (heat_range >= 1) {
heat_precision = 2;
}
let humidity_precision = null;
if (humidity_range >= 100) {
humidity_precision = 0;
} else if (humidity_range >= 10) {
humidity_precision = 1;
} else if (humidity_range >= 1) {
humidity_precision = 2;
}
let heatStr, humidityStr;
if (heat_precision !== null) {
heatStr = heat.toFixed(heat_precision);
} else {
heatStr = +heat;
}
if (heat_precision !== null) {
humidityStr = humidity.toFixed(humidity_precision);
} else {
humidityStr = +humidity;
}
if (!drawError) { if (!drawError) {
let html = "cursor coordinates: heat=<span class='statHeat'>"+heat+"</span>; humidity=<span class='statHumidity'>"+humidity+"</span>"; let html = "cursor coordinates: heat=<span class='statHeat'>"+heatStr+"</span>; humidity=<span class='statHumidity'>"+humidityStr+"</span>";
coordinateDisplay.innerHTML = html; coordinateDisplay.innerHTML = html;
} else { } else {
coordinateDisplay.innerHTML = "&nbsp;"; coordinateDisplay.innerHTML = "&nbsp;";