Skip to content

Commit 0149d21

Browse files
committed
fix: popup flickering on players
Instead of relying on Leaflet to handle the moving popups, the code handles it. This means that the popup isn't redrawing every time it moves position (I think that's what was heppening) So, we get a smooth experience with moving players.
1 parent 878ee15 commit 0149d21

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

dist/first-bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/map.1.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// ************************************************************************** //
1818

1919
window.MarkerStore = [];
20+
window.PopupStore = {}; // MarkerId : Popup
2021
window.CurrentLayer = undefined;
2122
window.PlayerMarkers = L.markerClusterGroup({
2223
maxClusterRadius: 20,

js/src/socket.1.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,6 @@ function getPlayerInfoHtml(plr) {
296296
return html;
297297
}
298298

299-
function updatePopups(){
300-
console.log("Updating?");
301-
for (const key in localCache) {
302-
//MarkerStore[localCache[plr.identifer].marker].unbindPopup();
303-
//MarkerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
304-
MarkerStore[localCache[key].marker].setPopupContent(localCache[key].lastHtml);
305-
}
306-
}
307-
308-
setInterval(updatePopups, 5000);
309-
310299
function doPlayerUpdate(players) {
311300

312301
if (config.debug) {
@@ -351,23 +340,41 @@ function doPlayerUpdate(players) {
351340

352341
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
353342

354-
//console.log(infoContent !== localCache[plr.identifer].lastHtml);
355-
if (infoContent !== localCache[plr.identifer].lastHtml) {
343+
var m = localCache[plr.identifer].marker;
344+
var marker = MarkerStore[m];
345+
var popup = PopupStore[m];
346+
347+
if (infoContent != localCache[plr.identifer].lastHtml){
348+
popup.setContent(infoContent);
356349
localCache[plr.identifer].lastHtml = infoContent;
357-
//MarkerStore[localCache[plr.identifer].marker].unbindPopup();
358-
//MarkerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
350+
}
351+
352+
if(popup.isOpen()){
353+
if (popup.getLatLng().distanceTo(marker.getLatLng()) != 0){
354+
popup.setLatLng(marker.getLatLng());
355+
}
359356
}
360357

361358

362359
} else {
360+
localCache[plr.identifer].lastHtml = infoContent;
361+
var html = getPlayerInfoHtml(plr);
362+
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-icon"></div><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
363363

364364
var obj = new MarkerObject(plr.name, new Coordinates(plr.pos.x, plr.pos.y, plr.pos.z), MarkerTypes[6], "", {isPlayer: true, player: plr});
365365
var m = localCache[plr.identifer].marker = createMarker(false, false, obj, plr.name) - 1;
366366

367-
var html = getPlayerInfoHtml(plr);
368-
369-
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-icon"></div><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
370-
localCache[plr.identifer].lastHtml = infoContent;
367+
MarkerStore[m].unbindPopup(); // We want to handle the popups ourselfs.
368+
PopupStore[m] = L.popup()
369+
.setContent(infoContent)
370+
.setLatLng(MarkerStore[m].getLatLng()); // Make a new marker
371+
372+
MarkerStore[m].on("click", function(e) {
373+
console._log(e);
374+
Map.closePopup(Map._popup);
375+
PopupStore[e.target.options.id].setLatLng(e.latlng);
376+
Map.openPopup(PopupStore[e.target.options.id]);
377+
});
371378
}
372379

373380
});

0 commit comments

Comments
 (0)