2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
(function( $ ){
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
var TIMEOUT = 5000,
|
2012-01-12 03:22:13 +04:00
|
|
|
|
2012-01-18 03:30:41 +04:00
|
|
|
BROWSER = $.Browser.vendor,
|
|
|
|
BROWSER_VERSION = $.Browser.version,
|
2012-01-12 03:22:13 +04:00
|
|
|
|
|
|
|
SUBPIXEL_RENDERING = (
|
2012-01-18 03:30:41 +04:00
|
|
|
( BROWSER == $.BROWSERS.FIREFOX ) ||
|
|
|
|
( BROWSER == $.BROWSERS.OPERA ) ||
|
|
|
|
( BROWSER == $.BROWSERS.SAFARI && BROWSER_VERSION >= 4 ) ||
|
|
|
|
( BROWSER == $.BROWSERS.CHROME && BROWSER_VERSION >= 2 )
|
2012-03-01 17:38:15 +04:00
|
|
|
) && ( !navigator.appVersion.match( 'Mobile' ) ),
|
2012-01-12 03:22:13 +04:00
|
|
|
|
2012-01-24 17:03:50 +04:00
|
|
|
USE_CANVAS = $.isFunction( document.createElement( "canvas" ).getContext ) &&
|
2012-01-12 03:22:13 +04:00
|
|
|
SUBPIXEL_RENDERING;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
//console.error( 'USE_CANVAS ' + USE_CANVAS );
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
|
|
|
* @class
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {OpenSeadragon.TileSource} source - Reference to Viewer tile source.
|
|
|
|
* @param {OpenSeadragon.Viewport} viewport - Reference to Viewer viewport.
|
|
|
|
* @param {Element} element - Reference to Viewer 'canvas'.
|
|
|
|
* @property {OpenSeadragon.TileSource} source - Reference to Viewer tile source.
|
|
|
|
* @property {OpenSeadragon.Viewport} viewport - Reference to Viewer viewport.
|
2012-02-01 06:01:37 +04:00
|
|
|
* @property {Element} container - Reference to Viewer 'canvas'.
|
|
|
|
* @property {Element|Canvas} canvas - TODO
|
|
|
|
* @property {CanvasContext} context - TODO
|
2012-02-01 00:59:09 +04:00
|
|
|
* @property {Object} config - Reference to Viewer config.
|
|
|
|
* @property {Number} downloading - How many images are currently being loaded in parallel.
|
|
|
|
* @property {Number} normHeight - Ratio of zoomable image height to width.
|
2012-02-01 06:01:37 +04:00
|
|
|
* @property {Object} tilesMatrix - A '3d' dictionary [level][x][y] --> Tile.
|
|
|
|
* @property {Array} tilesLoaded - An unordered list of Tiles with loaded images.
|
|
|
|
* @property {Object} coverage - A '3d' dictionary [level][x][y] --> Boolean.
|
|
|
|
* @property {Array} overlays - An unordered list of Overlays added.
|
|
|
|
* @property {Array} lastDrawn - An unordered list of Tiles drawn last frame.
|
|
|
|
* @property {Number} lastResetTime - Last time for which the drawer was reset.
|
|
|
|
* @property {Boolean} midUpdate - Is the drawer currently updating the viewport?
|
|
|
|
* @property {Boolean} updateAgain - Does the drawer need to update the viewort again?
|
2012-02-02 01:56:04 +04:00
|
|
|
* @property {Element} element - DEPRECATED Alias for container.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2012-03-01 17:38:15 +04:00
|
|
|
$.Drawer = function( options ) {
|
|
|
|
|
|
|
|
//backward compatibility for positional args while prefering more
|
|
|
|
//idiomatic javascript options object as the only argument
|
2012-03-16 19:36:28 +04:00
|
|
|
var args = arguments,
|
|
|
|
i;
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if( !$.isPlainObject( options ) ){
|
|
|
|
options = {
|
|
|
|
source: args[ 0 ],
|
|
|
|
viewport: args[ 1 ],
|
|
|
|
element: args[ 2 ]
|
|
|
|
};
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
$.extend( true, this, {
|
|
|
|
|
|
|
|
//internal state properties
|
|
|
|
downloading: 0,
|
|
|
|
tilesMatrix: {},
|
|
|
|
tilesLoaded: [],
|
|
|
|
coverage: {},
|
|
|
|
lastDrawn: [],
|
|
|
|
lastResetTime: 0,
|
|
|
|
midUpdate: false,
|
|
|
|
updateAgain: true,
|
|
|
|
|
2012-03-16 19:36:28 +04:00
|
|
|
//internal state / configurable settings
|
|
|
|
overlays: [],
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
//configurable settings
|
|
|
|
maxImageCacheCount: $.DEFAULT_SETTINGS.maxImageCacheCount,
|
|
|
|
imageLoaderLimit: $.DEFAULT_SETTINGS.imageLoaderLimit,
|
|
|
|
minZoomImageRatio: $.DEFAULT_SETTINGS.minZoomImageRatio,
|
|
|
|
wrapHorizontal: $.DEFAULT_SETTINGS.wrapHorizontal,
|
|
|
|
wrapVertical: $.DEFAULT_SETTINGS.wrapVertical,
|
|
|
|
immediateRender: $.DEFAULT_SETTINGS.immediateRender,
|
|
|
|
blendTime: $.DEFAULT_SETTINGS.blendTime,
|
|
|
|
alwaysBlend: $.DEFAULT_SETTINGS.alwaysBlend,
|
|
|
|
minPixelRatio: $.DEFAULT_SETTINGS.minPixelRatio
|
|
|
|
|
|
|
|
}, options );
|
|
|
|
|
|
|
|
this.container = $.getElement( this.element );
|
2012-02-01 00:59:09 +04:00
|
|
|
this.canvas = $.makeNeutralElement( USE_CANVAS ? "canvas" : "div" );
|
|
|
|
this.context = USE_CANVAS ? this.canvas.getContext( "2d" ) : null;
|
2012-03-01 17:38:15 +04:00
|
|
|
this.normHeight = this.source.dimensions.y / this.source.dimensions.x;
|
|
|
|
this.element = this.container;
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
this.canvas.style.width = "100%";
|
|
|
|
this.canvas.style.height = "100%";
|
|
|
|
this.canvas.style.position = "absolute";
|
2012-01-12 03:22:13 +04:00
|
|
|
|
|
|
|
// explicit left-align
|
|
|
|
this.container.style.textAlign = "left";
|
2012-02-01 00:59:09 +04:00
|
|
|
this.container.appendChild( this.canvas );
|
|
|
|
|
2012-03-16 19:36:28 +04:00
|
|
|
//create the correct type of overlay by convention if the overlays
|
|
|
|
//are not already OpenSeadragon.Overlays
|
|
|
|
for( i = 0; i < this.overlays.length; i++ ){
|
|
|
|
if( $.isPlainObject( this.overlays[ i ] ) ){
|
|
|
|
|
|
|
|
(function( _this, overlay ){
|
|
|
|
|
|
|
|
var link = document.createElement("a"),
|
|
|
|
rect = new $.Rect(
|
|
|
|
overlay.x,
|
|
|
|
overlay.y,
|
|
|
|
overlay.width,
|
|
|
|
overlay.height
|
|
|
|
),
|
|
|
|
id = Math.floor(Math.random()*10000000);
|
|
|
|
|
|
|
|
link.href = "#/overlay/"+id;
|
|
|
|
link.id = id;
|
2012-03-20 23:00:25 +04:00
|
|
|
link.className = overlay.className ?
|
|
|
|
overlay.className :
|
2012-03-16 19:36:28 +04:00
|
|
|
"openseadragon-overlay";
|
|
|
|
|
|
|
|
_this.overlays[ i ] = new $.Overlay( link, rect );
|
|
|
|
|
|
|
|
}( this, this.overlays[ i ] ));
|
|
|
|
|
|
|
|
} else if ( $.isFunction( this.overlays[ i ] ) ){
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//this.profiler = new $.Profiler();
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
$.Drawer.prototype = {
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Adds an html element as an overlay to the current viewport. Useful for
|
|
|
|
* highlighting words or areas of interest on an image or other zoomable
|
|
|
|
* interface.
|
|
|
|
* @method
|
|
|
|
* @param {Element|String} element - A reference to an element or an id for
|
|
|
|
* the element which will overlayed.
|
|
|
|
* @param {OpenSeadragon.Point|OpenSeadragon.Rect} location - The point or
|
|
|
|
* rectangle which will be overlayed.
|
|
|
|
* @param {OpenSeadragon.OverlayPlacement} placement - The position of the
|
|
|
|
* viewport which the location coordinates will be treated as relative
|
|
|
|
* to.
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
addOverlay: function( element, location, placement ) {
|
|
|
|
element = $.getElement( element );
|
|
|
|
|
|
|
|
if ( getOverlayIndex( this.overlays, element ) >= 0 ) {
|
|
|
|
// they're trying to add a duplicate overlay
|
|
|
|
return;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
this.overlays.push( new $.Overlay( element, location, placement ) );
|
|
|
|
this.updateAgain = true;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Updates the overlay represented by the reference to the element or
|
|
|
|
* element id moving it to the new location, relative to the new placement.
|
|
|
|
* @method
|
|
|
|
* @param {OpenSeadragon.Point|OpenSeadragon.Rect} location - The point or
|
|
|
|
* rectangle which will be overlayed.
|
|
|
|
* @param {OpenSeadragon.OverlayPlacement} placement - The position of the
|
|
|
|
* viewport which the location coordinates will be treated as relative
|
|
|
|
* to.
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
updateOverlay: function( element, location, placement ) {
|
|
|
|
var i;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
element = $.getElement( element );
|
|
|
|
i = getOverlayIndex( this.overlays, element );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( i >= 0 ) {
|
|
|
|
this.overlays[ i ].update( location, placement );
|
|
|
|
this.updateAgain = true;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
},
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Removes and overlay identified by the reference element or element id
|
|
|
|
* and schedules and update.
|
|
|
|
* @method
|
|
|
|
* @param {Element|String} element - A reference to the element or an
|
|
|
|
* element id which represent the ovelay content to be removed.
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
removeOverlay: function( element ) {
|
|
|
|
var i;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
element = $.getElement( element );
|
|
|
|
i = getOverlayIndex( this.overlays, element );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( i >= 0 ) {
|
|
|
|
this.overlays[ i ].destroy();
|
|
|
|
this.overlays.splice( i, 1 );
|
|
|
|
this.updateAgain = true;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Removes all currently configured Overlays from this Drawer and schedules
|
|
|
|
* and update.
|
|
|
|
* @method
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
clearOverlays: function() {
|
|
|
|
while ( this.overlays.length > 0 ) {
|
|
|
|
this.overlays.pop().destroy();
|
|
|
|
this.updateAgain = true;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
},
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Returns whether the Drawer is scheduled for an update at the
|
|
|
|
* soonest possible opportunity.
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} - Whether the Drawer is scheduled for an update at the
|
|
|
|
* soonest possible opportunity.
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
needsUpdate: function() {
|
|
|
|
return this.updateAgain;
|
|
|
|
},
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Returns the total number of tiles that have been loaded by this Drawer.
|
|
|
|
* @method
|
|
|
|
* @returns {Number} - The total number of tiles that have been loaded by
|
|
|
|
* this Drawer.
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
numTilesLoaded: function() {
|
|
|
|
return this.tilesLoaded.length;
|
|
|
|
},
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Clears all tiles and triggers an update on the next call to
|
|
|
|
* Drawer.prototype.update().
|
|
|
|
* @method
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
reset: function() {
|
|
|
|
clearTiles( this );
|
|
|
|
this.lastResetTime = +new Date();
|
2012-01-12 03:22:13 +04:00
|
|
|
this.updateAgain = true;
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Forces the Drawer to update.
|
|
|
|
* @method
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
update: function() {
|
|
|
|
//this.profiler.beginUpdate();
|
|
|
|
this.midUpdate = true;
|
|
|
|
updateViewport( this );
|
|
|
|
this.midUpdate = false;
|
|
|
|
//this.profiler.endUpdate();
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
/**
|
|
|
|
* Used internally to load images when required. May also be used to
|
|
|
|
* preload a set of images so the browser will have them available in
|
|
|
|
* the local cache to optimize user experience in certain cases. Because
|
|
|
|
* the number of parallel image loads is configurable, if too many images
|
|
|
|
* are currently being loaded, the request will be ignored. Since by
|
2012-03-01 17:38:15 +04:00
|
|
|
* default drawer.imageLoaderLimit is 0, the native browser parallel
|
2012-02-01 06:01:37 +04:00
|
|
|
* image loading policy will be used.
|
|
|
|
* @method
|
|
|
|
* @param {String} src - The url of the image to load.
|
|
|
|
* @param {Function} callback - The function that will be called with the
|
|
|
|
* Image object as the only parameter, whether on 'load' or on 'abort'.
|
|
|
|
* For now this means the callback is expected to distinguish between
|
|
|
|
* error and success conditions by inspecting the Image object.
|
|
|
|
* @return {Boolean} loading - Wheter the request was submitted or ignored
|
2012-03-01 17:38:15 +04:00
|
|
|
* based on OpenSeadragon.DEFAULT_SETTINGS.imageLoaderLimit.
|
2012-02-01 06:01:37 +04:00
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
loadImage: function( src, callback ) {
|
|
|
|
var _this = this,
|
|
|
|
loading = false,
|
|
|
|
image,
|
|
|
|
jobid,
|
|
|
|
complete;
|
2012-02-23 16:18:28 +04:00
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !this.imageLoaderLimit ||
|
|
|
|
this.downloading < this.imageLoaderLimit ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
this.downloading++;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
image = new Image();
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
complete = function( imagesrc ){
|
|
|
|
_this.downloading--;
|
|
|
|
if (typeof ( callback ) == "function") {
|
|
|
|
try {
|
|
|
|
callback( image );
|
|
|
|
} catch ( e ) {
|
|
|
|
$.console.error(
|
|
|
|
"%s while executing %s callback: %s",
|
|
|
|
e.name,
|
|
|
|
src,
|
|
|
|
e.message,
|
|
|
|
e
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
image.onload = function(){
|
|
|
|
finishLoadingImage( image, complete, true );
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
image.onabort = image.onerror = function(){
|
|
|
|
finishLoadingImage( image, complete, false );
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
jobid = window.setTimeout( function(){
|
|
|
|
finishLoadingImage( image, complete, false, jobid );
|
|
|
|
}, TIMEOUT );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
loading = true;
|
|
|
|
image.src = src;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return loading;
|
|
|
|
}
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Pretty much every other line in this needs to be documented so its clear
|
|
|
|
* how each piece of this routine contributes to the drawing process. That's
|
|
|
|
* why there are so many TODO's inside this function.
|
|
|
|
*/
|
|
|
|
function updateViewport( drawer ) {
|
2012-02-23 16:18:28 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
drawer.updateAgain = false;
|
|
|
|
|
|
|
|
var tile,
|
|
|
|
level,
|
|
|
|
best = null,
|
|
|
|
haveDrawn = false,
|
|
|
|
currentTime = +new Date(),
|
|
|
|
viewportSize = drawer.viewport.getContainerSize(),
|
|
|
|
viewportBounds = drawer.viewport.getBounds( true ),
|
|
|
|
viewportTL = viewportBounds.getTopLeft(),
|
|
|
|
viewportBR = viewportBounds.getBottomRight(),
|
|
|
|
zeroRatioC = drawer.viewport.deltaPixelsFromPoints(
|
|
|
|
drawer.source.getPixelRatio( 0 ),
|
|
|
|
true
|
|
|
|
).x,
|
|
|
|
lowestLevel = Math.max(
|
|
|
|
drawer.source.minLevel,
|
|
|
|
Math.floor(
|
2012-03-01 17:38:15 +04:00
|
|
|
Math.log( drawer.minZoomImageRatio ) /
|
2012-02-01 00:59:09 +04:00
|
|
|
Math.log( 2 )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
highestLevel = Math.min(
|
|
|
|
drawer.source.maxLevel,
|
|
|
|
Math.floor(
|
2012-03-01 17:38:15 +04:00
|
|
|
Math.log( zeroRatioC / drawer.minPixelRatio ) /
|
2012-02-01 00:59:09 +04:00
|
|
|
Math.log( 2 )
|
|
|
|
)
|
2012-02-23 16:18:28 +04:00
|
|
|
),
|
|
|
|
renderPixelRatioC,
|
|
|
|
renderPixelRatioT,
|
|
|
|
zeroRatioT,
|
|
|
|
optimalRatio,
|
|
|
|
levelOpacity,
|
|
|
|
levelVisibility;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
while ( drawer.lastDrawn.length > 0 ) {
|
|
|
|
tile = drawer.lastDrawn.pop();
|
|
|
|
tile.beingDrawn = false;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
drawer.canvas.innerHTML = "";
|
|
|
|
if ( USE_CANVAS ) {
|
|
|
|
drawer.canvas.width = viewportSize.x;
|
|
|
|
drawer.canvas.height = viewportSize.y;
|
|
|
|
drawer.context.clearRect( 0, 0, viewportSize.x, viewportSize.y );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !drawer.wrapHorizontal &&
|
2012-02-01 00:59:09 +04:00
|
|
|
( viewportBR.x < 0 || viewportTL.x > 1 ) ) {
|
|
|
|
return;
|
|
|
|
} else if
|
2012-03-01 17:38:15 +04:00
|
|
|
( !drawer.wrapVertical &&
|
2012-02-01 00:59:09 +04:00
|
|
|
( viewportBR.y < 0 || viewportTL.y > drawer.normHeight ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !drawer.wrapHorizontal ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
viewportTL.x = Math.max( viewportTL.x, 0 );
|
|
|
|
viewportBR.x = Math.min( viewportBR.x, 1 );
|
|
|
|
}
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !drawer.wrapVertical ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
viewportTL.y = Math.max( viewportTL.y, 0 );
|
|
|
|
viewportBR.y = Math.min( viewportBR.y, drawer.normHeight );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
lowestLevel = Math.min( lowestLevel, highestLevel );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
for ( level = highestLevel; level >= lowestLevel; level-- ) {
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-23 16:18:28 +04:00
|
|
|
//Avoid calculations for draw if we have already drawn this
|
|
|
|
renderPixelRatioC = drawer.viewport.deltaPixelsFromPoints(
|
|
|
|
drawer.source.getPixelRatio( level ),
|
|
|
|
true
|
|
|
|
).x;
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( ( !haveDrawn && renderPixelRatioC >= drawer.minPixelRatio ) ||
|
2012-02-23 16:18:28 +04:00
|
|
|
( level == lowestLevel ) ) {
|
|
|
|
drawLevel = true;
|
|
|
|
haveDrawn = true;
|
|
|
|
} else if ( !haveDrawn ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPixelRatioT = drawer.viewport.deltaPixelsFromPoints(
|
|
|
|
drawer.source.getPixelRatio( level ),
|
|
|
|
false
|
|
|
|
).x;
|
|
|
|
|
|
|
|
zeroRatioT = drawer.viewport.deltaPixelsFromPoints(
|
|
|
|
drawer.source.getPixelRatio( 0 ),
|
|
|
|
false
|
|
|
|
).x;
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
optimalRatio = drawer.immediateRender ?
|
2012-02-23 16:18:28 +04:00
|
|
|
1 :
|
|
|
|
zeroRatioT;
|
|
|
|
|
|
|
|
levelOpacity = Math.min( 1, ( renderPixelRatioC - 0.5 ) / 0.5 );
|
|
|
|
|
|
|
|
levelVisibility = optimalRatio / Math.abs(
|
|
|
|
optimalRatio - renderPixelRatioT
|
|
|
|
);
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
best = updateLevel(
|
|
|
|
drawer,
|
2012-02-23 16:18:28 +04:00
|
|
|
haveDrawn,
|
2012-02-01 00:59:09 +04:00
|
|
|
level,
|
2012-02-23 16:18:28 +04:00
|
|
|
levelOpacity,
|
|
|
|
levelVisibility,
|
2012-02-01 00:59:09 +04:00
|
|
|
viewportTL,
|
|
|
|
viewportBR,
|
|
|
|
currentTime,
|
|
|
|
best
|
|
|
|
);
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
if ( providesCoverage( drawer.coverage, level ) ) {
|
|
|
|
break;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
drawTiles( drawer, drawer.lastDrawn );
|
|
|
|
drawOverlays( drawer.viewport, drawer.overlays, drawer.container );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO
|
|
|
|
if ( best ) {
|
|
|
|
loadTile( drawer, best, currentTime );
|
|
|
|
// because we haven't finished drawing, so
|
|
|
|
drawer.updateAgain = true;
|
|
|
|
}
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-02-23 16:18:28 +04:00
|
|
|
function updateLevel( drawer, haveDrawn, level, levelOpacity, levelVisibility, viewportTL, viewportBR, currentTime, best ){
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
var x, y,
|
|
|
|
tileTL,
|
|
|
|
tileBR,
|
|
|
|
numberOfTiles,
|
2012-02-23 16:18:28 +04:00
|
|
|
viewportCenter = drawer.viewport.pixelFromPoint( drawer.viewport.getCenter() );
|
2012-02-01 00:59:09 +04:00
|
|
|
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//OK, a new drawing so do your calculations
|
|
|
|
tileTL = drawer.source.getTileAtPoint( level, viewportTL );
|
|
|
|
tileBR = drawer.source.getTileAtPoint( level, viewportBR );
|
|
|
|
numberOfTiles = drawer.source.getNumTiles( level );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
resetCoverage( drawer.coverage, level );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !drawer.wrapHorizontal ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
tileBR.x = Math.min( tileBR.x, numberOfTiles.x - 1 );
|
|
|
|
}
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( !drawer.wrapVertical ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
tileBR.y = Math.min( tileBR.y, numberOfTiles.y - 1 );
|
|
|
|
}
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
for ( x = tileTL.x; x <= tileBR.x; x++ ) {
|
|
|
|
for ( y = tileTL.y; y <= tileBR.y; y++ ) {
|
|
|
|
|
|
|
|
best = updateTile(
|
|
|
|
drawer,
|
|
|
|
drawLevel,
|
|
|
|
haveDrawn,
|
|
|
|
x, y,
|
|
|
|
level,
|
|
|
|
levelOpacity,
|
|
|
|
levelVisibility,
|
|
|
|
viewportCenter,
|
|
|
|
numberOfTiles,
|
|
|
|
currentTime,
|
|
|
|
best
|
2012-01-24 07:48:45 +04:00
|
|
|
);
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-01-19 06:52:22 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
}
|
|
|
|
return best;
|
|
|
|
};
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function updateTile( drawer, drawLevel, haveDrawn, x, y, level, levelOpacity, levelVisibility, viewportCenter, numberOfTiles, currentTime, best){
|
|
|
|
|
|
|
|
var tile = getTile(
|
|
|
|
x, y,
|
|
|
|
level,
|
|
|
|
drawer.source,
|
|
|
|
drawer.tilesMatrix,
|
|
|
|
currentTime,
|
|
|
|
numberOfTiles,
|
|
|
|
drawer.normHeight
|
|
|
|
),
|
2012-03-20 03:03:58 +04:00
|
|
|
drawTile = drawLevel,
|
|
|
|
newbest;
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
setCoverage( drawer.coverage, level, x, y, false );
|
|
|
|
|
|
|
|
if ( !tile.exists ) {
|
|
|
|
return best;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( haveDrawn && !drawTile ) {
|
|
|
|
if ( isCovered( drawer.coverage, level, x, y ) ) {
|
|
|
|
setCoverage( drawer.coverage, level, x, y, true );
|
|
|
|
} else {
|
|
|
|
drawTile = true;
|
2012-01-19 06:52:22 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !drawTile ) {
|
|
|
|
return best;
|
|
|
|
}
|
2012-01-19 06:52:22 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
positionTile(
|
|
|
|
tile,
|
|
|
|
drawer.source.tileOverlap,
|
|
|
|
drawer.viewport,
|
|
|
|
viewportCenter,
|
|
|
|
levelVisibility
|
|
|
|
);
|
2012-01-19 06:52:22 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( tile.loaded ) {
|
|
|
|
|
|
|
|
drawer.updateAgain = blendTile(
|
|
|
|
drawer,
|
|
|
|
tile,
|
|
|
|
x, y,
|
|
|
|
level,
|
|
|
|
levelOpacity,
|
|
|
|
currentTime
|
2012-01-19 06:52:22 +04:00
|
|
|
);
|
2012-03-20 11:38:27 +04:00
|
|
|
} else if ( tile.loading ) {
|
|
|
|
// the tile is already in the download queue
|
2012-03-20 23:00:25 +04:00
|
|
|
// thanks josh1093 for finally translating this typo
|
2012-02-01 00:59:09 +04:00
|
|
|
} else {
|
|
|
|
best = compareTiles( best, tile );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return best;
|
|
|
|
};
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function getTile( x, y, level, tileSource, tilesMatrix, time, numTiles, normHeight ) {
|
|
|
|
var xMod,
|
|
|
|
yMod,
|
|
|
|
bounds,
|
|
|
|
exists,
|
|
|
|
url,
|
|
|
|
tile;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !tilesMatrix[ level ] ) {
|
|
|
|
tilesMatrix[ level ] = {};
|
|
|
|
}
|
|
|
|
if ( !tilesMatrix[ level ][ x ] ) {
|
|
|
|
tilesMatrix[ level ][ x ] = {};
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !tilesMatrix[ level ][ x ][ y ] ) {
|
|
|
|
xMod = ( numTiles.x + ( x % numTiles.x ) ) % numTiles.x;
|
|
|
|
yMod = ( numTiles.y + ( y % numTiles.y ) ) % numTiles.y;
|
|
|
|
bounds = tileSource.getTileBounds( level, xMod, yMod );
|
|
|
|
exists = tileSource.tileExists( level, xMod, yMod );
|
|
|
|
url = tileSource.getTileUrl( level, xMod, yMod );
|
|
|
|
|
|
|
|
bounds.x += 1.0 * ( x - xMod ) / numTiles.x;
|
|
|
|
bounds.y += normHeight * ( y - yMod ) / numTiles.y;
|
|
|
|
|
|
|
|
tilesMatrix[ level ][ x ][ y ] = new $.Tile(
|
|
|
|
level,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
bounds,
|
|
|
|
exists,
|
|
|
|
url
|
|
|
|
);
|
|
|
|
}
|
2012-01-19 06:52:22 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
tile = tilesMatrix[ level ][ x ][ y ];
|
|
|
|
tile.lastTouchTime = time;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return tile;
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function loadTile( drawer, tile, time ) {
|
|
|
|
tile.loading = drawer.loadImage(
|
|
|
|
tile.url,
|
|
|
|
function( image ){
|
|
|
|
onTileLoad( drawer, tile, time, image );
|
2012-01-19 05:15:54 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
);
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function onTileLoad( drawer, tile, time, image ) {
|
|
|
|
var insertionIndex,
|
|
|
|
cutoff,
|
|
|
|
worstTile,
|
|
|
|
worstTime,
|
|
|
|
worstLevel,
|
|
|
|
worstTileIndex,
|
|
|
|
prevTile,
|
|
|
|
prevTime,
|
|
|
|
prevLevel,
|
|
|
|
i;
|
|
|
|
|
|
|
|
tile.loading = false;
|
|
|
|
|
|
|
|
if ( drawer.midUpdate ) {
|
|
|
|
$.console.warn( "Tile load callback in middle of drawing routine." );
|
|
|
|
return;
|
|
|
|
} else if ( !image ) {
|
|
|
|
$.console.log( "Tile %s failed to load: %s", tile, tile.url );
|
|
|
|
tile.exists = false;
|
|
|
|
return;
|
|
|
|
} else if ( time < drawer.lastResetTime ) {
|
|
|
|
$.console.log( "Ignoring tile %s loaded before reset: %s", tile, tile.url );
|
|
|
|
return;
|
|
|
|
}
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
tile.loaded = true;
|
|
|
|
tile.image = image;
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
insertionIndex = drawer.tilesLoaded.length;
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( drawer.tilesLoaded.length >= drawer.maxImageCacheCount ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
cutoff = Math.ceil( Math.log( drawer.source.tileSize ) / Math.log( 2 ) );
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
worstTile = null;
|
|
|
|
worstTileIndex = -1;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
for ( i = drawer.tilesLoaded.length - 1; i >= 0; i-- ) {
|
|
|
|
prevTile = drawer.tilesLoaded[ i ];
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( prevTile.level <= drawer.cutoff || prevTile.beingDrawn ) {
|
|
|
|
continue;
|
|
|
|
} else if ( !worstTile ) {
|
|
|
|
worstTile = prevTile;
|
|
|
|
worstTileIndex = i;
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
prevTime = prevTile.lastTouchTime;
|
|
|
|
worstTime = worstTile.lastTouchTime;
|
|
|
|
prevLevel = prevTile.level;
|
|
|
|
worstLevel = worstTile.level;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( prevTime < worstTime ||
|
|
|
|
( prevTime == worstTime && prevLevel > worstLevel ) ) {
|
|
|
|
worstTile = prevTile;
|
|
|
|
worstTileIndex = i;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( worstTile && worstTileIndex >= 0 ) {
|
|
|
|
worstTile.unload();
|
|
|
|
insertionIndex = worstTileIndex;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
drawer.tilesLoaded[ insertionIndex ] = tile;
|
|
|
|
drawer.updateAgain = true;
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function positionTile( tile, overlap, viewport, viewportCenter, levelVisibility ){
|
|
|
|
var boundsTL = tile.bounds.getTopLeft(),
|
|
|
|
boundsSize = tile.bounds.getSize(),
|
|
|
|
positionC = viewport.pixelFromPoint( boundsTL, true ),
|
|
|
|
positionT = viewport.pixelFromPoint( boundsTL, false ),
|
|
|
|
sizeC = viewport.deltaPixelsFromPoints( boundsSize, true ),
|
|
|
|
sizeT = viewport.deltaPixelsFromPoints( boundsSize, false ),
|
|
|
|
tileCenter = positionT.plus( sizeT.divide( 2 ) ),
|
|
|
|
tileDistance = viewportCenter.distanceTo( tileCenter );
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !overlap ) {
|
|
|
|
sizeC = sizeC.plus( new $.Point( 1, 1 ) );
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
tile.position = positionC;
|
|
|
|
tile.size = sizeC;
|
|
|
|
tile.distance = tileDistance;
|
|
|
|
tile.visibility = levelVisibility;
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function blendTile( drawer, tile, x, y, level, levelOpacity, currentTime ){
|
2012-03-01 17:38:15 +04:00
|
|
|
var blendTimeMillis = 1000 * drawer.blendTime,
|
2012-02-01 00:59:09 +04:00
|
|
|
deltaTime,
|
|
|
|
opacity;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !tile.blendStart ) {
|
|
|
|
tile.blendStart = currentTime;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
deltaTime = currentTime - tile.blendStart;
|
|
|
|
opacity = Math.min( 1, deltaTime / blendTimeMillis );
|
|
|
|
|
2012-03-01 17:38:15 +04:00
|
|
|
if ( drawer.alwaysBlend ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
opacity *= levelOpacity;
|
|
|
|
}
|
2012-01-19 05:15:54 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
tile.opacity = opacity;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
drawer.lastDrawn.push( tile );
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( opacity == 1 ) {
|
|
|
|
setCoverage( drawer.coverage, level, x, y, true );
|
|
|
|
} else if ( deltaTime < blendTimeMillis ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return false;
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
function clearTiles( drawer ) {
|
|
|
|
drawer.tilesMatrix = {};
|
|
|
|
drawer.tilesLoaded = [];
|
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Returns true if the given tile provides coverage to lower-level tiles of
|
|
|
|
* lower resolution representing the same content. If neither x nor y is
|
|
|
|
* given, returns true if the entire visible level provides coverage.
|
|
|
|
*
|
|
|
|
* Note that out-of-bounds tiles provide coverage in this sense, since
|
|
|
|
* there's no content that they would need to cover. Tiles at non-existent
|
|
|
|
* levels that are within the image bounds, however, do not.
|
|
|
|
*/
|
|
|
|
function providesCoverage( coverage, level, x, y ) {
|
|
|
|
var rows,
|
|
|
|
cols,
|
|
|
|
i, j;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !coverage[ level ] ) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( x === undefined || y === undefined ) {
|
|
|
|
rows = coverage[ level ];
|
|
|
|
for ( i in rows ) {
|
|
|
|
if ( rows.hasOwnProperty( i ) ) {
|
|
|
|
cols = rows[ i ];
|
|
|
|
for ( j in cols ) {
|
|
|
|
if ( cols.hasOwnProperty( j ) && !cols[ j ] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return (
|
|
|
|
coverage[ level ][ x] === undefined ||
|
|
|
|
coverage[ level ][ x ][ y ] === undefined ||
|
|
|
|
coverage[ level ][ x ][ y ] === true
|
|
|
|
);
|
|
|
|
};
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Returns true if the given tile is completely covered by higher-level
|
|
|
|
* tiles of higher resolution representing the same content. If neither x
|
|
|
|
* nor y is given, returns true if the entire visible level is covered.
|
|
|
|
*/
|
|
|
|
function isCovered( coverage, level, x, y ) {
|
|
|
|
if ( x === undefined || y === undefined ) {
|
|
|
|
return providesCoverage( coverage, level + 1 );
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
providesCoverage( coverage, level + 1, 2 * x, 2 * y ) &&
|
|
|
|
providesCoverage( coverage, level + 1, 2 * x, 2 * y + 1 ) &&
|
|
|
|
providesCoverage( coverage, level + 1, 2 * x + 1, 2 * y ) &&
|
|
|
|
providesCoverage( coverage, level + 1, 2 * x + 1, 2 * y + 1 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Sets whether the given tile provides coverage or not.
|
|
|
|
*/
|
|
|
|
function setCoverage( coverage, level, x, y, covers ) {
|
|
|
|
if ( !coverage[ level ] ) {
|
|
|
|
$.console.warn(
|
|
|
|
"Setting coverage for a tile before its level's coverage has been reset: %s",
|
|
|
|
level
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
if ( !coverage[ level ][ x ] ) {
|
|
|
|
coverage[ level ][ x ] = {};
|
|
|
|
}
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
coverage[ level ][ x ][ y ] = covers;
|
|
|
|
};
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Resets coverage information for the given level. This should be called
|
|
|
|
* after every draw routine. Note that at the beginning of the next draw
|
|
|
|
* routine, coverage for every visible tile should be explicitly set.
|
|
|
|
*/
|
|
|
|
function resetCoverage( coverage, level ) {
|
|
|
|
coverage[ level ] = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Determines the 'z-index' of the given overlay. Overlays are ordered in
|
|
|
|
* a z-index based on the order they are added to the Drawer.
|
|
|
|
*/
|
|
|
|
function getOverlayIndex( overlays, element ) {
|
|
|
|
var i;
|
|
|
|
for ( i = overlays.length - 1; i >= 0; i-- ) {
|
2012-02-02 01:56:04 +04:00
|
|
|
if ( overlays[ i ].element == element ) {
|
2012-02-01 00:59:09 +04:00
|
|
|
return i;
|
2011-12-28 03:17:24 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
}
|
2011-12-28 03:17:24 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @inner
|
|
|
|
* Determines whether the 'last best' tile for the area is better than the
|
|
|
|
* tile in question.
|
|
|
|
*/
|
|
|
|
function compareTiles( previousBest, tile ) {
|
|
|
|
if ( !previousBest ) {
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( tile.visibility > previousBest.visibility ) {
|
|
|
|
return tile;
|
|
|
|
} else if ( tile.visibility == previousBest.visibility ) {
|
|
|
|
if ( tile.distance < previousBest.distance ) {
|
|
|
|
return tile;
|
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
return previousBest;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2011-12-28 03:17:24 +04:00
|
|
|
function finishLoadingImage( image, callback, successful, jobid ){
|
|
|
|
|
|
|
|
image.onload = null;
|
|
|
|
image.onabort = null;
|
|
|
|
image.onerror = null;
|
|
|
|
|
|
|
|
if ( jobid ) {
|
|
|
|
window.clearTimeout( jobid );
|
|
|
|
}
|
|
|
|
window.setTimeout( function() {
|
|
|
|
callback( image.src, successful ? image : null);
|
|
|
|
}, 1 );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
|
|
|
|
function drawOverlays( viewport, overlays, container ){
|
|
|
|
var i,
|
|
|
|
length = overlays.length;
|
|
|
|
for ( i = 0; i < length; i++ ) {
|
|
|
|
drawOverlay( viewport, overlays[ i ], container );
|
2012-01-12 03:32:17 +04:00
|
|
|
}
|
2012-02-01 00:59:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
function drawOverlay( viewport, overlay, container ){
|
|
|
|
|
|
|
|
overlay.position = viewport.pixelFromPoint(
|
|
|
|
overlay.bounds.getTopLeft(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
overlay.size = viewport.deltaPixelsFromPoints(
|
|
|
|
overlay.bounds.getSize(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
overlay.drawHTML( container );
|
|
|
|
};
|
|
|
|
|
|
|
|
function drawTiles( drawer, lastDrawn ){
|
|
|
|
var i,
|
|
|
|
tile;
|
|
|
|
|
|
|
|
for ( i = lastDrawn.length - 1; i >= 0; i-- ) {
|
|
|
|
tile = lastDrawn[ i ];
|
2012-01-12 03:32:17 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
//TODO: get rid of this if by determining the tile draw method once up
|
|
|
|
// front and defining the appropriate 'draw' function
|
|
|
|
if ( USE_CANVAS ) {
|
|
|
|
tile.drawCanvas( drawer.context );
|
|
|
|
} else {
|
|
|
|
tile.drawHTML( drawer.canvas );
|
|
|
|
}
|
|
|
|
|
|
|
|
tile.beingDrawn = true;
|
|
|
|
}
|
2012-01-12 03:32:17 +04:00
|
|
|
};
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|