I've added an image overlay to a non-geographical map using Leaflet.js and CRS.Simple for positioning. When I call map.fitToBounds(bounds)
the image overlay is initially displayed mostly off screen, but if the browser window is resized (presumably triggering a reflow) it pops into the expected position.
What I Did
The relevant lines of code used to generate the map:
let map = L.map('floorplan', { crs: L.CRS.Simple });
const imageUrl = floorplanUrl;
const imageBounds = [[0, 0], [1000, 1000]];
L.imageOverlay(imageUrl, imageBounds).addTo(map);
map.fitBounds(imageBounds);
Expected:
The whole floorplan image is displayed on the screen and fills the map area.
Actual:
Most of the floorplan image initially appears off screen, but moves into the expected position if the browser window is resized. (Tried in both Firefox and Chrome).
Image overlay displayed off screen:
Image overlay after resizing browser window:
I can work around this by manually dispatching a resize event on the window object:
const event = new Event('resize');
window.dispatchEvent(event);
But I suspect I'm doing something wrong, either misunderstanding the coordinate system, or maybe there's some interference with some other CSS on the page.
I've noticed that the difference between the first state and the second state is that in the second state a 3D CSS transform is added to the div with classes leaflet-pane
and leaflet-map-pane
:
transform: translate3d(530px, 248px, 0px);
What am I doing wrong?