Skip to content

Support a ratchety-zoom mode #1695

Closed
Closed

Description

@tmcw

Noticed that some code that uses gl-js hacks it to snap to integer zoom levels. We should support this in core.

Activity

  1. lucaswoj commented on Nov 11, 2015

    @lucaswoj
    Contributor

    👍

    { ratchety: true, ... }

  2. changed the title [-]Ratchety-zoom mode[/-] [+]Support a ratchety-zoom mode[/+] on Jul 29, 2016
  3. lucaswoj commented on Jul 29, 2016

    @lucaswoj
    Contributor

    A note for context: the biggest advantage (in my mind) of enabling "ratchety zoom" is improving rendering performance.

  4. mourner commented on Jul 29, 2016

    @mourner
    Member

    How would this improve rendering performance?

  5. lucaswoj commented on Jul 29, 2016

    @lucaswoj
    Contributor

    My impression was that some users were using rachety zooming to avoid some calculations on fractional zoom changes. Is that not your understanding, @mourner?

  6. mourner commented on Jul 31, 2016

    @mourner
    Member

    @lucaswoj I don't think there's a significant performance difference between a frame when zooming and a frame when panning. Fractional vs integer zoom levels shouldn't affect performance as well.

    The only use case I can think of for snapping to integer zoom levels is compatibility with stuff like Leaflet, but e.g. https://github.com/mapbox/mapbox-gl-leaflet doesn't need a GL JS option for that because it synchronizes based on its own events.

    @tmcw can you elaborate more on use cases and how that would work exactly? Would it be completely discrete snapping (one frame per integer zoom), or animated zooming between integer zooms that you can't stop?

  7. tmcw commented on Jul 31, 2016

    @tmcw
    ContributorAuthor

    The ArcGIS API is one example - There's another scroll-zooming example I can dig up that snapped to integer zoom levels. Every example I've seen so far supports animated zoom but then snaps to a level, and I'm pretty sure that it's about two things:

    • Tradition!
    • Making raster imagery look nice, maybe? Since you don't have over or under-scaled images?
  8. zavan commented on Oct 25, 2016

    @zavan

    "Making raster imagery look nice, maybe? Since you don't have over or under-scaled images?"

    That is the point for me.

    PS.: Do you have those hacks? I need them for now...

  9. lucaswoj commented on Oct 25, 2016

    @lucaswoj
    Contributor

    @zavan You can use some iteration of this to get the behavior you're looking for:

    map.on('moveend', function() {
        map.setZoom(Math.round(map.getZoom()));
    });
  10. aliir74 commented on Jan 20, 2019

    @aliir74

    Do you support this feature now? The set zoom function sharply shakes map and isn't good for me.

  11. vwong commented on Jan 23, 2019

    @vwong

    This worked for me:

    import { debounce } from 'lodash'
    import { Map } from 'mapbox-gl'
    
    const map = new Map({
      scrollZoom: false
    })
    
    map.on('wheel', debounce(({ originalEvent: { wheelDelta } }) => {
      map.setZoom(map.getZoom() + Math.sign(wheelDelta))
    }, 100))
  12. HarelM commented on Mar 29, 2019

    @HarelM

    @vwong I assume your solution will not work when using the fitBounds function, right?
    So the only valid workaround is to use @lucaswoj answer?

  13. vwong commented on Mar 31, 2019

    @vwong

    @HarelM It should work. Here's a fuller version of my example. Works on touch devices too.

    const integerZoom = debounce(({ originalEvent: { deltaY, detail, wheelDelta } }) => {
      const delta = wheelDelta || -deltaY || -detail
      const zoom = Math.round(map.getZoom()) + (delta > 0 ? 1 : -1)
    
      map.setZoom(zoom)
    }, 50, { leading: true, trailing: false })
    
    const ratchet = () => {
      const zoom = Math.round(map.getZoom())
      if (zoom !== map.getZoom()) {
        map.setZoom(zoom)
      }
    }
    
    const ratchetOn = () => {
      map.setZoom(Math.round(map.getZoom()))
      map.on('idle', ratchet)
      map.on('wheel', integerZoom)
      map.scrollZoom.disable()
    }
    
    const ratchetOff = () => {
      map.off('idle', ratchet)
      map.off('wheel', integerZoom)
      map.scrollZoom.enable()
    }

    In my use case, I needed to turn ratchet on/off depending on other external factors. Good luck!

  14. geoyogesh commented on May 1, 2019

    @geoyogesh

    @zavan You can use some iteration of this to get the behavior you're looking for:

    map.on('moveend', function() {
        map.setZoom(Math.round(map.getZoom()));
    });

    this snippet will cause
    ERROR RangeError: Maximum call stack size exceeded

  15. geojs commented on Mar 2, 2021

    @geojs

    @zavan You can use some iteration of this to get the behavior you're looking for:

    map.on('moveend', function() {
        map.setZoom(Math.round(map.getZoom()));
    });

    this snippet will cause
    ERROR RangeError: Maximum call stack size exceeded

    @geoyogesh to not have the ERROR RangeError:

    map.on('moveend', function () {
      const zoom = Math.round(map.getZoom())
      if (zoom !== map.getZoom()) {
        map.setZoom(zoom)
      }
    })
  16. geoyogesh commented on Mar 2, 2021

    @geoyogesh

    @geojs i wanted rachety zoom mode because i am using dynamic map service and mapbox aggregively fetches data during factional zoom... i disabled zoom scroll which made api fetch calls more friendly
    map.scrollZoom.disable();

    user expecirece was bad while using map.setZoom so i prefered disabling zoom scroll
    Thanks

  17. IgnacioHeredia commented on Oct 13, 2021

    @IgnacioHeredia

    As far as the wheel zoom is concerned, you are probably looking for:

    map.scrollZoom.setWheelZoomRate(1);

    after starting from an integer zoom level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions