From 11ef3fcfbb29052a9f67ddd5493c9252c7924091 Mon Sep 17 00:00:00 2001 From: superbland Date: Sat, 9 Oct 2021 15:45:19 +0100 Subject: [PATCH 1/5] Add utility method for getting relative size --- src/tiledimage.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tiledimage.js b/src/tiledimage.js index 82e35ed7..d6e8025b 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -419,6 +419,13 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag return new $.Point(this.source.dimensions.x, this.source.dimensions.y); }, + /** + * @returns {OpenSeadragon.Point} The dimensions of the image as it would be currently rendered in the viewport + */ + getRelativeSize: function() { + return this.getContentSize().times(this.viewport.getZoom()); + }, + // private _viewportToImageDelta: function( viewerX, viewerY, current ) { var scale = (current ? this._scaleSpring.current.value : this._scaleSpring.target.value); From 45bdd87224354865494cf9aab4cdd428213227eb Mon Sep 17 00:00:00 2001 From: superbland Date: Sat, 9 Oct 2021 15:45:51 +0100 Subject: [PATCH 2/5] Tests for getRelativeSize --- test/modules/tiledimage.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/modules/tiledimage.js b/test/modules/tiledimage.js index 46860648..49f40d45 100644 --- a/test/modules/tiledimage.js +++ b/test/modules/tiledimage.js @@ -41,8 +41,11 @@ viewer.addHandler('open', function(event) { var image = viewer.world.getItemAt(0); var contentSize = image.getContentSize(); + var relativeSize = image.getRelativeSize(); assert.equal(contentSize.x, 500, 'contentSize.x'); assert.equal(contentSize.y, 2000, 'contentSize.y'); + assert.equal(relativeSize.x, 12.5, 'relativeSize.x'); + assert.equal(relativeSize.y, 50, 'relativeSize.y'); checkBounds(assert, image, new OpenSeadragon.Rect(5, 6, 10, 40), 'initial bounds'); @@ -74,7 +77,18 @@ image.setHeight(4); checkBounds(assert, image, new OpenSeadragon.Rect(7, 8, 1, 4), 'bounds after width'); - assert.equal(handlerCount, 1, 'correct number of handlers called'); + viewer.addHandler('zoom', function zoomHandler(event) { + var relativeSize = image.getRelativeSize(); + viewer.removeHandler('zoom', zoomHandler); + handlerCount++; + assert.equal(relativeSize.x, 4000, 'relativeSize.x after zoom'); + assert.equal(relativeSize.y, 16000, 'relativeSize.y after zoom'); + }); + + viewer.viewport.zoomTo(8); + + assert.equal(handlerCount, 2, 'correct number of handlers called'); + done(); }); From 92bcfa5416b1603bedd6b8a9ad2cc7ae0514c858 Mon Sep 17 00:00:00 2001 From: superbland Date: Sat, 30 Oct 2021 15:01:17 +0100 Subject: [PATCH 3/5] Rename method, update logic --- src/tiledimage.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index d6e8025b..8145a4d3 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -420,10 +420,12 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag }, /** - * @returns {OpenSeadragon.Point} The dimensions of the image as it would be currently rendered in the viewport + * @returns {OpenSeadragon.Point} The TiledImage's content size, in window coordinates. */ - getRelativeSize: function() { - return this.getContentSize().times(this.viewport.getZoom()); + getSizeInWindowCoordinates: function() { + var topLeft = this.viewport.imageToWindowCoordinates(new $.Point(0, 0)); + var bottomRight = this.viewport.imageToWindowCoordinates(this.getContentSize()); + return new $.Point(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y); }, // private From 57e97019d9479dc03e0e401b83d09b5ea285c436 Mon Sep 17 00:00:00 2001 From: superbland Date: Sat, 30 Oct 2021 15:01:25 +0100 Subject: [PATCH 4/5] Update tests --- test/modules/tiledimage.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/modules/tiledimage.js b/test/modules/tiledimage.js index 49f40d45..4a075328 100644 --- a/test/modules/tiledimage.js +++ b/test/modules/tiledimage.js @@ -41,11 +41,11 @@ viewer.addHandler('open', function(event) { var image = viewer.world.getItemAt(0); var contentSize = image.getContentSize(); - var relativeSize = image.getRelativeSize(); + var sizeInWindowCoords = image.getSizeInWindowCoordinates(); assert.equal(contentSize.x, 500, 'contentSize.x'); assert.equal(contentSize.y, 2000, 'contentSize.y'); - assert.equal(relativeSize.x, 12.5, 'relativeSize.x'); - assert.equal(relativeSize.y, 50, 'relativeSize.y'); + assert.equal(sizeInWindowCoords.x, 125, 'sizeInWindowCoords.x'); + assert.equal(sizeInWindowCoords.y, 500, 'sizeInWindowCoords.y'); checkBounds(assert, image, new OpenSeadragon.Rect(5, 6, 10, 40), 'initial bounds'); @@ -78,14 +78,14 @@ checkBounds(assert, image, new OpenSeadragon.Rect(7, 8, 1, 4), 'bounds after width'); viewer.addHandler('zoom', function zoomHandler(event) { - var relativeSize = image.getRelativeSize(); + var sizeInWindowCoords = image.getSizeInWindowCoordinates(); viewer.removeHandler('zoom', zoomHandler); handlerCount++; - assert.equal(relativeSize.x, 4000, 'relativeSize.x after zoom'); - assert.equal(relativeSize.y, 16000, 'relativeSize.y after zoom'); + assert.equal(sizeInWindowCoords.x, 4000, 'sizeInWindowCoords.x after zoom'); + assert.equal(sizeInWindowCoords.y, 16000, 'sizeInWindowCoords.y after zoom'); }); - viewer.viewport.zoomTo(8); + viewer.viewport.zoomTo(8, null, true); assert.equal(handlerCount, 2, 'correct number of handlers called'); From 446382f5a17202d9a8a01b03c8edd6b6ef2ca189 Mon Sep 17 00:00:00 2001 From: superbland Date: Thu, 4 Nov 2021 19:43:59 +0000 Subject: [PATCH 5/5] Use correct method for imageToWindowCoordinates --- src/tiledimage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index 8145a4d3..95ac4f3c 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -423,8 +423,8 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag * @returns {OpenSeadragon.Point} The TiledImage's content size, in window coordinates. */ getSizeInWindowCoordinates: function() { - var topLeft = this.viewport.imageToWindowCoordinates(new $.Point(0, 0)); - var bottomRight = this.viewport.imageToWindowCoordinates(this.getContentSize()); + var topLeft = this.imageToWindowCoordinates(new $.Point(0, 0)); + var bottomRight = this.imageToWindowCoordinates(this.getContentSize()); return new $.Point(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y); },