From 6b208f8a75de7a60a3d19ee76b7615c05587f046 Mon Sep 17 00:00:00 2001 From: Ian Gilman Date: Thu, 29 Oct 2015 14:29:59 -0700 Subject: [PATCH] * You can now change viewport margins after the viewer is created (#721) --- changelog.txt | 1 + src/viewport.js | 46 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/changelog.txt b/changelog.txt index 430571c7..a619bcdb 100644 --- a/changelog.txt +++ b/changelog.txt @@ -33,6 +33,7 @@ OPENSEADRAGON CHANGELOG * Fixed overlays position (use rounding instead of flooring and ceiling) (#741) * Fixed issue with including overlays in your tileSources array when creating/opening in the viewer (#745) * Fixed issue in iOS devices that would cause all touch events to fail after a Multitasking Gesture was triggered (#744) +* You can now change viewport margins after the viewer is created (#721) 2.0.0: diff --git a/src/viewport.js b/src/viewport.js index b882580e..c619198d 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -111,10 +111,7 @@ $.Viewport = function( options ) { }, options ); - this._containerInnerSize = new $.Point( - Math.max(1, this.containerSize.x - (this._margins.left + this._margins.right)), - Math.max(1, this.containerSize.y - (this._margins.top + this._margins.bottom)) - ); + this._updateContainerInnerSize(); this.centerSpringX = new $.Spring({ initial: 0, @@ -314,6 +311,34 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ ); }, + /** + * @function + * The margins push the "home" region in from the sides by the specified amounts. + * @returns {Object} Properties (Numbers, in screen coordinates): left, top, right, bottom. + */ + getMargins: function() { + return $.extend({}, this._margins); // Make a copy so we are not returning our original + }, + + /** + * @function + * The margins push the "home" region in from the sides by the specified amounts. + * @param {Object} margins - Properties (Numbers, in screen coordinates): left, top, right, bottom. + */ + setMargins: function(margins) { + $.console.assert($.type(margins) === 'object', '[Viewport.setMargins] margins must be an object'); + + this._margins = $.extend({ + left: 0, + top: 0, + right: 0, + bottom: 0 + }, margins); + + this._updateContainerInnerSize(); + this.viewer.forceRedraw(); + }, + /** * @function * @param {Boolean} current - Pass true for the current location; defaults to false (target location). @@ -830,10 +855,7 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ this.containerSize.x = newContainerSize.x; this.containerSize.y = newContainerSize.y; - this._containerInnerSize = new $.Point( - Math.max(1, newContainerSize.x - (this._margins.left + this._margins.right)), - Math.max(1, newContainerSize.y - (this._margins.top + this._margins.bottom)) - ); + this._updateContainerInnerSize(); if ( maintain ) { // TODO: widthDeltaFactor will always be 1; probably not what's intended @@ -863,6 +885,14 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ return this.fitBounds( newBounds, true ); }, + // private + _updateContainerInnerSize: function() { + this._containerInnerSize = new $.Point( + Math.max(1, this.containerSize.x - (this._margins.left + this._margins.right)), + Math.max(1, this.containerSize.y - (this._margins.top + this._margins.bottom)) + ); + }, + /** * @function */