diff --git a/externs/olx.js b/externs/olx.js index b54c279332..4e5fcedb2a 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2648,7 +2648,8 @@ olx.interaction.ModifyOptions.prototype.wrapX; /** - * @typedef {{duration: (number|undefined)}} + * @typedef {{duration: (number|undefined), + * useAnchor: (boolean|undefined)}} * @api */ olx.interaction.MouseWheelZoomOptions; @@ -2662,6 +2663,16 @@ olx.interaction.MouseWheelZoomOptions; olx.interaction.MouseWheelZoomOptions.prototype.duration; +/** + * Enable zooming using the mouse's location as the anchor. Default is `true`. + * When set to false, zooming in and out will zoom to the center of the screen + * instead of zooming on the mouse's location. + * @type {boolean|undefined} + * @api + */ +olx.interaction.MouseWheelZoomOptions.prototype.useAnchor; + + /** * @typedef {{threshold: (number|undefined), * duration: (number|undefined)}} diff --git a/src/ol/interaction/mousewheelzoominteraction.js b/src/ol/interaction/mousewheelzoominteraction.js index c163a61014..c9d1d6bb74 100644 --- a/src/ol/interaction/mousewheelzoominteraction.js +++ b/src/ol/interaction/mousewheelzoominteraction.js @@ -39,6 +39,13 @@ ol.interaction.MouseWheelZoom = function(opt_options) { */ this.duration_ = goog.isDef(options.duration) ? options.duration : 250; + /** + * @private + * @type {boolean} + */ + this.useAnchor_ = goog.isDef(options.useAnchor) ? + options.useAnchor : true; + /** * @private * @type {?ol.Coordinate} @@ -78,7 +85,10 @@ ol.interaction.MouseWheelZoom.handleEvent = function(mapBrowserEvent) { goog.asserts.assertInstanceof(mouseWheelEvent, goog.events.MouseWheelEvent, 'mouseWheelEvent should be of type MouseWheelEvent'); - this.lastAnchor_ = mapBrowserEvent.coordinate; + if (this.useAnchor_) { + this.lastAnchor_ = mapBrowserEvent.coordinate; + } + this.delta_ += mouseWheelEvent.deltaY; if (!goog.isDef(this.startTime_)) { @@ -119,3 +129,17 @@ ol.interaction.MouseWheelZoom.prototype.doZoom_ = function(map) { this.startTime_ = undefined; this.timeoutId_ = undefined; }; + + +/** + * Enable or disable using the mouse's location as an anchor when zooming + * @param {boolean} useAnchor true to zoom to the mouse's location, false + * to zoom to the center of the map + * @api + */ +ol.interaction.MouseWheelZoom.prototype.setMouseAnchor = function(useAnchor) { + this.useAnchor_ = useAnchor; + if (!useAnchor) { + this.lastAnchor_ = null; + } +}; diff --git a/test/spec/ol/map.test.js b/test/spec/ol/map.test.js index 8bddc2f2cc..85f58ddd36 100644 --- a/test/spec/ol/map.test.js +++ b/test/spec/ol/map.test.js @@ -243,6 +243,9 @@ describe('ol.Map', function() { var interactions = ol.interaction.defaults(options); expect(interactions.getLength()).to.eql(1); expect(interactions.item(0)).to.be.a(ol.interaction.MouseWheelZoom); + expect(interactions.item(0).useAnchor_).to.eql(true); + interactions.item(0).setMouseAnchor(false); + expect(interactions.item(0).useAnchor_).to.eql(false); }); });