Merge pull request #965 from MrP/master

Fix issue #890 in applyConstraints
This commit is contained in:
Ian Gilman 2016-06-15 09:40:09 -07:00 committed by GitHub
commit 6f4235c7f3
3 changed files with 45 additions and 35 deletions

View File

@ -495,52 +495,41 @@ $.Viewport.prototype = {
bounds.width, bounds.width,
bounds.height); bounds.height);
var horizontalThreshold = this.visibilityRatio * newBounds.width;
var verticalThreshold = this.visibilityRatio * newBounds.height;
if (this.wrapHorizontal) { if (this.wrapHorizontal) {
//do nothing //do nothing
} else { } else {
var dx = 0; var horizontalThreshold = this.visibilityRatio * newBounds.width;
var thresholdLeft = newBounds.x + (newBounds.width - horizontalThreshold); var boundsRight = newBounds.x + newBounds.width;
if (this._contentBoundsNoRotate.x > thresholdLeft) {
dx = this._contentBoundsNoRotate.x - thresholdLeft;
}
var contentRight = this._contentBoundsNoRotate.x + this._contentBoundsNoRotate.width; var contentRight = this._contentBoundsNoRotate.x + this._contentBoundsNoRotate.width;
var thresholdRight = newBounds.x + horizontalThreshold; var leftDx = this._contentBoundsNoRotate.x - boundsRight + horizontalThreshold;
if (contentRight < thresholdRight) { var rightDx = contentRight - newBounds.x - horizontalThreshold;
var newDx = contentRight - thresholdRight;
if (dx) { if (horizontalThreshold > this._contentBoundsNoRotate.width) {
dx = (dx + newDx) / 2; newBounds.x += (leftDx + rightDx) / 2;
} else { } else if (rightDx < 0) {
dx = newDx; newBounds.x += rightDx;
} else if (leftDx > 0) {
newBounds.x += leftDx;
} }
} }
newBounds.x += dx;
}
if (this.wrapVertical) { if (this.wrapVertical) {
//do nothing //do nothing
} else { } else {
var dy = 0; var verticalThreshold = this.visibilityRatio * newBounds.height;
var thresholdTop = newBounds.y + (newBounds.height - verticalThreshold); var boundsBottom = newBounds.y + newBounds.height;
if (this._contentBoundsNoRotate.y > thresholdTop) {
dy = this._contentBoundsNoRotate.y - thresholdTop;
}
var contentBottom = this._contentBoundsNoRotate.y + this._contentBoundsNoRotate.height; var contentBottom = this._contentBoundsNoRotate.y + this._contentBoundsNoRotate.height;
var thresholdBottom = newBounds.y + verticalThreshold; var topDy = this._contentBoundsNoRotate.y - boundsBottom + verticalThreshold;
if (contentBottom < thresholdBottom) { var bottomDy = contentBottom - newBounds.y - verticalThreshold;
var newDy = contentBottom - thresholdBottom;
if (dy) { if (verticalThreshold > this._contentBoundsNoRotate.height) {
dy = (dy + newDy) / 2; newBounds.y += (topDy + bottomDy) / 2;
} else { } else if (bottomDy < 0) {
dy = newDy; newBounds.y += bottomDy;
} else if (topDy > 0) {
newBounds.y += topDy;
} }
} }
newBounds.y += dy;
}
if (this.viewer) { if (this.viewer) {
/** /**

View File

@ -820,8 +820,8 @@
Util.assessNumericValue(zoom, 0.002, epsilon, Util.assessNumericValue(zoom, 0.002, epsilon,
"Zoom should not be prevented"); "Zoom should not be prevented");
Util.assertRectangleEquals( Util.assertRectangleEquals(
new OpenSeadragon.Rect(-249.5, -0.25, 500, 0.5),
bounds, bounds,
new OpenSeadragon.Rect(-250, -0.25, 500, 0.5),
epsilon, epsilon,
'Pan should not be prevented'); 'Pan should not be prevented');

View File

@ -437,8 +437,8 @@
viewport.applyConstraints(true); viewport.applyConstraints(true);
var bounds = viewport.getBounds(); var bounds = viewport.getBounds();
Util.assertRectangleEquals( Util.assertRectangleEquals(
bounds,
new OpenSeadragon.Rect(0.7, 0.7, 1, 1), new OpenSeadragon.Rect(0.7, 0.7, 1, 1),
bounds,
EPSILON, EPSILON,
"Viewport.applyConstraints should move viewport."); "Viewport.applyConstraints should move viewport.");
start(); start();
@ -447,6 +447,27 @@
viewer.open(DZI_PATH); 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() { asyncTest('applyConstraints with rotation', function() {
var openHandler = function() { var openHandler = function() {
viewer.removeHandler('open', openHandler); viewer.removeHandler('open', openHandler);