Add viewport.viewportToViewerElementRectangle

This commit is contained in:
Antoine Vandecreme 2016-03-28 17:07:47 -04:00
parent cac5f6dec3
commit f6c09ca716
2 changed files with 42 additions and 1 deletions

View File

@ -110,6 +110,33 @@ $.Rect = function(x, y, width, height, degrees) {
} }
}; };
/**
* Builds a rectangle having the 3 specified points as summits.
* @static
* @memberof OpenSeadragon.Rect
* @param {OpenSeadragon.Point} topLeft
* @param {OpenSeadragon.Point} topRight
* @param {OpenSeadragon.Point} bottomLeft
* @returns {OpenSeadragon.Rect}
*/
$.Rect.fromSummits = function(topLeft, topRight, bottomLeft) {
var width = topLeft.distanceTo(topRight);
var height = topLeft.distanceTo(bottomLeft);
var diff = topRight.minus(topLeft);
var radians = Math.atan(diff.y / diff.x);
if (diff.x < 0) {
radians += Math.PI;
} else if (diff.y < 0) {
radians += 2 * Math.PI;
}
return new $.Rect(
topLeft.x,
topLeft.y,
width,
height,
radians / Math.PI * 180);
};
/** @lends OpenSeadragon.Rect.prototype */ /** @lends OpenSeadragon.Rect.prototype */
$.Rect.prototype = { $.Rect.prototype = {
/** /**
@ -284,7 +311,7 @@ $.Rect.prototype = {
* Rotates a rectangle around a point. * Rotates a rectangle around a point.
* @function * @function
* @param {Number} degrees The angle in degrees to rotate. * @param {Number} degrees The angle in degrees to rotate.
* @param {OpenSeadragon.Point} pivot The point about which to rotate. * @param {OpenSeadragon.Point} [pivot] The point about which to rotate.
* Defaults to the center of the rectangle. * Defaults to the center of the rectangle.
* @return {OpenSeadragon.Rect} * @return {OpenSeadragon.Rect}
*/ */

View File

@ -1262,6 +1262,20 @@ $.Viewport.prototype = {
return this.pixelFromPoint( point, true ); return this.pixelFromPoint( point, true );
}, },
/**
* Convert a rectangle in viewport coordinates to pixel coordinates relative
* to the viewer element.
* @param {OpenSeadragon.Rect} rectangle the rectangle to convert
* @returns {OpenSeadragon.Rect} the converted rectangle
*/
viewportToViewerElementRectangle: function(rectangle) {
return $.Rect.fromSummits(
this.pixelFromPoint(rectangle.getTopLeft(), true),
this.pixelFromPoint(rectangle.getTopRight(), true),
this.pixelFromPoint(rectangle.getBottomLeft(), true)
);
},
/** /**
* Convert pixel coordinates relative to the window to viewport coordinates. * Convert pixel coordinates relative to the window to viewport coordinates.
* @param {OpenSeadragon.Point} pixel * @param {OpenSeadragon.Point} pixel