2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - Tile
|
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-03-06 14:51:31 +04:00
|
|
|
var TILE_CACHE = {};
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2013-11-16 10:19:53 +04:00
|
|
|
* @class Tile
|
|
|
|
* @memberof OpenSeadragon
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {Number} level The zoom level this tile belongs to.
|
|
|
|
* @param {Number} x The vector component 'x'.
|
|
|
|
* @param {Number} y The vector component 'y'.
|
2013-06-19 21:33:25 +04:00
|
|
|
* @param {OpenSeadragon.Point} bounds Where this tile fits, in normalized
|
2012-03-01 17:38:15 +04:00
|
|
|
* coordinates.
|
2013-06-19 21:33:25 +04:00
|
|
|
* @param {Boolean} exists Is this tile a part of a sparse image? ( Also has
|
2012-03-01 17:38:15 +04:00
|
|
|
* this tile failed to load? )
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {String} url The URL of this tile's image.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
$.Tile = function(level, x, y, bounds, exists, url) {
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The zoom level this tile belongs to.
|
|
|
|
* @member {Number} level
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-01-12 03:22:13 +04:00
|
|
|
this.level = level;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'x'.
|
|
|
|
* @member {Number} x
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-01-12 03:22:13 +04:00
|
|
|
this.x = x;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'y'.
|
|
|
|
* @member {Number} y
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-01-12 03:22:13 +04:00
|
|
|
this.y = y;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Where this tile fits, in normalized coordinates
|
|
|
|
* @member {OpenSeadragon.Point} bounds
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.bounds = bounds;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Is this tile a part of a sparse image? Also has this tile failed to load?
|
|
|
|
* @member {Boolean} exists
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.exists = exists;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The URL of this tile's image.
|
|
|
|
* @member {String} url
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.url = url;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Is this tile loaded?
|
|
|
|
* @member {Boolean} loaded
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.loaded = false;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Is this tile loading?
|
|
|
|
* @member {Boolean} loading
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.loading = false;
|
|
|
|
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The HTML div element for this tile
|
|
|
|
* @member {Element} element
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-02 01:56:04 +04:00
|
|
|
this.element = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The HTML img element for this tile.
|
|
|
|
* @member {Element} imgElement
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2013-10-28 23:36:29 +04:00
|
|
|
this.imgElement = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The Image object for this tile.
|
|
|
|
* @member {Object} image
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-04-03 11:08:27 +04:00
|
|
|
this.image = null;
|
2012-02-01 00:59:09 +04:00
|
|
|
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The alias of this.element.style.
|
|
|
|
* @member {String} style
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.style = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* This tile's position on screen, in pixels.
|
|
|
|
* @member {OpenSeadragon.Point} position
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.position = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* This tile's size on screen, in pixels.
|
|
|
|
* @member {OpenSeadragon.Point} size
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.size = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The start time of this tile's blending.
|
|
|
|
* @member {Number} blendStart
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.blendStart = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The current opacity this tile should be.
|
|
|
|
* @member {Number} opacity
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.opacity = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The distance of this tile to the viewport center.
|
|
|
|
* @member {Number} distance
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.distance = null;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The visibility score of this tile.
|
|
|
|
* @member {Number} visibility
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.visibility = null;
|
|
|
|
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Whether this tile is currently being drawn.
|
|
|
|
* @member {Boolean} beingDrawn
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.beingDrawn = false;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* Timestamp the tile was last touched.
|
|
|
|
* @member {Number} lastTouchTime
|
|
|
|
* @memberof OpenSeadragon.Tile#
|
|
|
|
*/
|
2012-02-01 00:59:09 +04:00
|
|
|
this.lastTouchTime = 0;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2013-11-16 10:19:53 +04:00
|
|
|
$.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2013-06-19 21:33:25 +04:00
|
|
|
* Provides a string representation of this tiles level and (x,y)
|
2012-02-01 00:59:09 +04:00
|
|
|
* components.
|
|
|
|
* @function
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
toString: function() {
|
|
|
|
return this.level + "/" + this.x + "_" + this.y;
|
|
|
|
},
|
2012-01-12 03:22:13 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Renders the tile in an html container.
|
|
|
|
* @function
|
|
|
|
* @param {Element} container
|
|
|
|
*/
|
2012-01-12 03:22:13 +04:00
|
|
|
drawHTML: function( container ) {
|
2012-04-03 11:08:27 +04:00
|
|
|
if ( !this.loaded || !this.image ) {
|
2012-01-25 23:14:02 +04:00
|
|
|
$.console.warn(
|
2012-01-24 07:48:45 +04:00
|
|
|
"Attempting to draw tile %s when it's not yet loaded.",
|
|
|
|
this.toString()
|
2012-01-12 03:22:13 +04:00
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-31 05:23:45 +04:00
|
|
|
//EXPERIMENTAL - trying to figure out how to scale the container
|
|
|
|
// content during animation of the container size.
|
2013-06-03 22:09:42 +04:00
|
|
|
|
2013-01-31 05:23:45 +04:00
|
|
|
if ( !this.element ) {
|
2013-10-28 23:36:29 +04:00
|
|
|
this.element = $.makeNeutralElement( "div" );
|
|
|
|
this.imgElement = $.makeNeutralElement( "img" );
|
|
|
|
this.imgElement.src = this.url;
|
|
|
|
this.imgElement.style.msInterpolationMode = "nearest-neighbor";
|
|
|
|
this.imgElement.style.width = "100%";
|
|
|
|
this.imgElement.style.height = "100%";
|
2013-01-31 05:23:45 +04:00
|
|
|
|
|
|
|
this.style = this.element.style;
|
|
|
|
this.style.position = "absolute";
|
|
|
|
}
|
2013-01-31 21:30:13 +04:00
|
|
|
if ( this.element.parentNode != container ) {
|
|
|
|
container.appendChild( this.element );
|
|
|
|
}
|
2013-10-28 23:36:29 +04:00
|
|
|
if ( this.imgElement.parentNode != this.element ) {
|
|
|
|
this.element.appendChild( this.imgElement );
|
|
|
|
}
|
2013-01-31 21:30:13 +04:00
|
|
|
|
2013-06-03 22:09:42 +04:00
|
|
|
this.style.top = this.position.y + "px";
|
|
|
|
this.style.left = this.position.x + "px";
|
|
|
|
this.style.height = this.size.y + "px";
|
|
|
|
this.style.width = this.size.x + "px";
|
2013-01-31 05:23:45 +04:00
|
|
|
|
2013-06-03 22:09:42 +04:00
|
|
|
$.setElementOpacity( this.element, this.opacity );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-01-12 03:22:13 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Renders the tile in a canvas-based context.
|
|
|
|
* @function
|
|
|
|
* @param {Canvas} context
|
|
|
|
*/
|
|
|
|
drawCanvas: function( context ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
|
|
|
|
var position = this.position,
|
2013-03-01 17:14:35 +04:00
|
|
|
size = this.size,
|
|
|
|
rendered,
|
|
|
|
canvas;
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2013-03-05 16:30:37 +04:00
|
|
|
if ( !this.loaded || !( this.image || TILE_CACHE[ this.url ] ) ){
|
2012-01-25 23:14:02 +04:00
|
|
|
$.console.warn(
|
2012-01-24 07:48:45 +04:00
|
|
|
"Attempting to draw tile %s when it's not yet loaded.",
|
|
|
|
this.toString()
|
2012-01-12 03:22:13 +04:00
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.globalAlpha = this.opacity;
|
2013-01-31 01:51:37 +04:00
|
|
|
|
2013-03-01 17:14:35 +04:00
|
|
|
//context.save();
|
2013-01-31 05:23:45 +04:00
|
|
|
|
2013-03-01 17:14:35 +04:00
|
|
|
//if we are supposed to be rendering fully opaque rectangle,
|
2013-01-31 21:30:13 +04:00
|
|
|
//ie its done fading or fading is turned off, and if we are drawing
|
|
|
|
//an image with an alpha channel, then the only way
|
|
|
|
//to avoid seeing the tile underneath is to clear the rectangle
|
2013-03-05 16:30:37 +04:00
|
|
|
if( context.globalAlpha == 1 && this.url.match('.png') ){
|
2013-01-31 21:30:13 +04:00
|
|
|
//clearing only the inside of the rectangle occupied
|
|
|
|
//by the png prevents edge flikering
|
2013-06-19 21:33:25 +04:00
|
|
|
context.clearRect(
|
|
|
|
position.x+1,
|
|
|
|
position.y+1,
|
|
|
|
size.x-2,
|
|
|
|
size.y-2
|
2013-01-31 01:51:37 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-05 16:30:37 +04:00
|
|
|
if( !TILE_CACHE[ this.url ] ){
|
2013-03-01 17:14:35 +04:00
|
|
|
canvas = document.createElement( 'canvas' );
|
|
|
|
canvas.width = this.image.width;
|
|
|
|
canvas.height = this.image.height;
|
|
|
|
rendered = canvas.getContext('2d');
|
|
|
|
rendered.drawImage( this.image, 0, 0 );
|
2013-03-05 16:30:37 +04:00
|
|
|
TILE_CACHE[ this.url ] = rendered;
|
|
|
|
//since we are caching the prerendered image on a canvas
|
|
|
|
//allow the image to not be held in memory
|
|
|
|
this.image = null;
|
2013-03-01 17:14:35 +04:00
|
|
|
}
|
|
|
|
|
2013-03-05 16:30:37 +04:00
|
|
|
rendered = TILE_CACHE[ this.url ];
|
2013-06-19 21:33:25 +04:00
|
|
|
|
2013-03-01 17:14:35 +04:00
|
|
|
//rendered.save();
|
2013-06-19 21:33:25 +04:00
|
|
|
context.drawImage(
|
|
|
|
rendered.canvas,
|
2013-03-01 17:14:35 +04:00
|
|
|
0,
|
2013-06-19 21:33:25 +04:00
|
|
|
0,
|
|
|
|
rendered.canvas.width,
|
|
|
|
rendered.canvas.height,
|
|
|
|
position.x,
|
|
|
|
position.y,
|
|
|
|
size.x,
|
|
|
|
size.y
|
2013-03-01 17:14:35 +04:00
|
|
|
);
|
|
|
|
//rendered.restore();
|
|
|
|
|
|
|
|
//context.restore();
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-01-18 03:30:41 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2013-12-14 01:12:35 +04:00
|
|
|
* Removes tile from its container.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
unload: function() {
|
2013-10-28 23:36:29 +04:00
|
|
|
if ( this.imgElement && this.imgElement.parentNode ) {
|
|
|
|
this.imgElement.parentNode.removeChild( this.imgElement );
|
|
|
|
}
|
2012-02-02 01:56:04 +04:00
|
|
|
if ( this.element && this.element.parentNode ) {
|
|
|
|
this.element.parentNode.removeChild( this.element );
|
2013-06-19 21:33:25 +04:00
|
|
|
}
|
2013-03-05 16:30:37 +04:00
|
|
|
if ( TILE_CACHE[ this.url ]){
|
|
|
|
delete TILE_CACHE[ this.url ];
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:38:17 +04:00
|
|
|
this.element = null;
|
2013-10-28 23:36:29 +04:00
|
|
|
this.imgElement = null;
|
2013-10-28 23:38:17 +04:00
|
|
|
this.image = null;
|
|
|
|
this.loaded = false;
|
|
|
|
this.loading = false;
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}( OpenSeadragon ));
|