openlayers icon indicating copy to clipboard operation
openlayers copied to clipboard

Zoom snapping

Open iamvdo opened this issue 1 year ago • 3 comments

Hello,

After reading the doc and looking at the examples, I can't find a simple approach to zoom snapping. What I want is to be able to zoom in/out, at a regular interval, let's say 0.25 for example (it's possible with zoom controls and delta, but what about mouse/pinch?).

Right now, building a simple map, at zoom 13, and zooming in gives zoom 13.38, then 13.76, then 14.14, etc. The problem is tiles are never sharp because zoom 14 is never set. Using constrainResolution: true forces integer zoom, but how to force decimal zoom ? Using resolutions ?

Thanks for your help!

iamvdo avatar Sep 10 '24 14:09 iamvdo

One option would be to configure the view with constrainResolution: true and smaller intervals between zoom levels zoomFactor: 2 ** 0.25. The reported zoom levels would then be 4 times those of a normal view, but that not important as it is the view resolution which is used to determine tile zoom. You would probably want to increase the MouseWheelZoom maxDelta setting to keep the interaction as responsive as in a normal view. Currently that does not work as expected if constrainResolution is set, due to the use of a hardcoded value which looks like a bug (and is easily fixable). https://stackblitz.com/edit/js-qh4iea?file=index.html,package.json,index.js

mike-000 avatar Sep 11 '24 10:09 mike-000

Wow, thanks for the code! As I understand, constrainResolution: true, zoomFactor: 2 ** 0.25 and maxDelta: 4 should do the trick, right? I'm not sure what kind of bug you're talking about, but overriding MouseWheelZoom.prototype.handleWheelZoom_ does not work on my app. It works on a fresh map though...

On a side note, do you have a reference for zoom/resolutions handling in ol, because i'm realizing I never understood the key concept.

Thanks!

iamvdo avatar Sep 12 '24 11:09 iamvdo

maxDelta: 4 is not essential, but mouse wheel zooming would be slower than usual without it, and it needs a fix for that to work with constrainResolution. The patch I included only works with the dev version as there has been another update since the last release.

mike-000 avatar Sep 12 '24 12:09 mike-000