From a6d147213467f33635987ac2bf52a73b4c065477 Mon Sep 17 00:00:00 2001 From: Joe DF <3848219+joedf@users.noreply.github.com> Date: Thu, 12 May 2022 15:31:51 -0400 Subject: [PATCH 1/4] apply only zoom constraint by default for scroll events --- src/viewer.js | 2 +- src/viewport.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/viewer.js b/src/viewer.js index ab779142..a1f965f8 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -3371,7 +3371,7 @@ function onCanvasScroll( event ) { factor, gestureSettings.zoomToRefPoint ? this.viewport.pointFromPixel( event.position, true ) : null ); - this.viewport.applyConstraints(); + this.viewport.applyZoomConstraints(); } } diff --git a/src/viewport.js b/src/viewport.js index be350af9..a8a53fa2 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -591,6 +591,25 @@ $.Viewport.prototype = { return this; }, + /** + * Enforces the minZoom, maxZoom constraints by zooming to the closest acceptable zoom. + * @function + * @param {Boolean} [immediately=false] + * @return {OpenSeadragon.Viewport} Chainable. + * @fires OpenSeadragon.Viewer.event:constrain + */ + applyZoomConstraints: function(immediately) { + var actualZoom = this.getZoom(); + var constrainedZoom = this._applyZoomConstraints(actualZoom); + + if (actualZoom !== constrainedZoom) { + this.zoomTo(constrainedZoom, this.zoomPoint, immediately); + } + + this._raiseConstraintsEvent(immediately); + return this; + }, + /** * Equivalent to {@link OpenSeadragon.Viewport#applyConstraints} * @function From 7d6437b2cdda2a901a4a1180b779924c446ee296 Mon Sep 17 00:00:00 2001 From: Joe DF <3848219+joedf@users.noreply.github.com> Date: Wed, 18 May 2022 10:48:58 -0400 Subject: [PATCH 2/4] Revert "apply only zoom constraint by default for scroll events" This reverts commit a6d147213467f33635987ac2bf52a73b4c065477. --- src/viewer.js | 2 +- src/viewport.js | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/viewer.js b/src/viewer.js index a1f965f8..ab779142 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -3371,7 +3371,7 @@ function onCanvasScroll( event ) { factor, gestureSettings.zoomToRefPoint ? this.viewport.pointFromPixel( event.position, true ) : null ); - this.viewport.applyZoomConstraints(); + this.viewport.applyConstraints(); } } diff --git a/src/viewport.js b/src/viewport.js index a8a53fa2..be350af9 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -591,25 +591,6 @@ $.Viewport.prototype = { return this; }, - /** - * Enforces the minZoom, maxZoom constraints by zooming to the closest acceptable zoom. - * @function - * @param {Boolean} [immediately=false] - * @return {OpenSeadragon.Viewport} Chainable. - * @fires OpenSeadragon.Viewer.event:constrain - */ - applyZoomConstraints: function(immediately) { - var actualZoom = this.getZoom(); - var constrainedZoom = this._applyZoomConstraints(actualZoom); - - if (actualZoom !== constrainedZoom) { - this.zoomTo(constrainedZoom, this.zoomPoint, immediately); - } - - this._raiseConstraintsEvent(immediately); - return this; - }, - /** * Equivalent to {@link OpenSeadragon.Viewport#applyConstraints} * @function From 325f65fe5c70a7b63a62dd1d2324c368b79912ac Mon Sep 17 00:00:00 2001 From: Joe DF <3848219+joedf@users.noreply.github.com> Date: Wed, 18 May 2022 11:16:28 -0400 Subject: [PATCH 3/4] attempt no.2 to fix #2160 --- src/viewport.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viewport.js b/src/viewport.js index be350af9..451b602c 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -506,7 +506,7 @@ $.Viewport.prototype = { var leftDx = this._contentBoundsNoRotate.x - boundsRight + horizontalThreshold; var rightDx = contentRight - newBounds.x - horizontalThreshold; - if (horizontalThreshold > this._contentBoundsNoRotate.width) { + if (horizontalThreshold > this._contentBoundsNoRotate.width && horizontalThreshold <= 1) { newBounds.x += (leftDx + rightDx) / 2; } else if (rightDx < 0) { newBounds.x += rightDx; @@ -524,7 +524,7 @@ $.Viewport.prototype = { var topDy = this._contentBoundsNoRotate.y - boundsBottom + verticalThreshold; var bottomDy = contentBottom - newBounds.y - verticalThreshold; - if (verticalThreshold > this._contentBoundsNoRotate.height) { + if (verticalThreshold > this._contentBoundsNoRotate.height && verticalThreshold <= 1) { newBounds.y += (topDy + bottomDy) / 2; } else if (bottomDy < 0) { newBounds.y += bottomDy; From 5b7be6be052452b99abf9918f6d908e0c30b1c91 Mon Sep 17 00:00:00 2001 From: Joe DF <3848219+joedf@users.noreply.github.com> Date: Thu, 19 May 2022 16:52:56 -0400 Subject: [PATCH 4/4] implement fixes from iangilman --- src/viewport.js | 62 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/viewport.js b/src/viewport.js index 451b602c..5a6f5bd5 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -500,36 +500,62 @@ $.Viewport.prototype = { if (this.wrapHorizontal) { //do nothing } else { - var horizontalThreshold = this.visibilityRatio * newBounds.width; var boundsRight = newBounds.x + newBounds.width; var contentRight = this._contentBoundsNoRotate.x + this._contentBoundsNoRotate.width; - var leftDx = this._contentBoundsNoRotate.x - boundsRight + horizontalThreshold; - var rightDx = contentRight - newBounds.x - horizontalThreshold; - if (horizontalThreshold > this._contentBoundsNoRotate.width && horizontalThreshold <= 1) { - newBounds.x += (leftDx + rightDx) / 2; - } else if (rightDx < 0) { - newBounds.x += rightDx; - } else if (leftDx > 0) { - newBounds.x += leftDx; + var horizontalThreshold, leftDx, rightDx; + if (newBounds.width > this._contentBoundsNoRotate.width) { + horizontalThreshold = this.visibilityRatio * this._contentBoundsNoRotate.width; + leftDx = this._contentBoundsNoRotate.x - newBounds.x + horizontalThreshold; + rightDx = contentRight - boundsRight - horizontalThreshold; + + if (rightDx > 0) { + newBounds.x += rightDx; + } else if (leftDx < 0) { + newBounds.x += leftDx; + } + } else { + horizontalThreshold = this.visibilityRatio * newBounds.width; + leftDx = this._contentBoundsNoRotate.x - boundsRight + horizontalThreshold; + 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; + } } } if (this.wrapVertical) { //do nothing } else { - var verticalThreshold = this.visibilityRatio * newBounds.height; var boundsBottom = newBounds.y + newBounds.height; var contentBottom = this._contentBoundsNoRotate.y + this._contentBoundsNoRotate.height; - var topDy = this._contentBoundsNoRotate.y - boundsBottom + verticalThreshold; - var bottomDy = contentBottom - newBounds.y - verticalThreshold; - if (verticalThreshold > this._contentBoundsNoRotate.height && verticalThreshold <= 1) { - newBounds.y += (topDy + bottomDy) / 2; - } else if (bottomDy < 0) { - newBounds.y += bottomDy; - } else if (topDy > 0) { - newBounds.y += topDy; + var verticalThreshold, topDy, bottomDy; + if (newBounds.height > this._contentBoundsNoRotate.height) { + verticalThreshold = this.visibilityRatio * this._contentBoundsNoRotate.height; + topDy = this._contentBoundsNoRotate.y - newBounds.y + verticalThreshold; + bottomDy = contentBottom - boundsBottom - verticalThreshold; + + if (bottomDy > 0) { + newBounds.y += bottomDy; + } else if (topDy < 0) { + newBounds.y += topDy; + } + } else { + verticalThreshold = this.visibilityRatio * newBounds.height; + topDy = this._contentBoundsNoRotate.y - boundsBottom + verticalThreshold; + 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; + } } }