From de1575e4578c82cd9da7e635f086ac6fdda30f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 25 Mar 2013 18:15:00 +0100 Subject: [PATCH] One direction pinch zoom --- src/ol/array.js | 34 ++- src/ol/interaction/touchzoominteraction.js | 17 +- src/ol/resolutionconstraint.js | 20 +- src/ol/source/imagesource.js | 2 +- src/ol/tilegrid/tilegrid.js | 2 +- src/ol/view2d.js | 17 +- test/spec/ol/array.test.js | 50 ++++- test/spec/ol/resolutionconstraint.test.js | 238 +++++++++++++-------- test/spec/ol/view2d.test.js | 26 +-- 9 files changed, 269 insertions(+), 137 deletions(-) diff --git a/src/ol/array.js b/src/ol/array.js index 2a1ce13de4..548e7d04c3 100644 --- a/src/ol/array.js +++ b/src/ol/array.js @@ -33,9 +33,12 @@ ol.array.binaryFindNearest = function(arr, target) { /** * @param {Array.} arr Array. * @param {number} target Target. + * @param {number} direction 0 means return the nearest, > 0 + * means return the largest nearest, < 0 means return the + * smallest nearest. * @return {number} Index. */ -ol.array.linearFindNearest = function(arr, target) { +ol.array.linearFindNearest = function(arr, target, direction) { var n = arr.length; if (arr[0] <= target) { return 0; @@ -43,17 +46,34 @@ ol.array.linearFindNearest = function(arr, target) { return n - 1; } else { var i; - for (i = 1; i < n; ++i) { - if (arr[i] == target) { - return i; - } else if (arr[i] < target) { - if (arr[i - 1] - target < target - arr[i]) { + if (direction > 0) { + for (i = 1; i < n; ++i) { + if (arr[i] < target) { return i - 1; - } else { + } + } + } else if (direction < 0) { + for (i = 1; i < n; ++i) { + if (arr[i] <= target) { return i; } } + } else { + for (i = 1; i < n; ++i) { + if (arr[i] == target) { + return i; + } else if (arr[i] < target) { + if (arr[i - 1] - target < target - arr[i]) { + return i - 1; + } else { + return i; + } + } + } } + // We should never get here, but the compiler complains + // if it finds a path for which no number is returned. + goog.asserts.assert(false); return n - 1; } }; diff --git a/src/ol/interaction/touchzoominteraction.js b/src/ol/interaction/touchzoominteraction.js index bbb0bab518..d3074b5ad3 100644 --- a/src/ol/interaction/touchzoominteraction.js +++ b/src/ol/interaction/touchzoominteraction.js @@ -29,6 +29,12 @@ ol.interaction.TouchZoom = function() { */ this.lastDistance_; + /** + * @private + * @type {number} + */ + this.lastScaleDelta_ = 1; + }; goog.inherits(ol.interaction.TouchZoom, ol.interaction.Touch); @@ -53,6 +59,9 @@ ol.interaction.TouchZoom.prototype.handleTouchMove = scaleDelta = this.lastDistance_ / distance; } this.lastDistance_ = distance; + if (scaleDelta != 1.0) { + this.lastScaleDelta_ = scaleDelta; + } var map = mapBrowserEvent.map; var view = map.getView(); @@ -78,9 +87,12 @@ ol.interaction.TouchZoom.prototype.handleTouchEnd = if (this.targetTouches.length < 2) { var map = mapBrowserEvent.map; var view = map.getView(); - // take the resolution constraint into account + // Zoom to final resolution, with an animation, and provide a + // direction not to zoom out/in if user was pinching in/out. + // Direction is > 0 if pinching out, and < 0 if pinching in. + var direction = this.lastScaleDelta_ - 1; view.zoom(map, view.getResolution(), undefined, - ol.interaction.TOUCHZOOM_ANIMATION_DURATION); + ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction); view.setHint(ol.ViewHint.INTERACTING, -1); return false; } else { @@ -97,6 +109,7 @@ ol.interaction.TouchZoom.prototype.handleTouchStart = if (this.targetTouches.length >= 2) { var view = mapBrowserEvent.map.getView(); this.lastDistance_ = undefined; + this.lastScaleDelta_ = 1; view.setHint(ol.ViewHint.INTERACTING, 1); return true; } else { diff --git a/src/ol/resolutionconstraint.js b/src/ol/resolutionconstraint.js index 5aa47625be..89a237e748 100644 --- a/src/ol/resolutionconstraint.js +++ b/src/ol/resolutionconstraint.js @@ -6,7 +6,7 @@ goog.require('ol.array'); /** - * @typedef {function((number|undefined), number): (number|undefined)} + * @typedef {function((number|undefined), number, number): (number|undefined)} */ ol.ResolutionConstraintType; @@ -20,7 +20,7 @@ ol.ResolutionConstraintType; ol.ResolutionConstraint.createContinuous = function(power, maxResolution, opt_minResolution) { var minResolution = opt_minResolution || 0; - return function(resolution, delta) { + return function(resolution, delta, direction) { if (goog.isDef(resolution)) { resolution /= Math.pow(power, delta); return goog.math.clamp(resolution, minResolution, maxResolution); @@ -37,9 +37,9 @@ ol.ResolutionConstraint.createContinuous = */ ol.ResolutionConstraint.createSnapToResolutions = function(resolutions) { - return function(resolution, delta) { + return function(resolution, delta, direction) { if (goog.isDef(resolution)) { - var z = ol.array.linearFindNearest(resolutions, resolution); + var z = ol.array.linearFindNearest(resolutions, resolution, direction); z = goog.math.clamp(z + delta, 0, resolutions.length - 1); return resolutions[z]; } else { @@ -57,10 +57,18 @@ ol.ResolutionConstraint.createSnapToResolutions = */ ol.ResolutionConstraint.createSnapToPower = function(power, maxResolution, opt_maxLevel) { - return function(resolution, delta) { + return function(resolution, delta, direction) { if (goog.isDef(resolution)) { + var offset; + if (direction > 0) { + offset = 0; + } else if (direction < 0) { + offset = 1; + } else { + offset = 0.5; + } var oldLevel = Math.floor( - Math.log(maxResolution / resolution) / Math.log(power) + 0.5); + Math.log(maxResolution / resolution) / Math.log(power) + offset); var newLevel = Math.max(oldLevel + delta, 0); if (goog.isDef(opt_maxLevel)) { newLevel = Math.min(newLevel, opt_maxLevel); diff --git a/src/ol/source/imagesource.js b/src/ol/source/imagesource.js index 417de189fe..8935dc541d 100644 --- a/src/ol/source/imagesource.js +++ b/src/ol/source/imagesource.js @@ -100,7 +100,7 @@ ol.source.ImageSource.prototype.createImage = ol.source.ImageSource.prototype.findNearestResolution = function(resolution) { if (!goog.isNull(this.resolutions_)) { - var idx = ol.array.linearFindNearest(this.resolutions_, resolution); + var idx = ol.array.linearFindNearest(this.resolutions_, resolution, 0); resolution = this.resolutions_[idx]; } return resolution; diff --git a/src/ol/tilegrid/tilegrid.js b/src/ol/tilegrid/tilegrid.js index 0c1d0fbbb2..cf54a595dd 100644 --- a/src/ol/tilegrid/tilegrid.js +++ b/src/ol/tilegrid/tilegrid.js @@ -329,7 +329,7 @@ ol.tilegrid.TileGrid.prototype.getTileSize = function(z) { * @return {number} Z. */ ol.tilegrid.TileGrid.prototype.getZForResolution = function(resolution) { - return ol.array.linearFindNearest(this.resolutions_, resolution); + return ol.array.linearFindNearest(this.resolutions_, resolution, 0); }; diff --git a/src/ol/view2d.js b/src/ol/view2d.js index d33ac86875..a2cffd78e3 100644 --- a/src/ol/view2d.js +++ b/src/ol/view2d.js @@ -193,7 +193,7 @@ ol.View2D.prototype.getView3D = function() { ol.View2D.prototype.fitExtent = function(extent, size) { this.setCenter(extent.getCenter()); var resolution = this.getResolutionForExtent(extent, size); - resolution = this.constraints_.resolution(resolution, 0); + resolution = this.constraints_.resolution(resolution, 0, 0); this.setResolution(resolution); }; @@ -342,10 +342,19 @@ ol.View2D.prototype.rotateWithoutConstraints = * @param {number|undefined} resolution Resolution to go to. * @param {ol.Coordinate=} opt_anchor Anchor coordinate. * @param {number=} opt_duration Duration. + * @param {number=} opt_direction Zooming direction; > 0 indicates + * zooming out, in which case the constraints system will select + * the largest nearest resolution; < 0 indicates zooming in, in + * which case the constraints system will select the smallest + * nearest resolution; == 0 indicates that the zooming direction + * is unknown/not relevant, in which case the constraints system + * will select the nearest resolution. If not defined 0 is + * assumed. */ ol.View2D.prototype.zoom = - function(map, resolution, opt_anchor, opt_duration) { - resolution = this.constraints_.resolution(resolution, 0); + function(map, resolution, opt_anchor, opt_duration, opt_direction) { + var direction = opt_direction || 0; + resolution = this.constraints_.resolution(resolution, 0, direction); this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration); }; @@ -359,7 +368,7 @@ ol.View2D.prototype.zoom = ol.View2D.prototype.zoomByDelta = function(map, delta, opt_anchor, opt_duration) { var currentResolution = this.getResolution(); - var resolution = this.constraints_.resolution(currentResolution, delta); + var resolution = this.constraints_.resolution(currentResolution, delta, 0); this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration); }; diff --git a/test/spec/ol/array.test.js b/test/spec/ol/array.test.js index fade150e9c..740b77c8c1 100644 --- a/test/spec/ol/array.test.js +++ b/test/spec/ol/array.test.js @@ -28,21 +28,49 @@ describe('ol.array', function() { it('returns expected value', function() { var arr = [1000, 500, 100]; - expect(ol.array.linearFindNearest(arr, 10000)).to.eql(0); - expect(ol.array.linearFindNearest(arr, 1000)).to.eql(0); - expect(ol.array.linearFindNearest(arr, 900)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 10000, 0)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 10000, 1)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 10000, -1)).to.eql(0); - expect(ol.array.linearFindNearest(arr, 750)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 1000, 0)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 1000, 1)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 1000, -1)).to.eql(0); - expect(ol.array.linearFindNearest(arr, 550)).to.eql(1); - expect(ol.array.linearFindNearest(arr, 500)).to.eql(1); - expect(ol.array.linearFindNearest(arr, 450)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 900, 0)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 900, 1)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 900, -1)).to.eql(1); - expect(ol.array.linearFindNearest(arr, 300)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 750, 0)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 750, 1)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 750, -1)).to.eql(1); - expect(ol.array.linearFindNearest(arr, 200)).to.eql(2); - expect(ol.array.linearFindNearest(arr, 100)).to.eql(2); - expect(ol.array.linearFindNearest(arr, 50)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 550, 0)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 550, 1)).to.eql(0); + expect(ol.array.linearFindNearest(arr, 550, -1)).to.eql(1); + + expect(ol.array.linearFindNearest(arr, 500, 0)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 500, 1)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 500, -1)).to.eql(1); + + expect(ol.array.linearFindNearest(arr, 450, 0)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 450, 1)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 450, -1)).to.eql(2); + + expect(ol.array.linearFindNearest(arr, 300, 0)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 300, 1)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 300, -1)).to.eql(2); + + expect(ol.array.linearFindNearest(arr, 200, 0)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 200, 1)).to.eql(1); + expect(ol.array.linearFindNearest(arr, 200, -1)).to.eql(2); + + expect(ol.array.linearFindNearest(arr, 100, 0)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 100, 1)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 100, -1)).to.eql(2); + + expect(ol.array.linearFindNearest(arr, 50, 0)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 50, 1)).to.eql(2); + expect(ol.array.linearFindNearest(arr, 50, -1)).to.eql(2); }); }); }); diff --git a/test/spec/ol/resolutionconstraint.test.js b/test/spec/ol/resolutionconstraint.test.js index 1a37bfdce4..b89df9bab5 100644 --- a/test/spec/ol/resolutionconstraint.test.js +++ b/test/spec/ol/resolutionconstraint.test.js @@ -13,28 +13,28 @@ describe('ol.ResolutionConstraint', function() { describe('delta 0', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1000, 0)).to.eql(1000); - expect(resolutionConstraint(500, 0)).to.eql(500); - expect(resolutionConstraint(250, 0)).to.eql(250); - expect(resolutionConstraint(100, 0)).to.eql(100); + expect(resolutionConstraint(1000, 0, 0)).to.eql(1000); + expect(resolutionConstraint(500, 0, 0)).to.eql(500); + expect(resolutionConstraint(250, 0, 0)).to.eql(250); + expect(resolutionConstraint(100, 0, 0)).to.eql(100); }); }); describe('zoom in', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1000, 1)).to.eql(500); - expect(resolutionConstraint(500, 1)).to.eql(250); - expect(resolutionConstraint(250, 1)).to.eql(100); - expect(resolutionConstraint(100, 1)).to.eql(100); + expect(resolutionConstraint(1000, 1, 0)).to.eql(500); + expect(resolutionConstraint(500, 1, 0)).to.eql(250); + expect(resolutionConstraint(250, 1, 0)).to.eql(100); + expect(resolutionConstraint(100, 1, 0)).to.eql(100); }); }); describe('zoom out', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1000, -1)).to.eql(1000); - expect(resolutionConstraint(500, -1)).to.eql(1000); - expect(resolutionConstraint(250, -1)).to.eql(500); - expect(resolutionConstraint(100, -1)).to.eql(250); + expect(resolutionConstraint(1000, -1, 0)).to.eql(1000); + expect(resolutionConstraint(500, -1, 0)).to.eql(1000); + expect(resolutionConstraint(250, -1, 0)).to.eql(500); + expect(resolutionConstraint(100, -1, 0)).to.eql(250); }); }); }); @@ -51,40 +51,40 @@ describe('ol.ResolutionConstraint', function() { describe('delta 0', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1050, 0)).to.eql(1000); - expect(resolutionConstraint(950, 0)).to.eql(1000); - expect(resolutionConstraint(550, 0)).to.eql(500); - expect(resolutionConstraint(400, 0)).to.eql(500); - expect(resolutionConstraint(300, 0)).to.eql(250); - expect(resolutionConstraint(200, 0)).to.eql(250); - expect(resolutionConstraint(150, 0)).to.eql(100); - expect(resolutionConstraint(50, 0)).to.eql(100); + expect(resolutionConstraint(1050, 0, 0)).to.eql(1000); + expect(resolutionConstraint(950, 0, 0)).to.eql(1000); + expect(resolutionConstraint(550, 0, 0)).to.eql(500); + expect(resolutionConstraint(400, 0, 0)).to.eql(500); + expect(resolutionConstraint(300, 0, 0)).to.eql(250); + expect(resolutionConstraint(200, 0, 0)).to.eql(250); + expect(resolutionConstraint(150, 0, 0)).to.eql(100); + expect(resolutionConstraint(50, 0, 0)).to.eql(100); }); }); describe('zoom in', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1050, 1)).to.eql(500); - expect(resolutionConstraint(950, 1)).to.eql(500); - expect(resolutionConstraint(550, 1)).to.eql(250); - expect(resolutionConstraint(450, 1)).to.eql(250); - expect(resolutionConstraint(300, 1)).to.eql(100); - expect(resolutionConstraint(200, 1)).to.eql(100); - expect(resolutionConstraint(150, 1)).to.eql(100); - expect(resolutionConstraint(50, 1)).to.eql(100); + expect(resolutionConstraint(1050, 1, 0)).to.eql(500); + expect(resolutionConstraint(950, 1, 0)).to.eql(500); + expect(resolutionConstraint(550, 1, 0)).to.eql(250); + expect(resolutionConstraint(450, 1, 0)).to.eql(250); + expect(resolutionConstraint(300, 1, 0)).to.eql(100); + expect(resolutionConstraint(200, 1, 0)).to.eql(100); + expect(resolutionConstraint(150, 1, 0)).to.eql(100); + expect(resolutionConstraint(50, 1, 0)).to.eql(100); }); }); describe('zoom out', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1050, -1)).to.eql(1000); - expect(resolutionConstraint(950, -1)).to.eql(1000); - expect(resolutionConstraint(550, -1)).to.eql(1000); - expect(resolutionConstraint(450, -1)).to.eql(1000); - expect(resolutionConstraint(300, -1)).to.eql(500); - expect(resolutionConstraint(200, -1)).to.eql(500); - expect(resolutionConstraint(150, -1)).to.eql(250); - expect(resolutionConstraint(50, -1)).to.eql(250); + expect(resolutionConstraint(1050, -1, 0)).to.eql(1000); + expect(resolutionConstraint(950, -1, 0)).to.eql(1000); + expect(resolutionConstraint(550, -1, 0)).to.eql(1000); + expect(resolutionConstraint(450, -1, 0)).to.eql(1000); + expect(resolutionConstraint(300, -1, 0)).to.eql(500); + expect(resolutionConstraint(200, -1, 0)).to.eql(500); + expect(resolutionConstraint(150, -1, 0)).to.eql(250); + expect(resolutionConstraint(50, -1, 0)).to.eql(250); }); }); }); @@ -100,49 +100,49 @@ describe('ol.ResolutionConstraint', function() { describe('delta 0', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1024, 0)).to.eql(1024); - expect(resolutionConstraint(512, 0)).to.eql(512); - expect(resolutionConstraint(256, 0)).to.eql(256); - expect(resolutionConstraint(128, 0)).to.eql(128); - expect(resolutionConstraint(64, 0)).to.eql(64); - expect(resolutionConstraint(32, 0)).to.eql(32); - expect(resolutionConstraint(16, 0)).to.eql(16); - expect(resolutionConstraint(8, 0)).to.eql(8); - expect(resolutionConstraint(4, 0)).to.eql(4); - expect(resolutionConstraint(2, 0)).to.eql(2); - expect(resolutionConstraint(1, 0)).to.eql(1); + expect(resolutionConstraint(1024, 0, 0)).to.eql(1024); + expect(resolutionConstraint(512, 0, 0)).to.eql(512); + expect(resolutionConstraint(256, 0, 0)).to.eql(256); + expect(resolutionConstraint(128, 0, 0)).to.eql(128); + expect(resolutionConstraint(64, 0, 0)).to.eql(64); + expect(resolutionConstraint(32, 0, 0)).to.eql(32); + expect(resolutionConstraint(16, 0, 0)).to.eql(16); + expect(resolutionConstraint(8, 0, 0)).to.eql(8); + expect(resolutionConstraint(4, 0, 0)).to.eql(4); + expect(resolutionConstraint(2, 0, 0)).to.eql(2); + expect(resolutionConstraint(1, 0, 0)).to.eql(1); }); }); describe('zoom in', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1024, 1)).to.eql(512); - expect(resolutionConstraint(512, 1)).to.eql(256); - expect(resolutionConstraint(256, 1)).to.eql(128); - expect(resolutionConstraint(128, 1)).to.eql(64); - expect(resolutionConstraint(64, 1)).to.eql(32); - expect(resolutionConstraint(32, 1)).to.eql(16); - expect(resolutionConstraint(16, 1)).to.eql(8); - expect(resolutionConstraint(8, 1)).to.eql(4); - expect(resolutionConstraint(4, 1)).to.eql(2); - expect(resolutionConstraint(2, 1)).to.eql(1); - expect(resolutionConstraint(1, 1)).to.eql(1); + expect(resolutionConstraint(1024, 1, 0)).to.eql(512); + expect(resolutionConstraint(512, 1, 0)).to.eql(256); + expect(resolutionConstraint(256, 1, 0)).to.eql(128); + expect(resolutionConstraint(128, 1, 0)).to.eql(64); + expect(resolutionConstraint(64, 1, 0)).to.eql(32); + expect(resolutionConstraint(32, 1, 0)).to.eql(16); + expect(resolutionConstraint(16, 1, 0)).to.eql(8); + expect(resolutionConstraint(8, 1, 0)).to.eql(4); + expect(resolutionConstraint(4, 1, 0)).to.eql(2); + expect(resolutionConstraint(2, 1, 0)).to.eql(1); + expect(resolutionConstraint(1, 1, 0)).to.eql(1); }); }); describe('zoom out', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1024, -1)).to.eql(1024); - expect(resolutionConstraint(512, -1)).to.eql(1024); - expect(resolutionConstraint(256, -1)).to.eql(512); - expect(resolutionConstraint(128, -1)).to.eql(256); - expect(resolutionConstraint(64, -1)).to.eql(128); - expect(resolutionConstraint(32, -1)).to.eql(64); - expect(resolutionConstraint(16, -1)).to.eql(32); - expect(resolutionConstraint(8, -1)).to.eql(16); - expect(resolutionConstraint(4, -1)).to.eql(8); - expect(resolutionConstraint(2, -1)).to.eql(4); - expect(resolutionConstraint(1, -1)).to.eql(2); + expect(resolutionConstraint(1024, -1, 0)).to.eql(1024); + expect(resolutionConstraint(512, -1, 0)).to.eql(1024); + expect(resolutionConstraint(256, -1, 0)).to.eql(512); + expect(resolutionConstraint(128, -1, 0)).to.eql(256); + expect(resolutionConstraint(64, -1, 0)).to.eql(128); + expect(resolutionConstraint(32, -1, 0)).to.eql(64); + expect(resolutionConstraint(16, -1, 0)).to.eql(32); + expect(resolutionConstraint(8, -1, 0)).to.eql(16); + expect(resolutionConstraint(4, -1, 0)).to.eql(8); + expect(resolutionConstraint(2, -1, 0)).to.eql(4); + expect(resolutionConstraint(1, -1, 0)).to.eql(2); }); }); }); @@ -156,30 +156,84 @@ describe('ol.ResolutionConstraint', function() { ol.ResolutionConstraint.createSnapToPower(2, 1024, 10); }); - describe('delta 0', function() { + describe('delta 0, direction 0', function() { it('returns expected resolution value', function() { - expect(resolutionConstraint(1050, 0)).to.eql(1024); - expect(resolutionConstraint(9050, 0)).to.eql(1024); - expect(resolutionConstraint(550, 0)).to.eql(512); - expect(resolutionConstraint(450, 0)).to.eql(512); - expect(resolutionConstraint(300, 0)).to.eql(256); - expect(resolutionConstraint(250, 0)).to.eql(256); - expect(resolutionConstraint(150, 0)).to.eql(128); - expect(resolutionConstraint(100, 0)).to.eql(128); - expect(resolutionConstraint(75, 0)).to.eql(64); - expect(resolutionConstraint(50, 0)).to.eql(64); - expect(resolutionConstraint(40, 0)).to.eql(32); - expect(resolutionConstraint(30, 0)).to.eql(32); - expect(resolutionConstraint(20, 0)).to.eql(16); - expect(resolutionConstraint(12, 0)).to.eql(16); - expect(resolutionConstraint(9, 0)).to.eql(8); - expect(resolutionConstraint(7, 0)).to.eql(8); - expect(resolutionConstraint(5, 0)).to.eql(4); - expect(resolutionConstraint(3.5, 0)).to.eql(4); - expect(resolutionConstraint(2.1, 0)).to.eql(2); - expect(resolutionConstraint(1.9, 0)).to.eql(2); - expect(resolutionConstraint(1.1, 0)).to.eql(1); - expect(resolutionConstraint(0.9, 0)).to.eql(1); + expect(resolutionConstraint(1050, 0, 0)).to.eql(1024); + expect(resolutionConstraint(9050, 0, 0)).to.eql(1024); + expect(resolutionConstraint(550, 0, 0)).to.eql(512); + expect(resolutionConstraint(450, 0, 0)).to.eql(512); + expect(resolutionConstraint(300, 0, 0)).to.eql(256); + expect(resolutionConstraint(250, 0, 0)).to.eql(256); + expect(resolutionConstraint(150, 0, 0)).to.eql(128); + expect(resolutionConstraint(100, 0, 0)).to.eql(128); + expect(resolutionConstraint(75, 0, 0)).to.eql(64); + expect(resolutionConstraint(50, 0, 0)).to.eql(64); + expect(resolutionConstraint(40, 0, 0)).to.eql(32); + expect(resolutionConstraint(30, 0, 0)).to.eql(32); + expect(resolutionConstraint(20, 0, 0)).to.eql(16); + expect(resolutionConstraint(12, 0, 0)).to.eql(16); + expect(resolutionConstraint(9, 0, 0)).to.eql(8); + expect(resolutionConstraint(7, 0, 0)).to.eql(8); + expect(resolutionConstraint(5, 0, 0)).to.eql(4); + expect(resolutionConstraint(3.5, 0, 0)).to.eql(4); + expect(resolutionConstraint(2.1, 0, 0)).to.eql(2); + expect(resolutionConstraint(1.9, 0, 0)).to.eql(2); + expect(resolutionConstraint(1.1, 0, 0)).to.eql(1); + expect(resolutionConstraint(0.9, 0, 0)).to.eql(1); + }); + }); + + describe('delta 0, direction > 0', function() { + it('returns expected resolution value', function() { + expect(resolutionConstraint(1050, 0, 1)).to.eql(1024); + expect(resolutionConstraint(9050, 0, 1)).to.eql(1024); + expect(resolutionConstraint(550, 0, 1)).to.eql(1024); + expect(resolutionConstraint(450, 0, 1)).to.eql(512); + expect(resolutionConstraint(300, 0, 1)).to.eql(512); + expect(resolutionConstraint(250, 0, 1)).to.eql(256); + expect(resolutionConstraint(150, 0, 1)).to.eql(256); + expect(resolutionConstraint(100, 0, 1)).to.eql(128); + expect(resolutionConstraint(75, 0, 1)).to.eql(128); + expect(resolutionConstraint(50, 0, 1)).to.eql(64); + expect(resolutionConstraint(40, 0, 1)).to.eql(64); + expect(resolutionConstraint(30, 0, 1)).to.eql(32); + expect(resolutionConstraint(20, 0, 1)).to.eql(32); + expect(resolutionConstraint(12, 0, 1)).to.eql(16); + expect(resolutionConstraint(9, 0, 1)).to.eql(16); + expect(resolutionConstraint(7, 0, 1)).to.eql(8); + expect(resolutionConstraint(5, 0, 1)).to.eql(8); + expect(resolutionConstraint(3.5, 0, 1)).to.eql(4); + expect(resolutionConstraint(2.1, 0, 1)).to.eql(4); + expect(resolutionConstraint(1.9, 0, 1)).to.eql(2); + expect(resolutionConstraint(1.1, 0, 1)).to.eql(2); + expect(resolutionConstraint(0.9, 0, 1)).to.eql(1); + }); + }); + + describe('delta 0, direction < 0', function() { + it('returns expected resolution value', function() { + expect(resolutionConstraint(1050, 0, -1)).to.eql(1024); + expect(resolutionConstraint(9050, 0, -1)).to.eql(1024); + expect(resolutionConstraint(550, 0, -1)).to.eql(512); + expect(resolutionConstraint(450, 0, -1)).to.eql(256); + expect(resolutionConstraint(300, 0, -1)).to.eql(256); + expect(resolutionConstraint(250, 0, -1)).to.eql(128); + expect(resolutionConstraint(150, 0, -1)).to.eql(128); + expect(resolutionConstraint(100, 0, -1)).to.eql(64); + expect(resolutionConstraint(75, 0, -1)).to.eql(64); + expect(resolutionConstraint(50, 0, -1)).to.eql(32); + expect(resolutionConstraint(40, 0, -1)).to.eql(32); + expect(resolutionConstraint(30, 0, -1)).to.eql(16); + expect(resolutionConstraint(20, 0, -1)).to.eql(16); + expect(resolutionConstraint(12, 0, -1)).to.eql(8); + expect(resolutionConstraint(9, 0, -1)).to.eql(8); + expect(resolutionConstraint(7, 0, -1)).to.eql(4); + expect(resolutionConstraint(5, 0, -1)).to.eql(4); + expect(resolutionConstraint(3.5, 0, -1)).to.eql(2); + expect(resolutionConstraint(2.1, 0, -1)).to.eql(2); + expect(resolutionConstraint(1.9, 0, -1)).to.eql(1); + expect(resolutionConstraint(1.1, 0, -1)).to.eql(1); + expect(resolutionConstraint(0.9, 0, -1)).to.eql(1); }); }); }); diff --git a/test/spec/ol/view2d.test.js b/test/spec/ol/view2d.test.js index eae7b4a123..f95e90fb72 100644 --- a/test/spec/ol/view2d.test.js +++ b/test/spec/ol/view2d.test.js @@ -9,9 +9,9 @@ describe('ol.View2D', function() { it('gives a correct resolution constraint function', function() { var options = {}; var fn = ol.View2D.createConstraints_(options).resolution; - expect(fn(156543.03392804097, 0)) + expect(fn(156543.03392804097, 0, 0)) .to.roughlyEqual(156543.03392804097, 1e-9); - expect(fn(78271.51696402048, 0)) + expect(fn(78271.51696402048, 0, 0)) .to.roughlyEqual(78271.51696402048, 1e-10); }); }); @@ -25,12 +25,12 @@ describe('ol.View2D', function() { zoomFactor: 3 }; var fn = ol.View2D.createConstraints_(options).resolution; - expect(fn(82, 0)).to.eql(81); - expect(fn(81, 0)).to.eql(81); - expect(fn(27, 0)).to.eql(27); - expect(fn(9, 0)).to.eql(9); - expect(fn(3, 0)).to.eql(3); - expect(fn(2, 0)).to.eql(3); + expect(fn(82, 0, 0)).to.eql(81); + expect(fn(81, 0, 0)).to.eql(81); + expect(fn(27, 0, 0)).to.eql(27); + expect(fn(9, 0, 0)).to.eql(9); + expect(fn(3, 0, 0)).to.eql(3); + expect(fn(2, 0, 0)).to.eql(3); }); }); @@ -40,11 +40,11 @@ describe('ol.View2D', function() { resolutions: [97, 76, 65, 54, 0.45] }; var fn = ol.View2D.createConstraints_(options).resolution; - expect(fn(97, 0)).to.eql(97); - expect(fn(76, 0)).to.eql(76); - expect(fn(65, 0)).to.eql(65); - expect(fn(54, 0)).to.eql(54); - expect(fn(0.45, 0)).to.eql(0.45); + expect(fn(97, 0, 0)).to.eql(97); + expect(fn(76, 0, 0)).to.eql(76); + expect(fn(65, 0, 0)).to.eql(65); + expect(fn(54, 0, 0)).to.eql(54); + expect(fn(0.45, 0, 0)).to.eql(0.45); }); });