Skip to content

Commit 7974caa

Browse files
Mohsen Azimijfirebaugh
Mohsen Azimi
authored andcommitted
Add "zoom around center" option
1 parent 630d98c commit 7974caa

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

js/ui/bind_handlers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function bindHandlers(map, options) {
2323
for (const name in handlers) {
2424
map[name] = new handlers[name](map, options);
2525
if (options.interactive && options[name]) {
26-
map[name].enable();
26+
map[name].enable(options[name]);
2727
}
2828
}
2929

js/ui/handler/scroll_zoom.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ class ScrollZoomHandler {
3737
/**
3838
* Enables the "scroll to zoom" interaction.
3939
*
40+
* @param {Object} [options]
41+
* @param {string} [options.around] If "center" is passed, map will zoom around center of map
42+
*
4043
* @example
4144
* map.scrollZoom.enable();
45+
* @example
46+
* map.scrollZoom.enable({ around: 'center' })
4247
*/
43-
enable() {
48+
enable(options) {
4449
if (this.isEnabled()) return;
4550
this._el.addEventListener('wheel', this._onWheel, false);
4651
this._el.addEventListener('mousewheel', this._onWheel, false);
4752
this._enabled = true;
53+
this._aroundCenter = options && options.around === 'center';
4854
}
4955

5056
/**
@@ -137,7 +143,7 @@ class ScrollZoomHandler {
137143

138144
map.zoomTo(targetZoom, {
139145
duration: this._type === 'wheel' ? 200 : 0,
140-
around: map.unproject(this._pos),
146+
around: this._aroundCenter ? map.getCenter() : map.unproject(this._pos),
141147
delayEndEvents: 200,
142148
smoothEasing: true
143149
}, { originalEvent: e });

js/ui/handler/touch_zoom_rotate.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ class TouchZoomRotateHandler {
4141
/**
4242
* Enables the "pinch to rotate and zoom" interaction.
4343
*
44+
* @param {Object} [options]
45+
* @param {string} [options.around] If "center" is passed, map will zoom around the center
46+
*
4447
* @example
4548
* map.touchZoomRotate.enable();
49+
* @example
50+
* map.touchZoomRotate.enable({ around: 'center' });
4651
*/
47-
enable() {
52+
enable(options) {
4853
if (this.isEnabled()) return;
4954
this._el.addEventListener('touchstart', this._onStart, false);
5055
this._enabled = true;
56+
this._aroundCenter = options && options.around === 'center';
5157
}
5258

5359
/**
@@ -197,7 +203,7 @@ class TouchZoomRotateHandler {
197203
zoom: targetScale,
198204
duration: duration,
199205
easing: inertiaEasing,
200-
around: map.unproject(p)
206+
around: this._aroundCenter ? map.getCenter() : map.unproject(p)
201207
}, { originalEvent: e });
202208
}
203209

js/ui/map.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ const defaultOptions = {
103103
* GL JS would be dramatically worse than expected (i.e. a software renderer would be used).
104104
* @param {boolean} [options.preserveDrawingBuffer=false] If `true`, the map's canvas can be exported to a PNG using `map.getCanvas().toDataURL()`. This is `false` by default as a performance optimization.
105105
* @param {LngLatBoundsLike} [options.maxBounds] If set, the map will be constrained to the given bounds.
106-
* @param {boolean} [options.scrollZoom=true] If `true`, the "scroll to zoom" interaction is enabled (see [`ScrollZoomHandler`](#ScrollZoomHandler)).
106+
* @param {boolean|Object} [options.scrollZoom=true] If `true`, the "scroll to zoom" interaction is enabled. An `Object` value is passed as options to [`ScrollZoomHandler#enable`](#ScrollZoomHandler#enable).
107107
* @param {boolean} [options.boxZoom=true] If `true`, the "box zoom" interaction is enabled (see [`BoxZoomHandler`](#BoxZoomHandler)).
108108
* @param {boolean} [options.dragRotate=true] If `true`, the "drag to rotate" interaction is enabled (see [`DragRotateHandler`](#DragRotateHandler)).
109109
* @param {boolean} [options.dragPan=true] If `true`, the "drag to pan" interaction is enabled (see [`DragPanHandler`](#DragPanHandler)).
110110
* @param {boolean} [options.keyboard=true] If `true`, keyboard shortcuts are enabled (see [`KeyboardHandler`](#KeyboardHandler)).
111111
* @param {boolean} [options.doubleClickZoom=true] If `true`, the "double click to zoom" interaction is enabled (see [`DoubleClickZoomHandler`](#DoubleClickZoomHandler)).
112-
* @param {boolean} [options.touchZoomRotate=true] If `true`, the "pinch to rotate and zoom" interaction is enabled (see [`TouchZoomRotateHandler`](#TouchZoomRotateHandler)).
112+
* @param {boolean|Object} [options.touchZoomRotate=true] If `true`, the "pinch to rotate and zoom" interaction is enabled. An `Object` value is passed as options to [`TouchZoomRotateHandler#enable`](#TouchZoomRotateHandler#enable).
113113
* @param {boolean} [options.trackResize=true] If `true`, the map will automatically resize when the browser window resizes.
114114
* @param {LngLatLike} [options.center=[0, 0]] The inital geographical centerpoint of the map. If `center` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `[0, 0]`.
115115
* @param {number} [options.zoom=0] The initial zoom level of the map. If `zoom` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`.

0 commit comments

Comments
 (0)