Handle mouse wheel zoom events as if they've come from a trackpad.
This commit is contained in:
committed by
Andreas Hocevar
parent
97246daf66
commit
54bae0168f
@@ -8,15 +8,6 @@ import Interaction, {zoomByDelta} from './Interaction.js';
|
|||||||
import {clamp} from '../math.js';
|
import {clamp} from '../math.js';
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @enum {string}
|
|
||||||
*/
|
|
||||||
export const Mode = {
|
|
||||||
TRACKPAD: 'trackpad',
|
|
||||||
WHEEL: 'wheel'
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Options
|
* @typedef {Object} Options
|
||||||
* @property {import("../events/condition.js").Condition} [condition] A function that
|
* @property {import("../events/condition.js").Condition} [condition] A function that
|
||||||
@@ -107,12 +98,6 @@ class MouseWheelZoom extends Interaction {
|
|||||||
*/
|
*/
|
||||||
this.timeoutId_;
|
this.timeoutId_;
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @type {Mode|undefined}
|
|
||||||
*/
|
|
||||||
this.mode_ = undefined;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trackpad events separated by this delay will be considered separate
|
* Trackpad events separated by this delay will be considered separate
|
||||||
* interactions.
|
* interactions.
|
||||||
@@ -206,51 +191,16 @@ class MouseWheelZoom extends Interaction {
|
|||||||
this.startTime_ = now;
|
this.startTime_ = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.mode_ || now - this.startTime_ > this.trackpadEventGap_) {
|
|
||||||
this.mode_ = Math.abs(delta) < 4 ?
|
|
||||||
Mode.TRACKPAD :
|
|
||||||
Mode.WHEEL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.mode_ === Mode.TRACKPAD) {
|
|
||||||
const view = map.getView();
|
|
||||||
if (this.trackpadTimeoutId_) {
|
|
||||||
clearTimeout(this.trackpadTimeoutId_);
|
|
||||||
} else {
|
|
||||||
view.beginInteraction();
|
|
||||||
}
|
|
||||||
this.trackpadTimeoutId_ = setTimeout(this.endInteraction_.bind(this), this.trackpadEventGap_);
|
|
||||||
view.adjustZoom(-delta / this.trackpadDeltaPerZoom_, this.lastAnchor_);
|
|
||||||
this.startTime_ = now;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.totalDelta_ += delta;
|
|
||||||
|
|
||||||
const timeLeft = Math.max(this.timeout_ - (now - this.startTime_), 0);
|
|
||||||
|
|
||||||
clearTimeout(this.timeoutId_);
|
|
||||||
this.timeoutId_ = setTimeout(this.handleWheelZoom_.bind(this, map), timeLeft);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @param {import("../PluggableMap.js").default} map Map.
|
|
||||||
*/
|
|
||||||
handleWheelZoom_(map) {
|
|
||||||
const view = map.getView();
|
const view = map.getView();
|
||||||
if (view.getAnimating()) {
|
if (this.trackpadTimeoutId_) {
|
||||||
view.cancelAnimations();
|
clearTimeout(this.trackpadTimeoutId_);
|
||||||
|
} else {
|
||||||
|
view.beginInteraction();
|
||||||
}
|
}
|
||||||
const delta = clamp(this.totalDelta_, -this.maxDelta_, this.maxDelta_);
|
this.trackpadTimeoutId_ = setTimeout(this.endInteraction_.bind(this), this.trackpadEventGap_);
|
||||||
zoomByDelta(view, -delta, this.lastAnchor_, this.duration_);
|
view.adjustZoom(-delta / this.trackpadDeltaPerZoom_, this.lastAnchor_);
|
||||||
this.mode_ = undefined;
|
this.startTime_ = now;
|
||||||
this.totalDelta_ = 0;
|
return false;
|
||||||
this.lastAnchor_ = null;
|
|
||||||
this.startTime_ = undefined;
|
|
||||||
this.timeoutId_ = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user