Better mousewheel/touchpad behavior for zooming

The navigation control gets better defaults, and the MouseWheel handler
gets a new maxDelta option, which can be used to avoid huge zoom level
jumps on heavy wheel/pad movements.
This commit is contained in:
ahocevar
2012-12-23 18:01:17 +01:00
parent 810d9ea95d
commit 1081fc4b54
3 changed files with 34 additions and 14 deletions

View File

@@ -31,6 +31,14 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
*/
interval: 0,
/**
* Property: maxDelta
* {Integer} Maximum delta to collect before breaking from the current
* interval. In cumulative mode, this also limits the maximum delta
* returned from the handler. Default is Number.POSITIVE_INFINITY.
*/
maxDelta: Number.POSITIVE_INFINITY,
/**
* Property: delta
* {Integer} When interval is set, delta collects the mousewheel z-deltas
@@ -176,10 +184,10 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
// so force delta 1 / -1
delta = - (e.detail / Math.abs(e.detail));
}
this.delta = this.delta + delta;
this.delta += delta;
if(this.interval) {
window.clearTimeout(this._timeoutId);
window.clearTimeout(this._timeoutId);
if(this.interval && Math.abs(this.delta) < this.maxDelta) {
// store e because window.event might change during delay
var evt = OpenLayers.Util.extend({}, e);
this._timeoutId = window.setTimeout(
@@ -211,9 +219,11 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
if (delta) {
e.xy = this.map.events.getMousePosition(e);
if (delta < 0) {
this.callback("down", [e, this.cumulative ? delta : -1]);
this.callback("down",
[e, this.cumulative ? Math.max(-this.maxDelta, delta) : -1]);
} else {
this.callback("up", [e, this.cumulative ? delta : 1]);
this.callback("up",
[e, this.cumulative ? Math.min(this.maxDelta, delta) : 1]);
}
}
},