question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Glitchy behavior when animating fitBounds multiple times

See original GitHub issue

Demo: 0.7.3, master.

Triggering code:

map.fitBounds([[-20, -20], [20, 20]]);
map.fitBounds([[-10, -10], [10, 10]]);

Expected behaviour: final state of the map reflects the last call to fitBounds.

Observed behaviour: final state of the map might reflect either of the fitBounds calls, depending on the initial map state.

The effect is reproducible as long as the second fitBounds is called before the first has finished animating, eg:

map.fitBounds([[-20, -20], [20, 20]]);
setTimeout(function() {
  map.fitBounds([[-10, -10], [10, 10]]);
}, 200);

or a user clicking two buttons in quick succession will also show the effect.

Disabling animation on the fitBounds calls prevents the issue, but… the zooms are so pretty! 😃

I don’t have a simple testcase for it, but in my project, two quick fitBounds calls can also leave the map in weirder states (zoomed out very far) that don’t reflect any of the fitBounds calls.

Edit: the issue may not affect other animated view changes. tested and working as expected:

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:7
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
natevwcommented, Mar 22, 2022

Is the core issue here that Leaflet doesn’t cancel previous animations when a new one is triggered? I’ve noticed a situation I think is related, where I am updating the map bounds as several layers each asynchronously load.

Basically something like:

let layer1 = null, layer2 = null;
function updateBounds() {
  if (layer1) map.setView(layer1.getBounds().getCenter(), 12);
  else if (layer2) map.fitBounds(layer2.getBounds());
}

loadData('layer_one').then((d) => {
  layer1 = addDataLayer(d, map);
  updateBounds();
});
loadData('layer_two').then((d) => {
  layer2 = addDataLayer(d, map);
  updateBounds();
});

Admittedly a little weird logic (and even more complicated in practice…), but regardless, what I’m seeing is that by default I don’t always get the expected boundaries once both layers have loaded. That is, the map should always be zoomed to the layer1 state once both layers load. But if layer2 happens to load first I do not get that result.

If I avoid the default by adding {animate:false} to both calls then it does work as expected!

So without diving into the Leaflet code, my theory is that if the map.fitBounds(layer2.getBounds()) animation is still in progress when I request map.setView(layer1.getBounds().getCenter(), 12) the earlier animation keeps progressing too, in parallel with and throwing off the end result of the new animation, perhaps?

1reaction
perliedmancommented, Aug 11, 2017

The original issue is still reproducible in @theashyster’s playground example. I think my explanation here is still a valid description of why this happens, but we need to figure out how.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animating Multiple Markers - javascript - Stack Overflow
I would like to move each marker along the path of the polyline to simulate simultaneous movement of each marker. I am having...
Read more >
openseadragon/openseadragon - Gitter
@altert My image is a mosaic: fitBounds could work well for alternating between "multiple visible sub images at a random point" and "a...
Read more >
Apply multiple animation effects to one object - Microsoft Support
In the Animation Pane, click the down arrow next to the animation effect, and then click Timing. Set the timing of an animation...
Read more >
Documentation - a JavaScript library for interactive maps
Pans the map by a given number of pixels (animated). ... moveend event so that it doesn't happen often even if the method...
Read more >
Behaviors: body (auto-animating) - Adobe Support
The Character Animator Motion Library is a collection of more than 350 full-body ... The maximum value is 1,000% (ten times faster).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found