From b0926b3e69c04d069e6a8c1a9c2966d7eaca5db6 Mon Sep 17 00:00:00 2001 From: eug-L Date: Thu, 4 Apr 2024 16:02:09 +0800 Subject: [PATCH 1/2] Getter & setter for Viewport.maxZoomPixelRatio --- src/viewport.js | 38 +++++++++++++++++++++++++++++++++++++- test/modules/viewport.js | 21 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/viewport.js b/src/viewport.js index 0f160bd6..60702c33 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -1789,7 +1789,43 @@ $.Viewport.prototype = { */ this.viewer.raiseEvent('flip', {flipped: state}); return this; - } + }, + + /** + * Gets current max zoom pixel ratio + * @function + * @returns {Number} Max zoom pixel ratio + */ + getMaxZoomPixelRatio: function() { + return this.maxZoomPixelRatio; + }, + + /** + * Sets max zoom pixel ratio + * @function + * @param {Number} ratio - Max zoom pixel ratio + * @param {Boolean} [constraints=false] - Apply constraints after setting ratio; + * Takes effect only if current zoom is greater than set max zoom pixel ratio + * @param {Boolean} [immediately=false] - Whether to animate to new zoom + */ + setMaxZoomPixelRatio: function(ratio, constraints, immediately) { + constraints = constraints || false; + immediately = immediately || false; + + $.console.assert(!isNaN(ratio), "[Viewport.setMaxZoomPixelRatio] ratio must be a number"); + + if (isNaN(ratio)) { + return; + } + + this.maxZoomPixelRatio = ratio; + + if (constraints) { + if (this.getZoom() > this.getMaxZoom()) { + this.applyConstraints(immediately); + } + } + }, }; diff --git a/test/modules/viewport.js b/test/modules/viewport.js index 3ac93734..c3620c3c 100644 --- a/test/modules/viewport.js +++ b/test/modules/viewport.js @@ -1407,4 +1407,25 @@ viewer.open(DZI_PATH); }); + QUnit.test('setMaxZoomPixelRatio', function(assert) { + var done = assert.async(); + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + + for (var i = 0; i < testZoomLevels.length; i++) { + viewport.setMaxZoomPixelRatio(testZoomLevels[i]) + assert.equal(viewport.getMaxZoomPixelRatio(), testZoomLevels[i], "Max zoom pixel ratio is set correctly."); + } + + viewport.zoomTo(viewport.getMaxZoom()) + viewport.setMaxZoomPixelRatio(testZoomLevels[0], true) + assert.equal(viewport.getZoom(), viewport.getMaxZoom(), "Zoom should be adjusted to max zoom level."); + + done(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + })(); From 44511319998f93fa5df3d79180063e048b8c2310 Mon Sep 17 00:00:00 2001 From: eug-L Date: Fri, 5 Apr 2024 12:47:15 +0800 Subject: [PATCH 2/2] update flags for setMaxZoomPixelRatio --- src/viewport.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/viewport.js b/src/viewport.js index 60702c33..4d209dc5 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -1804,13 +1804,11 @@ $.Viewport.prototype = { * Sets max zoom pixel ratio * @function * @param {Number} ratio - Max zoom pixel ratio - * @param {Boolean} [constraints=false] - Apply constraints after setting ratio; + * @param {Boolean} [applyConstraints=true] - Apply constraints after setting ratio; * Takes effect only if current zoom is greater than set max zoom pixel ratio * @param {Boolean} [immediately=false] - Whether to animate to new zoom */ - setMaxZoomPixelRatio: function(ratio, constraints, immediately) { - constraints = constraints || false; - immediately = immediately || false; + setMaxZoomPixelRatio: function(ratio, applyConstraints = true, immediately = false) { $.console.assert(!isNaN(ratio), "[Viewport.setMaxZoomPixelRatio] ratio must be a number"); @@ -1820,7 +1818,7 @@ $.Viewport.prototype = { this.maxZoomPixelRatio = ratio; - if (constraints) { + if (applyConstraints) { if (this.getZoom() > this.getMaxZoom()) { this.applyConstraints(immediately); }