2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - Rect
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
(function( $ ){
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2013-11-25 20:48:44 +04:00
|
|
|
* @class Rect
|
|
|
|
* @classdesc A Rectangle really represents a 2x2 matrix where each row represents a
|
2013-06-19 21:33:25 +04:00
|
|
|
* 2 dimensional vector component, the first is (x,y) and the second is
|
|
|
|
* (width, height). The latter component implies the equation of a simple
|
2012-02-01 00:59:09 +04:00
|
|
|
* plane.
|
|
|
|
*
|
2013-11-16 10:19:53 +04:00
|
|
|
* @memberof OpenSeadragon
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {Number} x The vector component 'x'.
|
|
|
|
* @param {Number} y The vector component 'y'.
|
|
|
|
* @param {Number} width The vector component 'height'.
|
|
|
|
* @param {Number} height The vector component 'width'.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2011-12-30 03:16:51 +04:00
|
|
|
$.Rect = function( x, y, width, height ) {
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'x'.
|
|
|
|
* @member {Number} x
|
|
|
|
* @memberof OpenSeadragon.Rect#
|
|
|
|
*/
|
2011-12-30 03:16:51 +04:00
|
|
|
this.x = typeof ( x ) == "number" ? x : 0;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'y'.
|
|
|
|
* @member {Number} y
|
|
|
|
* @memberof OpenSeadragon.Rect#
|
|
|
|
*/
|
2011-12-30 03:16:51 +04:00
|
|
|
this.y = typeof ( y ) == "number" ? y : 0;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'width'.
|
|
|
|
* @member {Number} width
|
|
|
|
* @memberof OpenSeadragon.Rect#
|
|
|
|
*/
|
2011-12-30 03:16:51 +04:00
|
|
|
this.width = typeof ( width ) == "number" ? width : 0;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'height'.
|
|
|
|
* @member {Number} height
|
|
|
|
* @memberof OpenSeadragon.Rect#
|
|
|
|
*/
|
2011-12-30 03:16:51 +04:00
|
|
|
this.height = typeof ( height ) == "number" ? height : 0;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2013-11-16 10:19:53 +04:00
|
|
|
$.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The aspect ratio is simply the ratio of width to height.
|
|
|
|
* @function
|
|
|
|
* @returns {Number} The ratio of width to height.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getAspectRatio: function() {
|
|
|
|
return this.width / this.height;
|
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2013-08-14 01:39:22 +04:00
|
|
|
* Provides the coordinates of the upper-left corner of the rectangle as a
|
2012-02-01 00:59:09 +04:00
|
|
|
* point.
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Point} The coordinate of the upper-left corner of
|
|
|
|
* the rectangle.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getTopLeft: function() {
|
2013-08-14 01:39:22 +04:00
|
|
|
return new $.Point(
|
|
|
|
this.x,
|
|
|
|
this.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Provides the coordinates of the bottom-right corner of the rectangle as a
|
|
|
|
* point.
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Point} The coordinate of the bottom-right corner of
|
|
|
|
* the rectangle.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getBottomRight: function() {
|
2011-12-30 03:16:51 +04:00
|
|
|
return new $.Point(
|
2013-06-19 21:33:25 +04:00
|
|
|
this.x + this.width,
|
2011-12-30 03:16:51 +04:00
|
|
|
this.y + this.height
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2013-08-14 01:39:22 +04:00
|
|
|
/**
|
|
|
|
* Provides the coordinates of the top-right corner of the rectangle as a
|
|
|
|
* point.
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Point} The coordinate of the top-right corner of
|
|
|
|
* the rectangle.
|
|
|
|
*/
|
|
|
|
getTopRight: function() {
|
|
|
|
return new $.Point(
|
|
|
|
this.x + this.width,
|
|
|
|
this.y
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides the coordinates of the bottom-left corner of the rectangle as a
|
|
|
|
* point.
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Point} The coordinate of the bottom-left corner of
|
|
|
|
* the rectangle.
|
|
|
|
*/
|
|
|
|
getBottomLeft: function() {
|
|
|
|
return new $.Point(
|
|
|
|
this.x,
|
|
|
|
this.y + this.height
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Computes the center of the rectangle.
|
|
|
|
* @function
|
2013-08-14 01:39:22 +04:00
|
|
|
* @returns {OpenSeadragon.Point} The center of the rectangle as represented
|
2012-02-01 00:59:09 +04:00
|
|
|
* as represented by a 2-dimensional vector (x,y)
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getCenter: function() {
|
2011-12-30 03:16:51 +04:00
|
|
|
return new $.Point(
|
|
|
|
this.x + this.width / 2.0,
|
|
|
|
this.y + this.height / 2.0
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Returns the width and height component as a vector OpenSeadragon.Point
|
|
|
|
* @function
|
2013-08-14 01:39:22 +04:00
|
|
|
* @returns {OpenSeadragon.Point} The 2 dimensional vector representing the
|
2012-02-01 00:59:09 +04:00
|
|
|
* the width and height of the rectangle.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getSize: function() {
|
2011-12-30 03:16:51 +04:00
|
|
|
return new $.Point( this.width, this.height );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2013-08-14 01:39:22 +04:00
|
|
|
* Determines if two Rectangles have equivalent components.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Rect} rectangle The Rectangle to compare to.
|
|
|
|
* @return {Boolean} 'true' if all components are equal, otherwise 'false'.
|
|
|
|
*/
|
2012-01-18 03:30:41 +04:00
|
|
|
equals: function( other ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
return ( other instanceof $.Rect ) &&
|
2013-06-19 21:33:25 +04:00
|
|
|
( this.x === other.x ) &&
|
2011-12-30 03:16:51 +04:00
|
|
|
( this.y === other.y ) &&
|
2013-06-19 21:33:25 +04:00
|
|
|
( this.width === other.width ) &&
|
2011-12-30 03:16:51 +04:00
|
|
|
( this.height === other.height );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2013-08-14 01:39:22 +04:00
|
|
|
* Rotates a rectangle around a point. Currently only 90, 180, and 270
|
|
|
|
* degrees are supported.
|
|
|
|
* @function
|
|
|
|
* @param {Number} degrees The angle in degrees to rotate.
|
|
|
|
* @param {OpenSeadragon.Point} pivot The point about which to rotate.
|
|
|
|
* Defaults to the center of the rectangle.
|
|
|
|
* @return {OpenSeadragon.Rect}
|
|
|
|
*/
|
|
|
|
rotate: function( degrees, pivot ) {
|
|
|
|
// TODO support arbitrary rotation
|
|
|
|
var width = this.width,
|
|
|
|
height = this.height,
|
|
|
|
newTopLeft;
|
|
|
|
|
2013-08-16 02:15:20 +04:00
|
|
|
degrees = ( degrees + 360 ) % 360;
|
2014-08-22 17:57:20 +04:00
|
|
|
if (degrees % 90 !== 0) {
|
2014-08-20 12:21:02 +04:00
|
|
|
throw new Error('Currently only 0, 90, 180, and 270 degrees are supported.');
|
|
|
|
}
|
|
|
|
|
2013-08-16 02:15:20 +04:00
|
|
|
if( degrees === 0 ){
|
2013-08-16 21:32:21 +04:00
|
|
|
return new $.Rect(
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.width,
|
|
|
|
this.height
|
|
|
|
);
|
2013-08-16 02:15:20 +04:00
|
|
|
}
|
|
|
|
|
2013-08-14 01:39:22 +04:00
|
|
|
pivot = pivot || this.getCenter();
|
|
|
|
|
|
|
|
switch ( degrees ) {
|
|
|
|
case 90:
|
|
|
|
newTopLeft = this.getBottomLeft();
|
|
|
|
width = this.height;
|
|
|
|
height = this.width;
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
newTopLeft = this.getBottomRight();
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
newTopLeft = this.getTopRight();
|
|
|
|
width = this.height;
|
|
|
|
height = this.width;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newTopLeft = this.getTopLeft();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
newTopLeft = newTopLeft.rotate(degrees, pivot);
|
|
|
|
|
|
|
|
return new $.Rect(newTopLeft.x, newTopLeft.y, width, height);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a string representation of the rectangle which is useful for
|
2012-02-01 00:59:09 +04:00
|
|
|
* debugging.
|
|
|
|
* @function
|
|
|
|
* @returns {String} A string representation of the rectangle.
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
toString: function() {
|
2013-06-19 21:33:25 +04:00
|
|
|
return "[" +
|
|
|
|
Math.round(this.x*100) + "," +
|
|
|
|
Math.round(this.y*100) + "," +
|
2013-01-24 08:00:11 +04:00
|
|
|
Math.round(this.width*100) + "x" +
|
2013-06-19 21:33:25 +04:00
|
|
|
Math.round(this.height*100) +
|
2011-12-30 03:16:51 +04:00
|
|
|
"]";
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|