One direction pinch zoom

This commit is contained in:
Éric Lemoine
2013-03-25 18:15:00 +01:00
parent 588e0c1cdc
commit de1575e457
9 changed files with 269 additions and 137 deletions

View File

@@ -33,9 +33,12 @@ ol.array.binaryFindNearest = function(arr, target) {
/** /**
* @param {Array.<number>} arr Array. * @param {Array.<number>} arr Array.
* @param {number} target Target. * @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. * @return {number} Index.
*/ */
ol.array.linearFindNearest = function(arr, target) { ol.array.linearFindNearest = function(arr, target, direction) {
var n = arr.length; var n = arr.length;
if (arr[0] <= target) { if (arr[0] <= target) {
return 0; return 0;
@@ -43,17 +46,34 @@ ol.array.linearFindNearest = function(arr, target) {
return n - 1; return n - 1;
} else { } else {
var i; var i;
for (i = 1; i < n; ++i) { if (direction > 0) {
if (arr[i] == target) { for (i = 1; i < n; ++i) {
return i; if (arr[i] < target) {
} else if (arr[i] < target) {
if (arr[i - 1] - target < target - arr[i]) {
return i - 1; return i - 1;
} else { }
}
} else if (direction < 0) {
for (i = 1; i < n; ++i) {
if (arr[i] <= target) {
return i; 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; return n - 1;
} }
}; };

View File

@@ -29,6 +29,12 @@ ol.interaction.TouchZoom = function() {
*/ */
this.lastDistance_; this.lastDistance_;
/**
* @private
* @type {number}
*/
this.lastScaleDelta_ = 1;
}; };
goog.inherits(ol.interaction.TouchZoom, ol.interaction.Touch); goog.inherits(ol.interaction.TouchZoom, ol.interaction.Touch);
@@ -53,6 +59,9 @@ ol.interaction.TouchZoom.prototype.handleTouchMove =
scaleDelta = this.lastDistance_ / distance; scaleDelta = this.lastDistance_ / distance;
} }
this.lastDistance_ = distance; this.lastDistance_ = distance;
if (scaleDelta != 1.0) {
this.lastScaleDelta_ = scaleDelta;
}
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var view = map.getView(); var view = map.getView();
@@ -78,9 +87,12 @@ ol.interaction.TouchZoom.prototype.handleTouchEnd =
if (this.targetTouches.length < 2) { if (this.targetTouches.length < 2) {
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var view = map.getView(); 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, view.zoom(map, view.getResolution(), undefined,
ol.interaction.TOUCHZOOM_ANIMATION_DURATION); ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction);
view.setHint(ol.ViewHint.INTERACTING, -1); view.setHint(ol.ViewHint.INTERACTING, -1);
return false; return false;
} else { } else {
@@ -97,6 +109,7 @@ ol.interaction.TouchZoom.prototype.handleTouchStart =
if (this.targetTouches.length >= 2) { if (this.targetTouches.length >= 2) {
var view = mapBrowserEvent.map.getView(); var view = mapBrowserEvent.map.getView();
this.lastDistance_ = undefined; this.lastDistance_ = undefined;
this.lastScaleDelta_ = 1;
view.setHint(ol.ViewHint.INTERACTING, 1); view.setHint(ol.ViewHint.INTERACTING, 1);
return true; return true;
} else { } else {

View File

@@ -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; ol.ResolutionConstraintType;
@@ -20,7 +20,7 @@ ol.ResolutionConstraintType;
ol.ResolutionConstraint.createContinuous = ol.ResolutionConstraint.createContinuous =
function(power, maxResolution, opt_minResolution) { function(power, maxResolution, opt_minResolution) {
var minResolution = opt_minResolution || 0; var minResolution = opt_minResolution || 0;
return function(resolution, delta) { return function(resolution, delta, direction) {
if (goog.isDef(resolution)) { if (goog.isDef(resolution)) {
resolution /= Math.pow(power, delta); resolution /= Math.pow(power, delta);
return goog.math.clamp(resolution, minResolution, maxResolution); return goog.math.clamp(resolution, minResolution, maxResolution);
@@ -37,9 +37,9 @@ ol.ResolutionConstraint.createContinuous =
*/ */
ol.ResolutionConstraint.createSnapToResolutions = ol.ResolutionConstraint.createSnapToResolutions =
function(resolutions) { function(resolutions) {
return function(resolution, delta) { return function(resolution, delta, direction) {
if (goog.isDef(resolution)) { 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); z = goog.math.clamp(z + delta, 0, resolutions.length - 1);
return resolutions[z]; return resolutions[z];
} else { } else {
@@ -57,10 +57,18 @@ ol.ResolutionConstraint.createSnapToResolutions =
*/ */
ol.ResolutionConstraint.createSnapToPower = ol.ResolutionConstraint.createSnapToPower =
function(power, maxResolution, opt_maxLevel) { function(power, maxResolution, opt_maxLevel) {
return function(resolution, delta) { return function(resolution, delta, direction) {
if (goog.isDef(resolution)) { 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( 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); var newLevel = Math.max(oldLevel + delta, 0);
if (goog.isDef(opt_maxLevel)) { if (goog.isDef(opt_maxLevel)) {
newLevel = Math.min(newLevel, opt_maxLevel); newLevel = Math.min(newLevel, opt_maxLevel);

View File

@@ -100,7 +100,7 @@ ol.source.ImageSource.prototype.createImage =
ol.source.ImageSource.prototype.findNearestResolution = ol.source.ImageSource.prototype.findNearestResolution =
function(resolution) { function(resolution) {
if (!goog.isNull(this.resolutions_)) { 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]; resolution = this.resolutions_[idx];
} }
return resolution; return resolution;

View File

@@ -329,7 +329,7 @@ ol.tilegrid.TileGrid.prototype.getTileSize = function(z) {
* @return {number} Z. * @return {number} Z.
*/ */
ol.tilegrid.TileGrid.prototype.getZForResolution = function(resolution) { ol.tilegrid.TileGrid.prototype.getZForResolution = function(resolution) {
return ol.array.linearFindNearest(this.resolutions_, resolution); return ol.array.linearFindNearest(this.resolutions_, resolution, 0);
}; };

View File

@@ -193,7 +193,7 @@ ol.View2D.prototype.getView3D = function() {
ol.View2D.prototype.fitExtent = function(extent, size) { ol.View2D.prototype.fitExtent = function(extent, size) {
this.setCenter(extent.getCenter()); this.setCenter(extent.getCenter());
var resolution = this.getResolutionForExtent(extent, size); var resolution = this.getResolutionForExtent(extent, size);
resolution = this.constraints_.resolution(resolution, 0); resolution = this.constraints_.resolution(resolution, 0, 0);
this.setResolution(resolution); this.setResolution(resolution);
}; };
@@ -342,10 +342,19 @@ ol.View2D.prototype.rotateWithoutConstraints =
* @param {number|undefined} resolution Resolution to go to. * @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate. * @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration. * @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 = ol.View2D.prototype.zoom =
function(map, resolution, opt_anchor, opt_duration) { function(map, resolution, opt_anchor, opt_duration, opt_direction) {
resolution = this.constraints_.resolution(resolution, 0); var direction = opt_direction || 0;
resolution = this.constraints_.resolution(resolution, 0, direction);
this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration); this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration);
}; };
@@ -359,7 +368,7 @@ ol.View2D.prototype.zoom =
ol.View2D.prototype.zoomByDelta = ol.View2D.prototype.zoomByDelta =
function(map, delta, opt_anchor, opt_duration) { function(map, delta, opt_anchor, opt_duration) {
var currentResolution = this.getResolution(); 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); this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration);
}; };

View File

@@ -28,21 +28,49 @@ describe('ol.array', function() {
it('returns expected value', function() { it('returns expected value', function() {
var arr = [1000, 500, 100]; var arr = [1000, 500, 100];
expect(ol.array.linearFindNearest(arr, 10000)).to.eql(0); expect(ol.array.linearFindNearest(arr, 10000, 0)).to.eql(0);
expect(ol.array.linearFindNearest(arr, 1000)).to.eql(0); expect(ol.array.linearFindNearest(arr, 10000, 1)).to.eql(0);
expect(ol.array.linearFindNearest(arr, 900)).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, 900, 0)).to.eql(0);
expect(ol.array.linearFindNearest(arr, 500)).to.eql(1); expect(ol.array.linearFindNearest(arr, 900, 1)).to.eql(0);
expect(ol.array.linearFindNearest(arr, 450)).to.eql(1); 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, 550, 0)).to.eql(1);
expect(ol.array.linearFindNearest(arr, 100)).to.eql(2); expect(ol.array.linearFindNearest(arr, 550, 1)).to.eql(0);
expect(ol.array.linearFindNearest(arr, 50)).to.eql(2); 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);
}); });
}); });
}); });

View File

@@ -13,28 +13,28 @@ describe('ol.ResolutionConstraint', function() {
describe('delta 0', function() { describe('delta 0', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, 0)).to.eql(1000); expect(resolutionConstraint(1000, 0, 0)).to.eql(1000);
expect(resolutionConstraint(500, 0)).to.eql(500); expect(resolutionConstraint(500, 0, 0)).to.eql(500);
expect(resolutionConstraint(250, 0)).to.eql(250); expect(resolutionConstraint(250, 0, 0)).to.eql(250);
expect(resolutionConstraint(100, 0)).to.eql(100); expect(resolutionConstraint(100, 0, 0)).to.eql(100);
}); });
}); });
describe('zoom in', function() { describe('zoom in', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, 1)).to.eql(500); expect(resolutionConstraint(1000, 1, 0)).to.eql(500);
expect(resolutionConstraint(500, 1)).to.eql(250); expect(resolutionConstraint(500, 1, 0)).to.eql(250);
expect(resolutionConstraint(250, 1)).to.eql(100); expect(resolutionConstraint(250, 1, 0)).to.eql(100);
expect(resolutionConstraint(100, 1)).to.eql(100); expect(resolutionConstraint(100, 1, 0)).to.eql(100);
}); });
}); });
describe('zoom out', function() { describe('zoom out', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, -1)).to.eql(1000); expect(resolutionConstraint(1000, -1, 0)).to.eql(1000);
expect(resolutionConstraint(500, -1)).to.eql(1000); expect(resolutionConstraint(500, -1, 0)).to.eql(1000);
expect(resolutionConstraint(250, -1)).to.eql(500); expect(resolutionConstraint(250, -1, 0)).to.eql(500);
expect(resolutionConstraint(100, -1)).to.eql(250); expect(resolutionConstraint(100, -1, 0)).to.eql(250);
}); });
}); });
}); });
@@ -51,40 +51,40 @@ describe('ol.ResolutionConstraint', function() {
describe('delta 0', function() { describe('delta 0', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0)).to.eql(1000); expect(resolutionConstraint(1050, 0, 0)).to.eql(1000);
expect(resolutionConstraint(950, 0)).to.eql(1000); expect(resolutionConstraint(950, 0, 0)).to.eql(1000);
expect(resolutionConstraint(550, 0)).to.eql(500); expect(resolutionConstraint(550, 0, 0)).to.eql(500);
expect(resolutionConstraint(400, 0)).to.eql(500); expect(resolutionConstraint(400, 0, 0)).to.eql(500);
expect(resolutionConstraint(300, 0)).to.eql(250); expect(resolutionConstraint(300, 0, 0)).to.eql(250);
expect(resolutionConstraint(200, 0)).to.eql(250); expect(resolutionConstraint(200, 0, 0)).to.eql(250);
expect(resolutionConstraint(150, 0)).to.eql(100); expect(resolutionConstraint(150, 0, 0)).to.eql(100);
expect(resolutionConstraint(50, 0)).to.eql(100); expect(resolutionConstraint(50, 0, 0)).to.eql(100);
}); });
}); });
describe('zoom in', function() { describe('zoom in', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 1)).to.eql(500); expect(resolutionConstraint(1050, 1, 0)).to.eql(500);
expect(resolutionConstraint(950, 1)).to.eql(500); expect(resolutionConstraint(950, 1, 0)).to.eql(500);
expect(resolutionConstraint(550, 1)).to.eql(250); expect(resolutionConstraint(550, 1, 0)).to.eql(250);
expect(resolutionConstraint(450, 1)).to.eql(250); expect(resolutionConstraint(450, 1, 0)).to.eql(250);
expect(resolutionConstraint(300, 1)).to.eql(100); expect(resolutionConstraint(300, 1, 0)).to.eql(100);
expect(resolutionConstraint(200, 1)).to.eql(100); expect(resolutionConstraint(200, 1, 0)).to.eql(100);
expect(resolutionConstraint(150, 1)).to.eql(100); expect(resolutionConstraint(150, 1, 0)).to.eql(100);
expect(resolutionConstraint(50, 1)).to.eql(100); expect(resolutionConstraint(50, 1, 0)).to.eql(100);
}); });
}); });
describe('zoom out', function() { describe('zoom out', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, -1)).to.eql(1000); expect(resolutionConstraint(1050, -1, 0)).to.eql(1000);
expect(resolutionConstraint(950, -1)).to.eql(1000); expect(resolutionConstraint(950, -1, 0)).to.eql(1000);
expect(resolutionConstraint(550, -1)).to.eql(1000); expect(resolutionConstraint(550, -1, 0)).to.eql(1000);
expect(resolutionConstraint(450, -1)).to.eql(1000); expect(resolutionConstraint(450, -1, 0)).to.eql(1000);
expect(resolutionConstraint(300, -1)).to.eql(500); expect(resolutionConstraint(300, -1, 0)).to.eql(500);
expect(resolutionConstraint(200, -1)).to.eql(500); expect(resolutionConstraint(200, -1, 0)).to.eql(500);
expect(resolutionConstraint(150, -1)).to.eql(250); expect(resolutionConstraint(150, -1, 0)).to.eql(250);
expect(resolutionConstraint(50, -1)).to.eql(250); expect(resolutionConstraint(50, -1, 0)).to.eql(250);
}); });
}); });
}); });
@@ -100,49 +100,49 @@ describe('ol.ResolutionConstraint', function() {
describe('delta 0', function() { describe('delta 0', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, 0)).to.eql(1024); expect(resolutionConstraint(1024, 0, 0)).to.eql(1024);
expect(resolutionConstraint(512, 0)).to.eql(512); expect(resolutionConstraint(512, 0, 0)).to.eql(512);
expect(resolutionConstraint(256, 0)).to.eql(256); expect(resolutionConstraint(256, 0, 0)).to.eql(256);
expect(resolutionConstraint(128, 0)).to.eql(128); expect(resolutionConstraint(128, 0, 0)).to.eql(128);
expect(resolutionConstraint(64, 0)).to.eql(64); expect(resolutionConstraint(64, 0, 0)).to.eql(64);
expect(resolutionConstraint(32, 0)).to.eql(32); expect(resolutionConstraint(32, 0, 0)).to.eql(32);
expect(resolutionConstraint(16, 0)).to.eql(16); expect(resolutionConstraint(16, 0, 0)).to.eql(16);
expect(resolutionConstraint(8, 0)).to.eql(8); expect(resolutionConstraint(8, 0, 0)).to.eql(8);
expect(resolutionConstraint(4, 0)).to.eql(4); expect(resolutionConstraint(4, 0, 0)).to.eql(4);
expect(resolutionConstraint(2, 0)).to.eql(2); expect(resolutionConstraint(2, 0, 0)).to.eql(2);
expect(resolutionConstraint(1, 0)).to.eql(1); expect(resolutionConstraint(1, 0, 0)).to.eql(1);
}); });
}); });
describe('zoom in', function() { describe('zoom in', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, 1)).to.eql(512); expect(resolutionConstraint(1024, 1, 0)).to.eql(512);
expect(resolutionConstraint(512, 1)).to.eql(256); expect(resolutionConstraint(512, 1, 0)).to.eql(256);
expect(resolutionConstraint(256, 1)).to.eql(128); expect(resolutionConstraint(256, 1, 0)).to.eql(128);
expect(resolutionConstraint(128, 1)).to.eql(64); expect(resolutionConstraint(128, 1, 0)).to.eql(64);
expect(resolutionConstraint(64, 1)).to.eql(32); expect(resolutionConstraint(64, 1, 0)).to.eql(32);
expect(resolutionConstraint(32, 1)).to.eql(16); expect(resolutionConstraint(32, 1, 0)).to.eql(16);
expect(resolutionConstraint(16, 1)).to.eql(8); expect(resolutionConstraint(16, 1, 0)).to.eql(8);
expect(resolutionConstraint(8, 1)).to.eql(4); expect(resolutionConstraint(8, 1, 0)).to.eql(4);
expect(resolutionConstraint(4, 1)).to.eql(2); expect(resolutionConstraint(4, 1, 0)).to.eql(2);
expect(resolutionConstraint(2, 1)).to.eql(1); expect(resolutionConstraint(2, 1, 0)).to.eql(1);
expect(resolutionConstraint(1, 1)).to.eql(1); expect(resolutionConstraint(1, 1, 0)).to.eql(1);
}); });
}); });
describe('zoom out', function() { describe('zoom out', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, -1)).to.eql(1024); expect(resolutionConstraint(1024, -1, 0)).to.eql(1024);
expect(resolutionConstraint(512, -1)).to.eql(1024); expect(resolutionConstraint(512, -1, 0)).to.eql(1024);
expect(resolutionConstraint(256, -1)).to.eql(512); expect(resolutionConstraint(256, -1, 0)).to.eql(512);
expect(resolutionConstraint(128, -1)).to.eql(256); expect(resolutionConstraint(128, -1, 0)).to.eql(256);
expect(resolutionConstraint(64, -1)).to.eql(128); expect(resolutionConstraint(64, -1, 0)).to.eql(128);
expect(resolutionConstraint(32, -1)).to.eql(64); expect(resolutionConstraint(32, -1, 0)).to.eql(64);
expect(resolutionConstraint(16, -1)).to.eql(32); expect(resolutionConstraint(16, -1, 0)).to.eql(32);
expect(resolutionConstraint(8, -1)).to.eql(16); expect(resolutionConstraint(8, -1, 0)).to.eql(16);
expect(resolutionConstraint(4, -1)).to.eql(8); expect(resolutionConstraint(4, -1, 0)).to.eql(8);
expect(resolutionConstraint(2, -1)).to.eql(4); expect(resolutionConstraint(2, -1, 0)).to.eql(4);
expect(resolutionConstraint(1, -1)).to.eql(2); expect(resolutionConstraint(1, -1, 0)).to.eql(2);
}); });
}); });
}); });
@@ -156,30 +156,84 @@ describe('ol.ResolutionConstraint', function() {
ol.ResolutionConstraint.createSnapToPower(2, 1024, 10); ol.ResolutionConstraint.createSnapToPower(2, 1024, 10);
}); });
describe('delta 0', function() { describe('delta 0, direction 0', function() {
it('returns expected resolution value', function() { it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0)).to.eql(1024); expect(resolutionConstraint(1050, 0, 0)).to.eql(1024);
expect(resolutionConstraint(9050, 0)).to.eql(1024); expect(resolutionConstraint(9050, 0, 0)).to.eql(1024);
expect(resolutionConstraint(550, 0)).to.eql(512); expect(resolutionConstraint(550, 0, 0)).to.eql(512);
expect(resolutionConstraint(450, 0)).to.eql(512); expect(resolutionConstraint(450, 0, 0)).to.eql(512);
expect(resolutionConstraint(300, 0)).to.eql(256); expect(resolutionConstraint(300, 0, 0)).to.eql(256);
expect(resolutionConstraint(250, 0)).to.eql(256); expect(resolutionConstraint(250, 0, 0)).to.eql(256);
expect(resolutionConstraint(150, 0)).to.eql(128); expect(resolutionConstraint(150, 0, 0)).to.eql(128);
expect(resolutionConstraint(100, 0)).to.eql(128); expect(resolutionConstraint(100, 0, 0)).to.eql(128);
expect(resolutionConstraint(75, 0)).to.eql(64); expect(resolutionConstraint(75, 0, 0)).to.eql(64);
expect(resolutionConstraint(50, 0)).to.eql(64); expect(resolutionConstraint(50, 0, 0)).to.eql(64);
expect(resolutionConstraint(40, 0)).to.eql(32); expect(resolutionConstraint(40, 0, 0)).to.eql(32);
expect(resolutionConstraint(30, 0)).to.eql(32); expect(resolutionConstraint(30, 0, 0)).to.eql(32);
expect(resolutionConstraint(20, 0)).to.eql(16); expect(resolutionConstraint(20, 0, 0)).to.eql(16);
expect(resolutionConstraint(12, 0)).to.eql(16); expect(resolutionConstraint(12, 0, 0)).to.eql(16);
expect(resolutionConstraint(9, 0)).to.eql(8); expect(resolutionConstraint(9, 0, 0)).to.eql(8);
expect(resolutionConstraint(7, 0)).to.eql(8); expect(resolutionConstraint(7, 0, 0)).to.eql(8);
expect(resolutionConstraint(5, 0)).to.eql(4); expect(resolutionConstraint(5, 0, 0)).to.eql(4);
expect(resolutionConstraint(3.5, 0)).to.eql(4); expect(resolutionConstraint(3.5, 0, 0)).to.eql(4);
expect(resolutionConstraint(2.1, 0)).to.eql(2); expect(resolutionConstraint(2.1, 0, 0)).to.eql(2);
expect(resolutionConstraint(1.9, 0)).to.eql(2); expect(resolutionConstraint(1.9, 0, 0)).to.eql(2);
expect(resolutionConstraint(1.1, 0)).to.eql(1); expect(resolutionConstraint(1.1, 0, 0)).to.eql(1);
expect(resolutionConstraint(0.9, 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);
}); });
}); });
}); });

View File

@@ -9,9 +9,9 @@ describe('ol.View2D', function() {
it('gives a correct resolution constraint function', function() { it('gives a correct resolution constraint function', function() {
var options = {}; var options = {};
var fn = ol.View2D.createConstraints_(options).resolution; var fn = ol.View2D.createConstraints_(options).resolution;
expect(fn(156543.03392804097, 0)) expect(fn(156543.03392804097, 0, 0))
.to.roughlyEqual(156543.03392804097, 1e-9); .to.roughlyEqual(156543.03392804097, 1e-9);
expect(fn(78271.51696402048, 0)) expect(fn(78271.51696402048, 0, 0))
.to.roughlyEqual(78271.51696402048, 1e-10); .to.roughlyEqual(78271.51696402048, 1e-10);
}); });
}); });
@@ -25,12 +25,12 @@ describe('ol.View2D', function() {
zoomFactor: 3 zoomFactor: 3
}; };
var fn = ol.View2D.createConstraints_(options).resolution; var fn = ol.View2D.createConstraints_(options).resolution;
expect(fn(82, 0)).to.eql(81); expect(fn(82, 0, 0)).to.eql(81);
expect(fn(81, 0)).to.eql(81); expect(fn(81, 0, 0)).to.eql(81);
expect(fn(27, 0)).to.eql(27); expect(fn(27, 0, 0)).to.eql(27);
expect(fn(9, 0)).to.eql(9); expect(fn(9, 0, 0)).to.eql(9);
expect(fn(3, 0)).to.eql(3); expect(fn(3, 0, 0)).to.eql(3);
expect(fn(2, 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] resolutions: [97, 76, 65, 54, 0.45]
}; };
var fn = ol.View2D.createConstraints_(options).resolution; var fn = ol.View2D.createConstraints_(options).resolution;
expect(fn(97, 0)).to.eql(97); expect(fn(97, 0, 0)).to.eql(97);
expect(fn(76, 0)).to.eql(76); expect(fn(76, 0, 0)).to.eql(76);
expect(fn(65, 0)).to.eql(65); expect(fn(65, 0, 0)).to.eql(65);
expect(fn(54, 0)).to.eql(54); expect(fn(54, 0, 0)).to.eql(54);
expect(fn(0.45, 0)).to.eql(0.45); expect(fn(0.45, 0, 0)).to.eql(0.45);
}); });
}); });