scroll wheel zoom improvements

This commit is contained in:
mourner 2010-09-27 15:00:16 +03:00
parent 0991c7fc78
commit dc906377ec

View File

@ -6,6 +6,7 @@ L.Handler.ScrollWheelZoom = L.Handler.extend({
enable: function() {
if (this._enabled) { return; }
L.DomEvent.addListener(this._map._container, 'mousewheel', this._onWheelScroll, this);
this._delta = 0;
this._enabled = true;
},
@ -16,23 +17,30 @@ L.Handler.ScrollWheelZoom = L.Handler.extend({
},
_onWheelScroll: function(e) {
var delta = L.DomEvent.getWheelDelta(e);
this._delta += L.DomEvent.getWheelDelta(e);
this._lastMousePos = this._map.mouseEventToContainerPoint(e);
delta = (delta > 0 ? Math.ceil(delta) : Math.floor(delta));
var center = this._getCenterForScrollWheelZoom(e, delta),
zoom = this._map.getZoom() + delta;
//TODO clamp many scroll events into one within a time interval
this._map.setView(center, zoom);
clearTimeout(this._timer);
this._timer = setTimeout(L.Util.bind(this._performZoom, this), 50);
L.DomEvent.preventDefault(e);
},
_getCenterForScrollWheelZoom: function(e, delta) {
_performZoom: function() {
var delta = Math.round(this._delta);
if (!delta) { return; }
var center = this._getCenterForScrollWheelZoom(this._lastMousePos, delta),
zoom = this._map.getZoom() + delta;
this._map.setView(center, zoom);
this._delta = 0;
},
_getCenterForScrollWheelZoom: function(mousePos, delta) {
var centerPoint = this._map.getPixelBounds().getCenter(),
viewHalf = this._map.getSize().divideBy(2),
mousePos = this._map.mouseEventToContainerPoint(e),
centerOffset = mousePos.subtract(viewHalf).multiplyBy(1 - Math.pow(2, -delta)),
newCenterPoint = centerPoint.add(centerOffset);
return this._map.unproject(newCenterPoint);