From e34ab4711cc9be60fa9949ddabd0b129883b7d5b Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sat, 15 Jul 2023 17:22:04 -0600 Subject: [PATCH] popup anchor and resize fix --- static/map.js | 22 ++++++++++++++-------- static/socket.js | 6 ++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/static/map.js b/static/map.js index 6fec0fc..d8296e0 100644 --- a/static/map.js +++ b/static/map.js @@ -24,16 +24,24 @@ function initializeMap(mapImgUrl) { // this works but assumes the map is square (reasonable limitation I think) function resizeMarkers() { + let tryAgain = false; tokens.forEach(t=>{ - const icon = t.m.options.icon; - const scaleFactor = mapImg._image.clientWidth / mapImg._image.naturalWidth; + if (t.m._icon == null) { + tryAgain = true; + } else { + const icon = t.m.options.icon; + const scaleFactor = mapImg._image.clientWidth / mapImg._image.naturalWidth; - icon.options.iconSize = [scaleFactor * t.t.w, scaleFactor * t.t.h]; - icon.options.iconAnchor = [scaleFactor * t.t.oX, scaleFactor * t.t.oY]; - icon.options.popupAnchor = [0, -scaleFactor * t.t.oY]; + icon.options.iconSize = [scaleFactor * t.t.w, scaleFactor * t.t.h]; + icon.options.iconAnchor = [scaleFactor * t.t.oX, scaleFactor * t.t.oY]; + icon.options.popupAnchor = [0, -scaleFactor * t.t.oY]; - t.m.setIcon(icon); + t.m.setIcon(icon); + } }); + if (tryAgain) { + setTimeout(resizeMarkers, 500) + } } function processTokens(tokenChanges) { @@ -45,7 +53,6 @@ function processTokens(tokenChanges) { if (t.x != null && t.y != null && !self.t.active && t.active) { self.t.active = true; self.m.addTo(map); - resizeMarkers(); // token was made inactive } else if (t.x != null && t.y != null && self.t.active && !t.active) { self.t.active = false; @@ -74,7 +81,6 @@ function processTokens(tokenChanges) { }); if (t.active) { self.m.addTo(map); - resizeMarkers(); } } } diff --git a/static/socket.js b/static/socket.js index 6c698bb..8fc66f7 100644 --- a/static/socket.js +++ b/static/socket.js @@ -197,8 +197,10 @@ function dial() { tokens[0].m.removeFrom(map); tokens.shift(); } - mapImg.removeFrom(map); - mapImg = null; + if (mapImg) { + mapImg.removeFrom(map); + mapImg = null; + } } }); conn.addEventListener("open", e => {