Skip to content

Commit

Permalink
Merge pull request #11 from eliotjordan/fix-initial-center
Browse files Browse the repository at this point in the history
improve initial image zoom and centering
  • Loading branch information
mejackreed committed Jan 2, 2015
2 parents bfbe91b + 2061168 commit 1b3e9d9
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions leaflet-iiif.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ L.TileLayer.Iiif = L.TileLayer.extend({
// Wait for deferred to complete
$.when(_this._infoDeferred).done(function() {

// Try to center the map a bit
map.setView([-_this.options.tileSize / 2, _this.options.tileSize / 4], 1);
// Find best zoom level and center map
var initialZoom = _this._getInitialZoom(map.getSize()),
imageSize = _this._imageSizes[initialZoom],
sw = map.options.crs.pointToLatLng(L.point(0, imageSize.y), initialZoom),
ne = map.options.crs.pointToLatLng(L.point(imageSize.x, 0), initialZoom),
bounds = L.latLngBounds(sw, ne);

map.fitBounds(bounds, true);

// Set maxZoom for map
map._layersMaxZoom = _this.maxZoom;
Expand Down Expand Up @@ -79,6 +85,7 @@ L.TileLayer.Iiif = L.TileLayer.extend({

var profile,
tierSizes = [],
imageSizes = [],
scale,
width_,
height_,
Expand Down Expand Up @@ -118,9 +125,11 @@ L.TileLayer.Iiif = L.TileLayer.extend({
tilesX_ = Math.ceil(width_ / _this.options.tileSize);
tilesY_ = Math.ceil(height_ / _this.options.tileSize);
tierSizes.push([tilesX_, tilesY_]);
imageSizes.push(L.point(width_,height_));
}

_this._tierSizes = tierSizes;
_this._imageSizes = imageSizes;

// Resolved Deferred to initiate tilelayer load
_this._infoDeferred.resolve();
Expand All @@ -145,6 +154,20 @@ L.TileLayer.Iiif = L.TileLayer.extend({
}else {
return true;
}
},
_getInitialZoom: function (mapSize) {
var _this = this,
tolerance = 0.8,
imageSize;

for (var i = _this.maxZoom; i >= 0; i--) {
imageSize = this._imageSizes[i];
if (imageSize.x * tolerance < mapSize.x && imageSize.y * tolerance < mapSize.y) {
return i;
}
}
// return a default zoom
return 2;
}
});

Expand Down

0 comments on commit 1b3e9d9

Please sign in to comment.