Skip to content

Commit

Permalink
fix maxBounds constrains, close #1539
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Sep 24, 2015
1 parent 1be67c8 commit 8967b8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
17 changes: 12 additions & 5 deletions js/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,19 @@ Transform.prototype = {
get zoom() { return this._zoom; },
set zoom(zoom) {
zoom = Math.min(Math.max(zoom, this.minZoom), this.maxZoom);
if (this._zoom === zoom) return;
this._zoom = zoom;
this.scale = this.zoomScale(zoom);
this.tileZoom = Math.floor(zoom);
this.zoomFraction = zoom - this.tileZoom;
this._constrain();
},

get center() { return this._center; },
set center(center) {
this._center = center;
this._constrain();
},

zoomScale: function(zoom) { return Math.pow(2, zoom); },
scaleZoom: function(scale) { return Math.log(scale) / Math.LN2; },

Expand Down Expand Up @@ -145,7 +150,6 @@ Transform.prototype = {
panBy: function(offset) {
var point = this.centerPoint._add(offset);
this.center = this.pointLocation(point);
this._constrain();
},

setLocationAtPoint: function(lnglat, point) {
Expand All @@ -155,8 +159,6 @@ Transform.prototype = {

var translate = coordAtPoint._sub(c);
this.center = this.coordinateLocation(coordCenter._sub(translate));

this._constrain();
},

setZoomAround: function(zoom, center) {
Expand Down Expand Up @@ -291,7 +293,9 @@ Transform.prototype = {
},

_constrain: function() {
if (!this.center || !this.width || !this.height) return;
if (!this.center || !this.width || !this.height || this._constraining) return;

this._constraining = true;

var minY, maxY, minX, maxX, sy, sx, x2, y2,
size = this.size;
Expand All @@ -316,6 +320,7 @@ Transform.prototype = {
sx ? (maxX + minX) / 2 : this.x,
sy ? (maxY + minY) / 2 : this.y));
this.zoom += this.scaleZoom(s);
this._constraining = false;
return;
}

Expand All @@ -341,6 +346,8 @@ Transform.prototype = {
x2 !== undefined ? x2 : this.x,
y2 !== undefined ? y2 : this.y));
}

this._constraining = false;
},

getProjMatrix: function() {
Expand Down
22 changes: 17 additions & 5 deletions test/js/geo/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,26 @@ test('transform', function(t) {
t.end();
});

t.test('lngRange', function(t) {
t.test('lngRange & latRange constrain zoom and center', function(t) {
var transform = new Transform();
transform.center = new LngLat(0, 0);
transform.zoom = 10;
transform.width = 500;
transform.height = 500;
transform.lngRange = [-10, 10];
t.equal(transform.tileZoom, 0);
t.equal(transform.tileZoom, transform.zoom);
t.equal(transform.zoom, 0);

transform.lngRange = [-5, 5];
transform.latRange = [-5, 5];

transform.zoom = 0;
t.equal(transform.zoom, 5.135709286104402);

transform.center = new LngLat(-50, -30);
t.same(transform.center, new LngLat(0, -0.006358305286099153));

transform.zoom = 10;
transform.center = new LngLat(-50, -30);
t.same(transform.center, new LngLat(-4.828338623046875, -4.828969771321582));

t.end();
});
});
Expand Down

0 comments on commit 8967b8c

Please sign in to comment.