mirror of
https://github.com/openseadragon/openseadragon.git
synced 2024-11-22 05:06:09 +03:00
Fix home bounds with clipping. Fix #891
This commit is contained in:
parent
d631d97545
commit
9e68f6c27b
@ -287,6 +287,26 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag
|
||||
return this.getBounds();
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the bounds of the displayed part of the tiled image.
|
||||
* @param {Boolean} [current=false] Pass true for the current location,
|
||||
* false for the target location.
|
||||
* @returns {$.Rect} The clipped bounds in viewport coordinates.
|
||||
*/
|
||||
getClippedBounds: function(current) {
|
||||
var bounds = this.getBounds(current);
|
||||
if (this._clip) {
|
||||
var ratio = this._worldWidthCurrent / this.source.dimensions.x;
|
||||
var clip = this._clip.times(ratio);
|
||||
bounds = new $.Rect(
|
||||
bounds.x + clip.x,
|
||||
bounds.y + clip.y,
|
||||
clip.width,
|
||||
clip.height);
|
||||
}
|
||||
return bounds;
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns {OpenSeadragon.Point} This TiledImage's content size, in original pixels.
|
||||
*/
|
||||
|
@ -1085,8 +1085,18 @@ $.Viewport.prototype = {
|
||||
return this.viewportToImageCoordinates(viewerX.x, viewerX.y);
|
||||
}
|
||||
|
||||
if (this.viewer && this.viewer.world.getItemCount() > 1) {
|
||||
$.console.error('[Viewport.viewportToImageCoordinates] is not accurate with multi-image; use TiledImage.viewportToImageCoordinates instead.');
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.viewportToImageCoordinates] is not accurate ' +
|
||||
'with multi-image; use TiledImage.viewportToImageCoordinates instead.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.viewportToImageCoordinates
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.viewportToImageCoordinates(viewerX, viewerY, true);
|
||||
}
|
||||
}
|
||||
|
||||
return this._viewportToImageDelta(
|
||||
@ -1119,8 +1129,18 @@ $.Viewport.prototype = {
|
||||
return this.imageToViewportCoordinates(imageX.x, imageX.y);
|
||||
}
|
||||
|
||||
if (this.viewer && this.viewer.world.getItemCount() > 1) {
|
||||
$.console.error('[Viewport.imageToViewportCoordinates] is not accurate with multi-image; use TiledImage.imageToViewportCoordinates instead.');
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.imageToViewportCoordinates] is not accurate ' +
|
||||
'with multi-image; use TiledImage.imageToViewportCoordinates instead.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.viewportToImageCoordinates
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.imageToViewportCoordinates(imageX, imageY, true);
|
||||
}
|
||||
}
|
||||
|
||||
var point = this._imageToViewportDelta(imageX, imageY);
|
||||
@ -1150,6 +1170,21 @@ $.Viewport.prototype = {
|
||||
rect = new $.Rect(imageX, imageY, pixelWidth, pixelHeight);
|
||||
}
|
||||
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.imageToViewportRectangle] is not accurate ' +
|
||||
'with multi-image; use TiledImage.imageToViewportRectangle instead.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.imageToViewportRectangle
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.imageToViewportRectangle(
|
||||
imageX, imageY, pixelWidth, pixelHeight, true);
|
||||
}
|
||||
}
|
||||
|
||||
var coordA = this.imageToViewportCoordinates(rect.x, rect.y);
|
||||
var coordB = this._imageToViewportDelta(rect.width, rect.height);
|
||||
return new $.Rect(
|
||||
@ -1183,6 +1218,21 @@ $.Viewport.prototype = {
|
||||
rect = new $.Rect(viewerX, viewerY, pointWidth, pointHeight);
|
||||
}
|
||||
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.viewportToImageRectangle] is not accurate ' +
|
||||
'with multi-image; use TiledImage.viewportToImageRectangle instead.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.viewportToImageCoordinates
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.viewportToImageRectangle(
|
||||
viewerX, viewerY, pointWidth, pointHeight, true);
|
||||
}
|
||||
}
|
||||
|
||||
var coordA = this.viewportToImageCoordinates(rect.x, rect.y);
|
||||
var coordB = this._viewportToImageDelta(rect.width, rect.height);
|
||||
return new $.Rect(
|
||||
@ -1296,9 +1346,19 @@ $.Viewport.prototype = {
|
||||
* target zoom.
|
||||
* @returns {Number} imageZoom The image zoom
|
||||
*/
|
||||
viewportToImageZoom: function( viewportZoom ) {
|
||||
if (this.viewer && this.viewer.world.getItemCount() > 1) {
|
||||
$.console.error('[Viewport.viewportToImageZoom] is not accurate with multi-image.');
|
||||
viewportToImageZoom: function(viewportZoom) {
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.viewportToImageZoom] is not ' +
|
||||
'accurate with multi-image.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.viewportToImageZoom
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.viewportToImageZoom(viewportZoom);
|
||||
}
|
||||
}
|
||||
|
||||
var imageWidth = this._contentSizeNoRotate.x;
|
||||
@ -1320,9 +1380,19 @@ $.Viewport.prototype = {
|
||||
* target zoom.
|
||||
* @returns {Number} viewportZoom The viewport zoom
|
||||
*/
|
||||
imageToViewportZoom: function( imageZoom ) {
|
||||
if (this.viewer && this.viewer.world.getItemCount() > 1) {
|
||||
$.console.error('[Viewport.imageToViewportZoom] is not accurate with multi-image.');
|
||||
imageToViewportZoom: function(imageZoom) {
|
||||
if (this.viewer) {
|
||||
var count = this.viewer.world.getItemCount();
|
||||
if (count > 1) {
|
||||
$.console.error('[Viewport.imageToViewportZoom] is not accurate ' +
|
||||
'with multi-image.');
|
||||
} else if (count === 1) {
|
||||
// It is better to use TiledImage.imageToViewportZoom
|
||||
// because this._contentBoundsNoRotate can not be relied on
|
||||
// with clipping.
|
||||
var item = this.viewer.world.getItemAt(0);
|
||||
return item.imageToViewportZoom(imageZoom);
|
||||
}
|
||||
}
|
||||
|
||||
var imageWidth = this._contentSizeNoRotate.x;
|
||||
|
44
src/world.js
44
src/world.js
@ -375,34 +375,40 @@ $.extend( $.World.prototype, $.EventSource.prototype, /** @lends OpenSeadragon.W
|
||||
var oldContentSize = this._contentSize ? this._contentSize.clone() : null;
|
||||
var oldContentFactor = this._contentFactor || 0;
|
||||
|
||||
if ( !this._items.length ) {
|
||||
if (!this._items.length) {
|
||||
this._homeBounds = new $.Rect(0, 0, 1, 1);
|
||||
this._contentSize = new $.Point(1, 1);
|
||||
this._contentFactor = 1;
|
||||
} else {
|
||||
var bounds = this._items[0].getBounds();
|
||||
this._contentFactor = this._items[0].getContentSize().x / bounds.width;
|
||||
var left = bounds.x;
|
||||
var top = bounds.y;
|
||||
var right = bounds.x + bounds.width;
|
||||
var bottom = bounds.y + bounds.height;
|
||||
var box;
|
||||
for ( var i = 1; i < this._items.length; i++ ) {
|
||||
box = this._items[i].getBounds();
|
||||
this._contentFactor = Math.max(this._contentFactor, this._items[i].getContentSize().x / box.width);
|
||||
left = Math.min( left, box.x );
|
||||
top = Math.min( top, box.y );
|
||||
right = Math.max( right, box.x + box.width );
|
||||
bottom = Math.max( bottom, box.y + box.height );
|
||||
var item = this._items[0];
|
||||
var bounds = item.getBounds();
|
||||
this._contentFactor = item.getContentSize().x / bounds.width;
|
||||
var clippedBounds = item.getClippedBounds();
|
||||
var left = clippedBounds.x;
|
||||
var top = clippedBounds.y;
|
||||
var right = clippedBounds.x + clippedBounds.width;
|
||||
var bottom = clippedBounds.y + clippedBounds.height;
|
||||
for (var i = 1; i < this._items.length; i++) {
|
||||
item = this._items[i];
|
||||
bounds = item.getBounds();
|
||||
this._contentFactor = Math.max(this._contentFactor,
|
||||
item.getContentSize().x / bounds.width);
|
||||
clippedBounds = item.getClippedBounds();
|
||||
left = Math.min(left, clippedBounds.x);
|
||||
top = Math.min(top, clippedBounds.y);
|
||||
right = Math.max(right, clippedBounds.x + clippedBounds.width);
|
||||
bottom = Math.max(bottom, clippedBounds.y + clippedBounds.height);
|
||||
}
|
||||
|
||||
this._homeBounds = new $.Rect( left, top, right - left, bottom - top );
|
||||
this._contentSize = new $.Point(this._homeBounds.width * this._contentFactor,
|
||||
this._homeBounds = new $.Rect(left, top, right - left, bottom - top);
|
||||
this._contentSize = new $.Point(
|
||||
this._homeBounds.width * this._contentFactor,
|
||||
this._homeBounds.height * this._contentFactor);
|
||||
}
|
||||
|
||||
if (this._contentFactor !== oldContentFactor || !this._homeBounds.equals(oldHomeBounds) ||
|
||||
!this._contentSize.equals(oldContentSize)) {
|
||||
if (this._contentFactor !== oldContentFactor ||
|
||||
!this._homeBounds.equals(oldHomeBounds) ||
|
||||
!this._contentSize.equals(oldContentSize)) {
|
||||
/**
|
||||
* Raised when the home bounds or content factor change.
|
||||
* @event metrics-change
|
||||
|
@ -206,9 +206,8 @@
|
||||
propEqual(image.getClip(), clip, 'clip is set correctly');
|
||||
|
||||
Util.spyOnce(viewer.drawer, 'setClip', function(rect) {
|
||||
ok(true, 'drawer.setClip is called');
|
||||
var pixelRatio = viewer.viewport.getContainerSize().x / image.getContentSize().x;
|
||||
var canvasClip = clip.times(pixelRatio * OpenSeadragon.pixelDensityRatio);
|
||||
var homeBounds = viewer.viewport.getHomeBounds();
|
||||
var canvasClip = viewer.viewport.viewportToViewerElementRectangle(homeBounds);
|
||||
propEqual(rect, canvasClip, 'clipping to correct rect');
|
||||
start();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user