mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-02-04 17:41:23 +01:00
throttle work done on zoom
This commit is contained in:
parent
9df7b2f7a5
commit
e7b354e217
3 changed files with 278 additions and 162 deletions
48
main.js
48
main.js
|
|
@ -128,9 +128,23 @@ let color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
|
|||
const lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
|
||||
|
||||
// d3 zoom behavior
|
||||
let scale = 1,
|
||||
viewX = 0,
|
||||
viewY = 0;
|
||||
let scale = 1;
|
||||
let viewX = 0;
|
||||
let viewY = 0;
|
||||
|
||||
const zoomThrottled = throttle(doWorkOnZoom, 100);
|
||||
function zoomed() {
|
||||
const {k, x, y} = d3.event.transform;
|
||||
|
||||
const isScaleChanged = Boolean(scale - k);
|
||||
const isPositionChanged = Boolean(viewX - x || viewY - y);
|
||||
|
||||
scale = k;
|
||||
viewX = x;
|
||||
viewY = y;
|
||||
|
||||
zoomThrottled(isScaleChanged, isPositionChanged);
|
||||
}
|
||||
const zoom = d3.zoom().scaleExtent([1, 20]).on("zoom", zoomed);
|
||||
|
||||
// default options
|
||||
|
|
@ -414,30 +428,24 @@ function showWelcomeMessage() {
|
|||
});
|
||||
}
|
||||
|
||||
function zoomed() {
|
||||
const transform = d3.event.transform;
|
||||
const scaleDiff = scale - transform.k;
|
||||
const positionDiff = (viewX - transform.x) | (viewY - transform.y);
|
||||
if (!positionDiff && !scaleDiff) return;
|
||||
function doWorkOnZoom(isScaleChanged, isPositionChanged) {
|
||||
viewbox.attr("transform", `translate(${viewX} ${viewY}) scale(${scale})`);
|
||||
|
||||
scale = transform.k;
|
||||
viewX = transform.x;
|
||||
viewY = transform.y;
|
||||
viewbox.attr("transform", transform);
|
||||
if (isPositionChanged) drawCoordinates();
|
||||
|
||||
// update grid only if view position
|
||||
if (positionDiff) drawCoordinates();
|
||||
|
||||
// rescale only if zoom is changed
|
||||
if (scaleDiff) {
|
||||
if (isScaleChanged) {
|
||||
invokeActiveZooming();
|
||||
drawScaleBar();
|
||||
}
|
||||
|
||||
// zoom image converter overlay
|
||||
const canvas = document.getElementById("canvas");
|
||||
if (canvas && +canvas.style.opacity) {
|
||||
const img = document.getElementById("image");
|
||||
if (customization === 1) {
|
||||
const canvas = document.getElementById("canvas");
|
||||
if (!canvas || canvas.style.opacity === "0") return;
|
||||
|
||||
const img = document.getElementById("imageToConvert");
|
||||
if (!img) return;
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.setTransform(scale, 0, 0, scale, viewX, viewY);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue