mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
slightly optimize onZoom
This commit is contained in:
parent
decfbc1337
commit
aab27ca673
4 changed files with 25 additions and 12 deletions
21
main.js
21
main.js
|
|
@ -131,20 +131,21 @@ let scale = 1;
|
||||||
let viewX = 0;
|
let viewX = 0;
|
||||||
let viewY = 0;
|
let viewY = 0;
|
||||||
|
|
||||||
const zoomThrottled = throttle(doWorkOnZoom, 100);
|
function onZoom() {
|
||||||
function zoomed() {
|
|
||||||
const {k, x, y} = d3.event.transform;
|
const {k, x, y} = d3.event.transform;
|
||||||
|
|
||||||
const isScaleChanged = Boolean(scale - k);
|
const isScaleChanged = Boolean(scale - k);
|
||||||
const isPositionChanged = Boolean(viewX - x || viewY - y);
|
const isPositionChanged = Boolean(viewX - x || viewY - y);
|
||||||
|
if (!isScaleChanged && !isPositionChanged) return;
|
||||||
|
|
||||||
scale = k;
|
scale = k;
|
||||||
viewX = x;
|
viewX = x;
|
||||||
viewY = y;
|
viewY = y;
|
||||||
|
|
||||||
zoomThrottled(isScaleChanged, isPositionChanged);
|
handleZoom(isScaleChanged, isPositionChanged);
|
||||||
}
|
}
|
||||||
const zoom = d3.zoom().scaleExtent([1, 20]).on("zoom", zoomed);
|
const onZoomDebouced = debounce(onZoom, 50);
|
||||||
|
const zoom = d3.zoom().scaleExtent([1, 20]).on("zoom", onZoomDebouced);
|
||||||
|
|
||||||
// default options
|
// default options
|
||||||
let options = {
|
let options = {
|
||||||
|
|
@ -449,7 +450,17 @@ function applyDefaultBiomesSystem() {
|
||||||
return {i: d3.range(0, name.length), name, color, biomesMartix, habitability, iconsDensity, icons, cost};
|
return {i: d3.range(0, name.length), name, color, biomesMartix, habitability, iconsDensity, icons, cost};
|
||||||
}
|
}
|
||||||
|
|
||||||
function doWorkOnZoom(isScaleChanged, isPositionChanged) {
|
let optimizedQuality = false;
|
||||||
|
|
||||||
|
function handleZoom(isScaleChanged, isPositionChanged) {
|
||||||
|
if (!optimizedQuality) {
|
||||||
|
setRendering("optimizeSpeed");
|
||||||
|
setTimeout(() => {
|
||||||
|
optimizedQuality = false;
|
||||||
|
setRendering(shapeRendering.value);
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
viewbox.attr("transform", `translate(${viewX} ${viewY}) scale(${scale})`);
|
viewbox.attr("transform", `translate(${viewX} ${viewY}) scale(${scale})`);
|
||||||
|
|
||||||
if (isPositionChanged) drawCoordinates();
|
if (isPositionChanged) drawCoordinates();
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ modules.editors = true;
|
||||||
// restore default viewbox events
|
// restore default viewbox events
|
||||||
function restoreDefaultEvents() {
|
function restoreDefaultEvents() {
|
||||||
svg.call(zoom);
|
svg.call(zoom);
|
||||||
viewbox.style("cursor", "default").on(".drag", null).on("click", clicked).on("touchmove mousemove", moved);
|
viewbox.style("cursor", "default").on(".drag", null).on("click", clicked).on("touchmove mousemove", onMouseMove);
|
||||||
legend.call(d3.drag().on("start", dragLegendBox));
|
legend.call(d3.drag().on("start", dragLegendBox));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,8 +65,8 @@ function showElementLockTip(event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const moved = debounce(mouseMove, 100);
|
const onMouseMove = debounce(handleMouseMove, 100);
|
||||||
function mouseMove() {
|
function handleMouseMove() {
|
||||||
const point = d3.mouse(this);
|
const point = d3.mouse(this);
|
||||||
const i = findCell(point[0], point[1]); // pack cell id
|
const i = findCell(point[0], point[1]); // pack cell id
|
||||||
if (i === undefined) return;
|
if (i === undefined) return;
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ optionsContent.addEventListener("change", function (event) {
|
||||||
if (id === "zoomExtentMin" || id === "zoomExtentMax") changeZoomExtent(value);
|
if (id === "zoomExtentMin" || id === "zoomExtentMax") changeZoomExtent(value);
|
||||||
else if (id === "optionsSeed") generateMapWithSeed("seed change");
|
else if (id === "optionsSeed") generateMapWithSeed("seed change");
|
||||||
else if (id === "uiSizeInput" || id === "uiSizeOutput") changeUIsize(value);
|
else if (id === "uiSizeInput" || id === "uiSizeOutput") changeUIsize(value);
|
||||||
if (id === "shapeRendering") viewbox.attr("shape-rendering", value);
|
else if (id === "shapeRendering") setRendering(value);
|
||||||
else if (id === "yearInput") changeYear();
|
else if (id === "yearInput") changeYear();
|
||||||
else if (id === "eraInput") changeEra();
|
else if (id === "eraInput") changeEra();
|
||||||
else if (id === "stateLabelsModeInput") options.stateLabelsMode = value;
|
else if (id === "stateLabelsModeInput") options.stateLabelsMode = value;
|
||||||
|
|
@ -543,9 +543,7 @@ function applyStoredOptions() {
|
||||||
const themeColor = localStorage.getItem("themeColor");
|
const themeColor = localStorage.getItem("themeColor");
|
||||||
changeDialogsTheme(themeColor, transparency);
|
changeDialogsTheme(themeColor, transparency);
|
||||||
|
|
||||||
// set shape rendering
|
setRendering(shapeRendering.value);
|
||||||
viewbox.attr("shape-rendering", shapeRendering.value);
|
|
||||||
|
|
||||||
options.stateLabelsMode = stateLabelsModeInput.value;
|
options.stateLabelsMode = stateLabelsModeInput.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -622,6 +620,10 @@ function randomizeCultureSet() {
|
||||||
changeCultureSet();
|
changeCultureSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setRendering(value) {
|
||||||
|
viewbox.attr("shape-rendering", value);
|
||||||
|
}
|
||||||
|
|
||||||
// generate current year and era name
|
// generate current year and era name
|
||||||
function generateEra() {
|
function generateEra() {
|
||||||
if (!stored("year")) yearInput.value = rand(100, 2000); // current year
|
if (!stored("year")) yearInput.value = rand(100, 2000); // current year
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue