Fix fitBounds with extreme zoom values.

This commit is contained in:
Antoine Vandecreme 2016-05-17 14:27:28 -04:00
parent b11edddf68
commit 8951ac3f5b
2 changed files with 43 additions and 5 deletions

View File

@ -664,15 +664,15 @@ $.Viewport.prototype = {
var oldBounds = this.getBounds(); var oldBounds = this.getBounds();
var oldZoom = this.getZoom(); var oldZoom = this.getZoom();
if (Math.abs(newZoom - oldZoom) < 0.00000001 || if (oldZoom === 0 || Math.abs(newZoom / oldZoom - 1) < 0.00000001) {
Math.abs(newBounds.width - oldBounds.width) < 0.00000001) { this.zoomTo(newZoom, true);
return this.panTo(center, immediately); return this.panTo(center, immediately);
} }
newBounds = newBounds.rotate(-this.getRotation()); newBounds = newBounds.rotate(-this.getRotation());
var referencePoint = newBounds.getTopLeft().divide(newBounds.width) var referencePoint = newBounds.getTopLeft().times(newZoom)
.minus(oldBounds.getTopLeft().divide(oldBounds.width)) .minus(oldBounds.getTopLeft().times(oldZoom))
.divide(1 / newBounds.width - 1 / oldBounds.width); .divide(newZoom - oldZoom);
return this.zoomTo(newZoom, referencePoint, immediately); return this.zoomTo(newZoom, referencePoint, immediately);
}, },

View File

@ -587,6 +587,44 @@
viewer.open(DZI_PATH); viewer.open(DZI_PATH);
}); });
asyncTest('fitBounds with almost same zoom', function() {
var openHandler = function() {
var viewport = viewer.viewport;
var rect1 = new OpenSeadragon.Rect(0, 0, 1, 1);
viewport.fitBounds(rect1, true);
Util.assertRectangleEquals(rect1, viewport.getBounds(), 1e-6,
'Bounds should be ' + rect1);
// Zoom and pan
var rect2 = new OpenSeadragon.Rect(1, 1, 1 + 1e-8, 1 + 1e-8);
viewport.fitBounds(rect2);
Util.assertRectangleEquals(rect2, viewport.getBounds(), 1e-6,
'Bounds should be ' + rect2);
start();
};
viewer.addOnceHandler('open', openHandler);
viewer.open(DZI_PATH);
});
asyncTest('fitBounds with big rectangle', function() {
var openHandler = function() {
var viewport = viewer.viewport;
var rect1 = new OpenSeadragon.Rect(0, 0, 1e9, 1e9);
viewport.fitBounds(rect1, true);
Util.assertRectangleEquals(rect1, viewport.getBounds(), 1e-6,
'Bounds should be ' + rect1);
// Zoom and pan
var rect2 = new OpenSeadragon.Rect(1, 1, 2e9, 2e9);
viewport.fitBounds(rect2);
Util.assertRectangleEquals(rect2, viewport.getBounds(), 1e-6,
'Bounds should be ' + rect2);
start();
};
viewer.addOnceHandler('open', openHandler);
viewer.open(DZI_PATH);
});
asyncTest('fitHorizontally', function(){ asyncTest('fitHorizontally', function(){
var openHandler = function(event) { var openHandler = function(event) {
viewer.removeHandler('open', openHandler); viewer.removeHandler('open', openHandler);