Merge pull request #212 from avandecreme/master

Add viewportToImageRectangle method
This commit is contained in:
iangilman 2013-09-05 16:22:13 -07:00
commit a221477eb3

View File

@ -722,34 +722,65 @@ $.Viewport.prototype = {
}, },
/** /**
* Translates from Seajax viewer coordinate * Translates from Seajax viewer coordinate system to image coordinate system.
* system to image coordinate system * This method can be called either by passing X,Y coordinates or an
* OpenSeadragon.Point
* @function
* @param {OpenSeadragon.Point} viewerX the point in viewport coordinate system.
* @param {Number} viewerX X coordinate in viewport coordinate system.
* @param {Number} viewerY Y coordinate in viewport coordinate system.
* @return {OpenSeadragon.Point} a point representing the coordinates in the image.
*/ */
viewportToImageCoordinates: function(viewerX, viewerY) { viewportToImageCoordinates: function( viewerX, viewerY ) {
return new $.Point(viewerX * this.contentSize.x, viewerY * this.contentSize.y * this.contentAspectX); if ( arguments.length == 1 ) {
//they passed a point instead of individual components
return this.viewportToImageCoordinates( viewerX.x, viewerX.y );
}
return new $.Point( viewerX * this.contentSize.x, viewerY * this.contentSize.y * this.contentAspectX );
}, },
/** /**
* Translates from image coordinate system to * Translates from image coordinate system to Seajax viewer coordinate system
* Seajax viewer coordinate system * This method can be called either by passing X,Y coordinates or an
* OpenSeadragon.Point
* @function
* @param {OpenSeadragon.Point} imageX the point in image coordinate system.
* @param {Number} imageX X coordinate in image coordinate system.
* @param {Number} imageY Y coordinate in image coordinate system.
* @return {OpenSeadragon.Point} a point representing the coordinates in the viewport.
*/ */
imageToViewportCoordinates: function( imageX, imageY ) { imageToViewportCoordinates: function( imageX, imageY ) {
return new $.Point( imageX / this.contentSize.x, imageY / this.contentSize.y / this.contentAspectX); if ( arguments.length == 1 ) {
//they passed a point instead of individual components
return this.imageToViewportCoordinates( imageX.x, imageX.y );
}
return new $.Point( imageX / this.contentSize.x, imageY / this.contentSize.y / this.contentAspectX );
}, },
/** /**
* Translates from a rectangle which describes a portion of * Translates from a rectangle which describes a portion of the image in
* the image in pixel coordinates to OpenSeadragon viewport * pixel coordinates to OpenSeadragon viewport rectangle coordinates.
* rectangle coordinates. * This method can be called either by passing X,Y,width,height or an
* OpenSeadragon.Rect
* @function
* @param {OpenSeadragon.Rect} imageX the rectangle in image coordinate system.
* @param {Number} imageX the X coordinate of the top left corner of the rectangle
* in image coordinate system.
* @param {Number} imageY the Y coordinate of the top left corner of the rectangle
* in image coordinate system.
* @param {Number} pixelWidth the width in pixel of the rectangle.
* @param {Number} pixelHeight the height in pixel of the rectangle.
*/ */
imageToViewportRectangle: function( imageX, imageY, pixelWidth, pixelHeight ) { imageToViewportRectangle: function( imageX, imageY, pixelWidth, pixelHeight ) {
var coordA, var coordA,
coordB, coordB,
rect; rect;
if( arguments.length == 1 ){ if( arguments.length == 1 ) {
//they passed a rectangle instead of individual components //they passed a rectangle instead of individual components
rect = imageX; rect = imageX;
return this.imageToViewportRectangle(rect.x, rect.y, rect.width, rect.height); return this.imageToViewportRectangle(
rect.x, rect.y, rect.width, rect.height
);
} }
coordA = this.imageToViewportCoordinates( coordA = this.imageToViewportCoordinates(
imageX, imageY imageX, imageY
@ -763,6 +794,41 @@ $.Viewport.prototype = {
coordB.x, coordB.x,
coordB.y coordB.y
); );
},
/**
* Translates from a rectangle which describes a portion of
* the viewport in point coordinates to image rectangle coordinates.
* This method can be called either by passing X,Y,width,height or an
* OpenSeadragon.Rect
* @function
* @param {OpenSeadragon.Rect} viewerX the rectangle in viewport coordinate system.
* @param {Number} viewerX the X coordinate of the top left corner of the rectangle
* in viewport coordinate system.
* @param {Number} imageY the Y coordinate of the top left corner of the rectangle
* in viewport coordinate system.
* @param {Number} pointWidth the width of the rectangle in viewport coordinate system.
* @param {Number} pointHeight the height of the rectangle in viewport coordinate system.
*/
viewportToImageRectangle: function( viewerX, viewerY, pointWidth, pointHeight ) {
var coordA,
coordB,
rect;
if ( arguments.length == 1 ) {
//they passed a rectangle instead of individual components
rect = viewerX;
return this.viewportToImageRectangle(
rect.x, rect.y, rect.width, rect.height
);
}
coordA = this.viewportToImageCoordinates( viewerX, viewerY );
coordB = this.viewportToImageCoordinates( pointWidth, pointHeight );
return new $.Rect(
coordA.x,
coordA.y,
coordB.x,
coordB.y
);
} }
}; };