diff --git a/src/viewport.js b/src/viewport.js index da2ce761..2ac499d4 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -495,51 +495,40 @@ $.Viewport.prototype = { bounds.width, bounds.height); - var horizontalThreshold = this.visibilityRatio * newBounds.width; - var verticalThreshold = this.visibilityRatio * newBounds.height; - if (this.wrapHorizontal) { //do nothing } else { - var dx = 0; - var thresholdLeft = newBounds.x + (newBounds.width - horizontalThreshold); - if (this._contentBoundsNoRotate.x > thresholdLeft) { - dx = this._contentBoundsNoRotate.x - thresholdLeft; - } - + var horizontalThreshold = this.visibilityRatio * newBounds.width; + var boundsRight = newBounds.x + newBounds.width; var contentRight = this._contentBoundsNoRotate.x + this._contentBoundsNoRotate.width; - var thresholdRight = newBounds.x + horizontalThreshold; - if (contentRight < thresholdRight) { - var newDx = contentRight - thresholdRight; - if (dx) { - dx = (dx + newDx) / 2; - } else { - dx = newDx; - } + var leftDx = this._contentBoundsNoRotate.x - boundsRight + horizontalThreshold; + var rightDx = contentRight - newBounds.x - horizontalThreshold; + + if (horizontalThreshold > this._contentBoundsNoRotate.width) { + newBounds.x += (leftDx + rightDx) / 2; + } else if (rightDx < 0) { + newBounds.x += rightDx; + } else if (leftDx > 0) { + newBounds.x += leftDx; } - newBounds.x += dx; } if (this.wrapVertical) { //do nothing } else { - var dy = 0; - var thresholdTop = newBounds.y + (newBounds.height - verticalThreshold); - if (this._contentBoundsNoRotate.y > thresholdTop) { - dy = this._contentBoundsNoRotate.y - thresholdTop; - } - + var verticalThreshold = this.visibilityRatio * newBounds.height; + var boundsBottom = newBounds.y + newBounds.height; var contentBottom = this._contentBoundsNoRotate.y + this._contentBoundsNoRotate.height; - var thresholdBottom = newBounds.y + verticalThreshold; - if (contentBottom < thresholdBottom) { - var newDy = contentBottom - thresholdBottom; - if (dy) { - dy = (dy + newDy) / 2; - } else { - dy = newDy; - } + var topDy = this._contentBoundsNoRotate.y - boundsBottom + verticalThreshold; + var bottomDy = contentBottom - newBounds.y - verticalThreshold; + + if (verticalThreshold > this._contentBoundsNoRotate.height) { + newBounds.y += (topDy + bottomDy) / 2; + } else if (bottomDy < 0) { + newBounds.y += bottomDy; + } else if (topDy > 0) { + newBounds.y += topDy; } - newBounds.y += dy; } if (this.viewer) { diff --git a/test/modules/events.js b/test/modules/events.js index 30dc28a2..ccfbab26 100644 --- a/test/modules/events.js +++ b/test/modules/events.js @@ -820,8 +820,8 @@ Util.assessNumericValue(zoom, 0.002, epsilon, "Zoom should not be prevented"); Util.assertRectangleEquals( + new OpenSeadragon.Rect(-249.5, -0.25, 500, 0.5), bounds, - new OpenSeadragon.Rect(-250, -0.25, 500, 0.5), epsilon, 'Pan should not be prevented'); diff --git a/test/modules/viewport.js b/test/modules/viewport.js index f39473cf..fb94d11d 100644 --- a/test/modules/viewport.js +++ b/test/modules/viewport.js @@ -437,8 +437,8 @@ viewport.applyConstraints(true); var bounds = viewport.getBounds(); Util.assertRectangleEquals( - bounds, new OpenSeadragon.Rect(0.7, 0.7, 1, 1), + bounds, EPSILON, "Viewport.applyConstraints should move viewport."); start(); @@ -447,6 +447,27 @@ viewer.open(DZI_PATH); }); + asyncTest('applyConstraints with visibilityRatio = 1 shouldn\'t bounce around', function() { + var openHandler = function() { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + + viewport.visibilityRatio = 1; + viewport.zoomTo(0.5, undefined, true); + viewport.panBy(new OpenSeadragon.Point(0.75, 0), true); + viewport.applyConstraints(true); + var bounds = viewport.getBounds(); + Util.assertRectangleEquals( + new OpenSeadragon.Rect(-0.5, 1, 2, 2), + bounds, + EPSILON, + "Viewport.applyConstraints should move viewport to the center, not to a side."); + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(TALL_PATH); + }); + asyncTest('applyConstraints with rotation', function() { var openHandler = function() { viewer.removeHandler('open', openHandler);