Add constrainResolution option

Sometimes we may have the functionality to set scale/resolution provided by a user and it can be any number (so constrainResolution in View class is not useful), but we would like to have constrainResolution in mouse wheel zoom event.
This commit is contained in:
wussup
2020-04-20 15:36:19 +02:00
committed by GitHub
parent e7968bcc1a
commit fc4ec899ca

View File

@@ -29,6 +29,9 @@ export const Mode = {
* @property {boolean} [useAnchor=true] Enable zooming using the mouse's
* location as the anchor. When set to `false`, zooming in and out will zoom to
* the center of the screen instead of zooming on the mouse's location.
* @property {boolean} [constrainResolution=false] If true, the mouse wheel zoom
* event will always animate to the closest zoom level after an interaction;
* false means intermediary zoom levels are allowed.
*/
/**
@@ -84,6 +87,12 @@ class MouseWheelZoom extends Interaction {
this.useAnchor_ =
options.useAnchor !== undefined ? options.useAnchor : true;
/**
* @private
* @type {boolean}
*/
this.constrainResolution_ = options.constrainResolution !== undefined ? options.constrainResolution : false;
/**
* @private
* @type {import("../events/condition.js").Condition}
@@ -214,7 +223,7 @@ class MouseWheelZoom extends Interaction {
}
const view = map.getView();
if (this.mode_ === Mode.TRACKPAD && !view.getConstrainResolution()) {
if (this.mode_ === Mode.TRACKPAD && !(view.getConstrainResolution() || this.constrainResolution_)) {
if (this.trackpadTimeoutId_) {
clearTimeout(this.trackpadTimeoutId_);
} else {
@@ -260,7 +269,7 @@ class MouseWheelZoom extends Interaction {
-this.maxDelta_ * this.deltaPerZoom_,
this.maxDelta_ * this.deltaPerZoom_
) / this.deltaPerZoom_;
if (view.getConstrainResolution()) {
if (view.getConstrainResolution() || this.constrainResolution_) {
// view has a zoom constraint, zoom by 1
delta = delta ? (delta > 0 ? 1 : -1) : 0;
}