aboutsummaryrefslogtreecommitdiffstats
path: root/extlib/leaflet/src/handler/ScrollWheelZoom.js
blob: dc877e17a6e0e0252d2c94c2f75a15db845f3b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * L.Handler.ScrollWheelZoom is used internally by L.Map to enable mouse scroll wheel zooming on the map.
 */

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;
	},
	
	disable: function() {
		if (!this._enabled) { return; }
		L.DomEvent.removeListener(this._map._container, 'mousewheel', this._onWheelScroll);
		this._enabled = false;
	},
	
	_onWheelScroll: function(e) {
		this._delta += L.DomEvent.getWheelDelta(e);
		this._lastMousePos = this._map.mouseEventToContainerPoint(e);
		
		clearTimeout(this._timer);
		this._timer = setTimeout(L.Util.bind(this._performZoom, this), 50);
		
		L.DomEvent.preventDefault(e);
	},
	
	_performZoom: function() {
		var delta = Math.round(this._delta);
		this._delta = 0;
		
		if (!delta) { return; }
		
		var center = this._getCenterForScrollWheelZoom(this._lastMousePos, delta),
			zoom = this._map.getZoom() + delta;
		
		if (this._map._limitZoom(zoom) == this._map._zoom) { return; }

		this._map.setView(center, zoom);
	},
	
	_getCenterForScrollWheelZoom: function(mousePos, delta) {
		var centerPoint = this._map.getPixelBounds().getCenter(),
			viewHalf = this._map.getSize().divideBy(2),
			centerOffset = mousePos.subtract(viewHalf).multiplyBy(1 - Math.pow(2, -delta)),
			newCenterPoint = centerPoint.add(centerOffset);
		return this._map.unproject(newCenterPoint, this._map._zoom, true);
	}
});