2011-12-06 07:50:25 +04:00
|
|
|
(function( $ ){
|
2012-01-25 23:14:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
$.Viewport = function( options ) {
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
//backward compatibility for positional args while prefering more
|
|
|
|
//idiomatic javascript options object as the only argument
|
|
|
|
var args = arguments;
|
|
|
|
if( args.length && args[ 0 ] instanceof $.Point ){
|
2012-01-24 07:48:45 +04:00
|
|
|
options = {
|
2012-03-01 17:38:15 +04:00
|
|
|
containerSize: args[ 0 ],
|
|
|
|
contentSize: args[ 1 ],
|
|
|
|
config: args[ 2 ]
|
2012-01-24 07:48:45 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
//options.config and the general config argument are deprecated
|
|
|
|
//in favor of the more direct specification of optional settings
|
|
|
|
//being pass directly on the options object
|
|
|
|
if ( options.config ){
|
|
|
|
$.extend( true, options, options.config );
|
|
|
|
delete options.config;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( true, this, {
|
|
|
|
|
|
|
|
//required settings
|
|
|
|
containerSize: null,
|
|
|
|
contentSize: null,
|
|
|
|
|
|
|
|
//internal state properties
|
|
|
|
zoomPoint: null,
|
2013-02-14 04:44:23 +04:00
|
|
|
viewer: null,
|
2012-03-01 17:38:15 +04:00
|
|
|
|
|
|
|
//configurable options
|
|
|
|
springStiffness: $.DEFAULT_SETTINGS.springStiffness,
|
|
|
|
animationTime: $.DEFAULT_SETTINGS.animationTime,
|
|
|
|
minZoomImageRatio: $.DEFAULT_SETTINGS.minZoomImageRatio,
|
|
|
|
maxZoomPixelRatio: $.DEFAULT_SETTINGS.maxZoomPixelRatio,
|
|
|
|
visibilityRatio: $.DEFAULT_SETTINGS.visibilityRatio,
|
|
|
|
wrapHorizontal: $.DEFAULT_SETTINGS.wrapHorizontal,
|
2013-02-06 06:26:40 +04:00
|
|
|
wrapVertical: $.DEFAULT_SETTINGS.wrapVertical,
|
|
|
|
defaultZoomLevel: $.DEFAULT_SETTINGS.defaultZoomLevel,
|
|
|
|
minZoomLevel: $.DEFAULT_SETTINGS.minZoomLevel,
|
|
|
|
maxZoomLevel: $.DEFAULT_SETTINGS.maxZoomLevel
|
2012-03-01 17:38:15 +04:00
|
|
|
|
|
|
|
}, options );
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringX = new $.Spring({
|
|
|
|
initial: 0,
|
2012-03-01 17:38:15 +04:00
|
|
|
springStiffness: this.springStiffness,
|
|
|
|
animationTime: this.animationTime
|
2012-01-05 03:14:20 +04:00
|
|
|
});
|
|
|
|
this.centerSpringY = new $.Spring({
|
|
|
|
initial: 0,
|
2012-03-01 17:38:15 +04:00
|
|
|
springStiffness: this.springStiffness,
|
|
|
|
animationTime: this.animationTime
|
2012-01-05 03:14:20 +04:00
|
|
|
});
|
2012-02-28 03:29:00 +04:00
|
|
|
this.zoomSpring = new $.Spring({
|
2012-01-05 03:14:20 +04:00
|
|
|
initial: 1,
|
2012-03-01 17:38:15 +04:00
|
|
|
springStiffness: this.springStiffness,
|
|
|
|
animationTime: this.animationTime
|
2012-01-05 03:14:20 +04:00
|
|
|
});
|
2012-02-28 03:29:00 +04:00
|
|
|
|
2012-04-03 11:08:27 +04:00
|
|
|
this.resetContentSize( this.contentSize );
|
2012-01-05 04:45:47 +04:00
|
|
|
this.goHome( true );
|
2011-12-06 07:50:25 +04:00
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
|
|
|
$.Viewport.prototype = {
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2013-02-14 04:44:23 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
|
|
|
*/
|
2012-04-03 11:08:27 +04:00
|
|
|
resetContentSize: function( contentSize ){
|
|
|
|
this.contentSize = contentSize;
|
|
|
|
this.contentAspectX = this.contentSize.x / this.contentSize.y;
|
|
|
|
this.contentAspectY = this.contentSize.y / this.contentSize.x;
|
2013-01-31 21:30:13 +04:00
|
|
|
this.fitWidthBounds = new $.Rect( 0, 0, 1, this.contentAspectY );
|
|
|
|
this.fitHeightBounds = new $.Rect( 0, 0, this.contentAspectY, this.contentAspectY);
|
2012-08-29 22:46:34 +04:00
|
|
|
|
2013-01-31 21:30:13 +04:00
|
|
|
this.homeBounds = new $.Rect( 0, 0, 1, this.contentAspectY );
|
2013-02-14 04:44:23 +04:00
|
|
|
|
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'reset-size', {
|
|
|
|
contentSize: contentSize,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:51:37 +04:00
|
|
|
return this;
|
2012-04-03 11:08:27 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 03:14:20 +04:00
|
|
|
getHomeZoom: function() {
|
2012-08-30 04:48:45 +04:00
|
|
|
var aspectFactor =
|
|
|
|
this.contentAspectX / this.getAspectRatio();
|
2012-04-03 11:08:27 +04:00
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
if( this.defaultZoomLevel ){
|
|
|
|
return this.defaultZoomLevel;
|
|
|
|
} else {
|
|
|
|
return ( aspectFactor >= 1 ) ?
|
|
|
|
1 :
|
|
|
|
aspectFactor;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
getHomeBounds: function() {
|
|
|
|
var center = this.homeBounds.getCenter( ),
|
|
|
|
width = 1.0 / this.getHomeZoom( ),
|
|
|
|
height = width / this.getAspectRatio();
|
|
|
|
|
|
|
|
return new $.Rect(
|
|
|
|
center.x - ( width / 2.0 ),
|
|
|
|
center.y - ( height / 2.0 ),
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Boolean} immediately
|
|
|
|
*/
|
|
|
|
goHome: function( immediately ) {
|
2013-02-14 04:44:23 +04:00
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'home', {
|
|
|
|
immediately: immediately,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.fitBounds( this.getHomeBounds(), immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 03:14:20 +04:00
|
|
|
getMinZoom: function() {
|
2012-08-29 22:46:34 +04:00
|
|
|
var homeZoom = this.getHomeZoom(),
|
2013-02-06 06:26:40 +04:00
|
|
|
zoom = this.minZoomLevel ?
|
|
|
|
this.minZoomLevel :
|
|
|
|
this.minZoomImageRatio * homeZoom;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
return Math.min( zoom, homeZoom );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 03:14:20 +04:00
|
|
|
getMaxZoom: function() {
|
2013-02-06 06:26:40 +04:00
|
|
|
var zoom = this.maxZoomLevel ?
|
|
|
|
this.maxZoomLevel :
|
|
|
|
( this.contentSize.x * this.maxZoomPixelRatio / this.containerSize.x );
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
return Math.max( zoom, this.getHomeZoom() );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-01-05 03:14:20 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getAspectRatio: function() {
|
2012-01-05 03:14:20 +04:00
|
|
|
return this.containerSize.x / this.containerSize.y;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-01-05 03:14:20 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
getContainerSize: function() {
|
2012-01-25 23:14:02 +04:00
|
|
|
return new $.Point(
|
|
|
|
this.containerSize.x,
|
|
|
|
this.containerSize.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
getBounds: function( current ) {
|
2012-01-25 23:14:02 +04:00
|
|
|
var center = this.getCenter( current ),
|
|
|
|
width = 1.0 / this.getZoom( current ),
|
2012-01-05 03:14:20 +04:00
|
|
|
height = width / this.getAspectRatio();
|
|
|
|
|
|
|
|
return new $.Rect(
|
2012-03-07 07:20:00 +04:00
|
|
|
center.x - ( width / 2.0 ),
|
|
|
|
center.y - ( height / 2.0 ),
|
2012-01-05 03:14:20 +04:00
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
getCenter: function( current ) {
|
2011-12-17 02:56:38 +04:00
|
|
|
var centerCurrent = new $.Point(
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringX.current.value,
|
|
|
|
this.centerSpringY.current.value
|
|
|
|
),
|
|
|
|
centerTarget = new $.Point(
|
|
|
|
this.centerSpringX.target.value,
|
|
|
|
this.centerSpringY.target.value
|
|
|
|
),
|
|
|
|
oldZoomPixel,
|
|
|
|
zoom,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
bounds,
|
|
|
|
newZoomPixel,
|
|
|
|
deltaZoomPixels,
|
|
|
|
deltaZoomPoints;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( current ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
return centerCurrent;
|
2012-01-25 23:14:02 +04:00
|
|
|
} else if ( !this.zoomPoint ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
return centerTarget;
|
|
|
|
}
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
oldZoomPixel = this.pixelFromPoint(this.zoomPoint, true);
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
zoom = this.getZoom();
|
|
|
|
width = 1.0 / zoom;
|
|
|
|
height = width / this.getAspectRatio();
|
|
|
|
bounds = new $.Rect(
|
|
|
|
centerCurrent.x - width / 2.0,
|
|
|
|
centerCurrent.y - height / 2.0,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
newZoomPixel = this.zoomPoint.minus(
|
|
|
|
bounds.getTopLeft()
|
|
|
|
).times(
|
|
|
|
this.containerSize.x / bounds.width
|
|
|
|
);
|
|
|
|
deltaZoomPixels = newZoomPixel.minus( oldZoomPixel );
|
|
|
|
deltaZoomPoints = deltaZoomPixels.divide( this.containerSize.x * zoom );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
return centerTarget.plus( deltaZoomPoints );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
getZoom: function( current ) {
|
|
|
|
if ( current ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
return this.zoomSpring.current.value;
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
2012-01-05 03:14:20 +04:00
|
|
|
return this.zoomSpring.target.value;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
applyConstraints: function( immediately ) {
|
|
|
|
var actualZoom = this.getZoom(),
|
|
|
|
constrainedZoom = Math.max(
|
|
|
|
Math.min( actualZoom, this.getMaxZoom() ),
|
|
|
|
this.getMinZoom()
|
|
|
|
),
|
|
|
|
bounds,
|
|
|
|
horizontalThreshold,
|
|
|
|
verticalThreshold,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
top,
|
|
|
|
bottom,
|
2013-02-06 06:26:40 +04:00
|
|
|
center,
|
2012-01-05 04:45:47 +04:00
|
|
|
dx = 0,
|
2013-02-06 06:26:40 +04:00
|
|
|
dy = 0,
|
|
|
|
dx1 = 0, dx2 = 0, dy1 = 0, dy2 = 0;
|
2012-01-05 04:45:47 +04:00
|
|
|
|
|
|
|
if ( actualZoom != constrainedZoom ) {
|
|
|
|
this.zoomTo( constrainedZoom, this.zoomPoint, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 04:45:47 +04:00
|
|
|
bounds = this.getBounds();
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 04:45:47 +04:00
|
|
|
horizontalThreshold = this.visibilityRatio * bounds.width;
|
|
|
|
verticalThreshold = this.visibilityRatio * bounds.height;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 04:45:47 +04:00
|
|
|
left = bounds.x + bounds.width;
|
|
|
|
right = 1 - bounds.x;
|
|
|
|
top = bounds.y + bounds.height;
|
2012-04-03 11:08:27 +04:00
|
|
|
bottom = this.contentAspectY - bounds.y;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
if ( this.wrapHorizontal ) {
|
2012-01-05 04:45:47 +04:00
|
|
|
//do nothing
|
2013-02-06 06:26:40 +04:00
|
|
|
} else {
|
|
|
|
if ( left < horizontalThreshold ) {
|
|
|
|
dx = horizontalThreshold - left;
|
|
|
|
}
|
|
|
|
if ( right < horizontalThreshold ) {
|
|
|
|
dx = dx ?
|
|
|
|
( dx + right - horizontalThreshold ) / 2 :
|
|
|
|
( right - horizontalThreshold );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
if ( this.wrapVertical ) {
|
2012-01-05 04:45:47 +04:00
|
|
|
//do nothing
|
2013-02-06 06:26:40 +04:00
|
|
|
} else {
|
|
|
|
if ( top < verticalThreshold ) {
|
|
|
|
dy = ( verticalThreshold - top );
|
|
|
|
}
|
|
|
|
if ( bottom < verticalThreshold ) {
|
|
|
|
dy = dy ?
|
|
|
|
( dy + bottom - verticalThreshold ) / 2 :
|
|
|
|
( bottom - verticalThreshold );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
if ( dx || dy || immediately ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
bounds.x += dx;
|
|
|
|
bounds.y += dy;
|
2013-02-06 06:26:40 +04:00
|
|
|
if( bounds.width > 1 ){
|
|
|
|
bounds.x = 0.5 - bounds.width/2;
|
|
|
|
}
|
|
|
|
if( bounds.height > this.contentAspectY ){
|
|
|
|
bounds.y = this.contentAspectY/2 - bounds.height/2;
|
|
|
|
}
|
2012-01-05 04:45:47 +04:00
|
|
|
this.fitBounds( bounds, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2013-02-14 04:44:23 +04:00
|
|
|
|
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'constrain', {
|
|
|
|
immediately: immediately,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:51:37 +04:00
|
|
|
return this;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Boolean} immediately
|
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
ensureVisible: function( immediately ) {
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.applyConstraints( immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2012-02-15 23:50:27 +04:00
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Rect} bounds
|
|
|
|
* @param {Boolean} immediately
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2012-01-05 04:45:47 +04:00
|
|
|
fitBounds: function( bounds, immediately ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
var aspect = this.getAspectRatio(),
|
|
|
|
center = bounds.getCenter(),
|
|
|
|
newBounds = new $.Rect(
|
|
|
|
bounds.x,
|
|
|
|
bounds.y,
|
|
|
|
bounds.width,
|
|
|
|
bounds.height
|
|
|
|
),
|
|
|
|
oldBounds,
|
|
|
|
oldZoom,
|
|
|
|
newZoom,
|
2012-01-05 04:45:47 +04:00
|
|
|
referencePoint;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( newBounds.getAspectRatio() >= aspect ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
newBounds.height = bounds.width / aspect;
|
2012-01-05 04:45:47 +04:00
|
|
|
newBounds.y = center.y - newBounds.height / 2;
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
|
|
|
newBounds.width = bounds.height * aspect;
|
2012-01-05 04:45:47 +04:00
|
|
|
newBounds.x = center.x - newBounds.width / 2;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
this.panTo( this.getCenter( true ), true );
|
|
|
|
this.zoomTo( this.getZoom( true ), null, true );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
oldBounds = this.getBounds();
|
|
|
|
oldZoom = this.getZoom();
|
|
|
|
newZoom = 1.0 / newBounds.width;
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( newZoom == oldZoom || newBounds.width == oldBounds.width ) {
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.panTo( center, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 04:45:47 +04:00
|
|
|
referencePoint = oldBounds.getTopLeft().times(
|
2012-01-05 03:14:20 +04:00
|
|
|
this.containerSize.x / oldBounds.width
|
|
|
|
).minus(
|
|
|
|
newBounds.getTopLeft().times(
|
|
|
|
this.containerSize.x / newBounds.width
|
|
|
|
)
|
|
|
|
).divide(
|
|
|
|
this.containerSize.x / oldBounds.width -
|
|
|
|
this.containerSize.x / newBounds.width
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.zoomTo( newZoom, referencePoint, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-04-03 11:08:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Boolean} immediately
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-04-03 11:08:27 +04:00
|
|
|
*/
|
|
|
|
fitVertically: function( immediately ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
var center = this.getCenter();
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
if ( this.wrapHorizontal ) {
|
2012-01-25 23:14:02 +04:00
|
|
|
center.x = ( 1 + ( center.x % 1 ) ) % 1;
|
|
|
|
this.centerSpringX.resetTo( center.x );
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringX.update();
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
if ( this.wrapVertical ) {
|
2012-01-25 23:14:02 +04:00
|
|
|
center.y = (
|
2012-04-03 11:08:27 +04:00
|
|
|
this.contentAspectY + ( center.y % this.contentAspectY )
|
|
|
|
) % this.contentAspectY;
|
2012-01-25 23:14:02 +04:00
|
|
|
this.centerSpringY.resetTo( center.y );
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringY.update();
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.fitBounds( this.fitHeightBounds, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-04-03 11:08:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Boolean} immediately
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-04-03 11:08:27 +04:00
|
|
|
*/
|
|
|
|
fitHorizontally: function( immediately ) {
|
|
|
|
var center = this.getCenter();
|
|
|
|
|
|
|
|
if ( this.wrapHorizontal ) {
|
|
|
|
center.x = (
|
|
|
|
this.contentAspectX + ( center.x % this.contentAspectX )
|
|
|
|
) % this.contentAspectX;
|
|
|
|
this.centerSpringX.resetTo( center.x );
|
|
|
|
this.centerSpringX.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.wrapVertical ) {
|
|
|
|
center.y = ( 1 + ( center.y % 1 ) ) % 1;
|
|
|
|
this.centerSpringY.resetTo( center.y );
|
|
|
|
this.centerSpringY.update();
|
|
|
|
}
|
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.fitBounds( this.fitWidthBounds, immediately );
|
2012-04-03 11:08:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Point} delta
|
|
|
|
* @param {Boolean} immediately
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
panBy: function( delta, immediately ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
var center = new $.Point(
|
|
|
|
this.centerSpringX.target.value,
|
|
|
|
this.centerSpringY.target.value
|
|
|
|
);
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.panTo( center.plus( delta ), immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Point} center
|
|
|
|
* @param {Boolean} immediately
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
panTo: function( center, immediately ) {
|
|
|
|
if ( immediately ) {
|
|
|
|
this.centerSpringX.resetTo( center.x );
|
|
|
|
this.centerSpringY.resetTo( center.y );
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
2012-01-25 23:14:02 +04:00
|
|
|
this.centerSpringX.springTo( center.x );
|
|
|
|
this.centerSpringY.springTo( center.y );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2013-01-31 01:51:37 +04:00
|
|
|
|
2013-02-14 04:44:23 +04:00
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'pan', {
|
|
|
|
center: center,
|
|
|
|
immediately: immediately,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:51:37 +04:00
|
|
|
return this;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
zoomBy: function( factor, refPoint, immediately ) {
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.zoomTo( this.zoomSpring.target.value * factor, refPoint, immediately );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
zoomTo: function( zoom, refPoint, immediately ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2013-01-31 01:51:37 +04:00
|
|
|
this.zoomPoint = refPoint instanceof $.Point ?
|
|
|
|
refPoint :
|
|
|
|
null;
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( immediately ) {
|
|
|
|
this.zoomSpring.resetTo( zoom );
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
2012-01-25 23:14:02 +04:00
|
|
|
this.zoomSpring.springTo( zoom );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 04:44:23 +04:00
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'zoom', {
|
|
|
|
zoom: zoom,
|
|
|
|
refPoint: refPoint,
|
|
|
|
immediately: immediately,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:51:37 +04:00
|
|
|
|
|
|
|
return this;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
2013-02-14 04:44:23 +04:00
|
|
|
* @return {OpenSeadragon.Viewport} Chainable.
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
resize: function( newContainerSize, maintain ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
var oldBounds = this.getBounds(),
|
|
|
|
newBounds = oldBounds,
|
|
|
|
widthDeltaFactor = newContainerSize.x / this.containerSize.x;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
this.containerSize = new $.Point(
|
|
|
|
newContainerSize.x,
|
|
|
|
newContainerSize.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
if (maintain) {
|
2012-01-18 03:30:41 +04:00
|
|
|
newBounds.width = oldBounds.width * widthDeltaFactor;
|
2011-12-06 07:50:25 +04:00
|
|
|
newBounds.height = newBounds.width / this.getAspectRatio();
|
|
|
|
}
|
|
|
|
|
2013-02-14 04:44:23 +04:00
|
|
|
if( this.viewer ){
|
|
|
|
this.viewer.raiseEvent( 'resize', {
|
|
|
|
newContainerSize: newContainerSize,
|
|
|
|
maintain: maintain,
|
|
|
|
viewer: this.viewer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-06 06:26:40 +04:00
|
|
|
return this.fitBounds( newBounds, true );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
update: function() {
|
2012-01-05 03:14:20 +04:00
|
|
|
var oldCenterX = this.centerSpringX.current.value,
|
|
|
|
oldCenterY = this.centerSpringY.current.value,
|
|
|
|
oldZoom = this.zoomSpring.current.value,
|
|
|
|
oldZoomPixel,
|
|
|
|
newZoomPixel,
|
|
|
|
deltaZoomPixels,
|
|
|
|
deltaZoomPoints;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
if (this.zoomPoint) {
|
2012-01-25 23:14:02 +04:00
|
|
|
oldZoomPixel = this.pixelFromPoint( this.zoomPoint, true );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
this.zoomSpring.update();
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
if (this.zoomPoint && this.zoomSpring.current.value != oldZoom) {
|
|
|
|
newZoomPixel = this.pixelFromPoint( this.zoomPoint, true );
|
2012-01-25 23:14:02 +04:00
|
|
|
deltaZoomPixels = newZoomPixel.minus( oldZoomPixel );
|
2012-01-05 03:14:20 +04:00
|
|
|
deltaZoomPoints = this.deltaPointsFromPixels( deltaZoomPixels, true );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringX.shiftBy( deltaZoomPoints.x );
|
|
|
|
this.centerSpringY.shiftBy( deltaZoomPoints.y );
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
|
|
|
this.zoomPoint = null;
|
|
|
|
}
|
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
this.centerSpringX.update();
|
|
|
|
this.centerSpringY.update();
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-05 03:14:20 +04:00
|
|
|
return this.centerSpringX.current.value != oldCenterX ||
|
|
|
|
this.centerSpringY.current.value != oldCenterY ||
|
|
|
|
this.zoomSpring.current.value != oldZoom;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
deltaPixelsFromPoints: function( deltaPoints, current ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
return deltaPoints.times(
|
|
|
|
this.containerSize.x * this.getZoom( current )
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
deltaPointsFromPixels: function( deltaPixels, current ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
return deltaPixels.divide(
|
|
|
|
this.containerSize.x * this.getZoom( current )
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
pixelFromPoint: function( point, current ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
var bounds = this.getBounds( current );
|
|
|
|
return point.minus(
|
|
|
|
bounds.getTopLeft()
|
|
|
|
).times(
|
|
|
|
this.containerSize.x / bounds.width
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
pointFromPixel: function( pixel, current ) {
|
2012-01-05 03:14:20 +04:00
|
|
|
var bounds = this.getBounds( current );
|
|
|
|
return pixel.divide(
|
|
|
|
this.containerSize.x / bounds.width
|
|
|
|
).plus(
|
|
|
|
bounds.getTopLeft()
|
|
|
|
);
|
2013-02-11 07:53:51 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates from Seajax viewer coordinate
|
|
|
|
* system to image coordinate system
|
|
|
|
*/
|
|
|
|
viewportToImageCoordinates: function(viewerX, viewerY) {
|
|
|
|
return new $.Point(viewerX * this.contentSize.x, viewerY * this.contentSize.y * this.contentAspectX);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates from image coordinate system to
|
|
|
|
* Seajax viewer coordinate system
|
|
|
|
*/
|
|
|
|
imageToViewportCoordinates: function( imageX, imageY ) {
|
|
|
|
return new $.Point( imageX / this.contentSize.x, imageY / this.contentSize.y / this.contentAspectX);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates from a rectanlge which describes a portion of
|
|
|
|
* the image in pixel coordinates to OpenSeadragon viewport
|
|
|
|
* rectangle coordinates.
|
|
|
|
*/
|
|
|
|
imageToViewportRectangle: function( imageX, imageY, pixelWidth, pixelHeight ) {
|
|
|
|
var coordA,
|
|
|
|
coordB,
|
|
|
|
rect;
|
|
|
|
if( arguments.length == 1 ){
|
|
|
|
//they passed a rectangle instead of individual components
|
|
|
|
rect = imageX;
|
|
|
|
return this.imageToViewportRectangle(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
coordA = this.imageToViewportCoordinates(
|
|
|
|
imageX, imageY
|
|
|
|
);
|
|
|
|
coordB = this.imageToViewportCoordinates(
|
|
|
|
pixelWidth, pixelHeight
|
|
|
|
);
|
|
|
|
return new $.Rect(
|
|
|
|
coordA.x,
|
|
|
|
coordA.y,
|
|
|
|
coordA.x + coordB.x,
|
|
|
|
coordA.y + coordB.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}( OpenSeadragon ));
|