Skip to content

Commit

Permalink
Hold element positions in WeakMap (Leaflet#8749)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops authored Dec 27, 2022
1 parent cc73ef3 commit c6fd99f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/examples/extending/pixelorigin.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ title: Grid coordinates
const pixelOrigin = map.getPixelOrigin();
const markerPixelCoords = map.project(trd, map.getZoom());
const markerAnchor = marker.options.icon.options.iconAnchor;
const markerOffset = marker._icon._leaflet_pos;
const markerOffset = L.DomUtil.getPosition(marker._icon);

document.getElementById('info').innerHTML =
'<div style="color: green">CRS origin: 0,0</div>' +
Expand Down
12 changes: 6 additions & 6 deletions spec/suites/layer/PopupSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,20 @@ describe('Popup', () => {

marker1.bindPopup('Popup').addTo(map);
marker1.openPopup();
const defaultLeft = marker1._popup._container._leaflet_pos.x;
const defaultTop = marker1._popup._container._leaflet_pos.y;
const defaultLeft = L.DomUtil.getPosition(marker1._popup._container).x;
const defaultTop = L.DomUtil.getPosition(marker1._popup._container).y;
marker2.bindPopup('Popup').addTo(map);
marker2.openPopup();
let offsetLeft = marker2._popup._container._leaflet_pos.x;
let offsetTop = marker2._popup._container._leaflet_pos.y;
let offsetLeft = L.DomUtil.getPosition(marker2._popup._container).x;
let offsetTop = L.DomUtil.getPosition(marker2._popup._container).y;
expect(offsetLeft - offset.x).to.eql(defaultLeft);
expect(offsetTop - offset.y).to.eql(defaultTop);

// Now retry passing a popup instance to bindPopup
marker2.bindPopup(L.popup());
marker2.openPopup();
offsetLeft = marker2._popup._container._leaflet_pos.x;
offsetTop = marker2._popup._container._leaflet_pos.y;
offsetLeft = L.DomUtil.getPosition(marker2._popup._container).x;
offsetTop = L.DomUtil.getPosition(marker2._popup._container).y;
expect(offsetLeft - offset.x).to.eql(defaultLeft);
expect(offsetTop - offset.y).to.eql(defaultTop);

Expand Down
11 changes: 4 additions & 7 deletions src/dom/DomUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ export function setTransform(el, offset, scale) {
el.style.transform = `translate3d(${pos.x}px,${pos.y}px,0)${scale ? ` scale(${scale})` : ''}`;
}

const positions = new WeakMap();

// @function setPosition(el: HTMLElement, position: Point)
// Sets the position of `el` to coordinates specified by `position`,
// using CSS translate or top/left positioning depending on the browser
// (used by Leaflet internally to position its layers).
export function setPosition(el, point) {

/*eslint-disable */
el._leaflet_pos = point;
/* eslint-enable */

positions.set(el, point);
setTransform(el, point);
}

Expand All @@ -77,8 +75,7 @@ export function setPosition(el, point) {
export function getPosition(el) {
// this method is only used for elements previously positioned using setPosition,
// so it's safe to cache the position for performance

return el._leaflet_pos || new Point(0, 0);
return positions.get(el) ?? new Point(0, 0);
}

const documentStyle = document.documentElement.style;
Expand Down

0 comments on commit c6fd99f

Please sign in to comment.