Move tile caching code inside tilecache.js.

This commit is contained in:
Antoine Vandecreme 2015-05-29 11:49:58 -04:00
parent 300cf6e777
commit 2538f2023c
3 changed files with 30 additions and 27 deletions

View File

@ -190,7 +190,14 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{
* @param {Element} container * @param {Element} container
*/ */
drawHTML: function( container ) { drawHTML: function( container ) {
if ( !this.loaded || !this.image ) { if (!this.cacheImageRecord) {
$.console.warn(
'[Tile.drawHTML] attempting to draw tile %s when it\'s not cached',
this.toString());
return;
}
if ( !this.loaded ) {
$.console.warn( $.console.warn(
"Attempting to draw tile %s when it's not yet loaded.", "Attempting to draw tile %s when it's not yet loaded.",
this.toString() this.toString()
@ -239,17 +246,18 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{
var position = this.position, var position = this.position,
size = this.size, size = this.size,
rendered, rendered;
canvas;
if (!this.cacheImageRecord) { if (!this.cacheImageRecord) {
$.console.warn('[Tile.drawCanvas] attempting to draw tile %s when it\'s not cached', this.toString()); $.console.warn(
'[Tile.drawCanvas] attempting to draw tile %s when it\'s not cached',
this.toString());
return; return;
} }
rendered = this.cacheImageRecord.getRenderedContext(); rendered = this.cacheImageRecord.getRenderedContext();
if ( !this.loaded || !( this.image || rendered) ){ if ( !this.loaded || !rendered ){
$.console.warn( $.console.warn(
"Attempting to draw tile %s when it's not yet loaded.", "Attempting to draw tile %s when it's not yet loaded.",
this.toString() this.toString()
@ -276,19 +284,8 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{
} }
if(!rendered){ // This gives the application a chance to make image manipulation
canvas = document.createElement( 'canvas' ); // changes as we are rendering the image
canvas.width = this.image.width;
canvas.height = this.image.height;
rendered = canvas.getContext('2d');
rendered.drawImage( this.image, 0, 0 );
this.cacheImageRecord.setRenderedContext(rendered);
//since we are caching the prerendered image on a canvas
//allow the image to not be held in memory
this.image = null;
}
// This gives the application a chance to make image manipulation changes as we are rendering the image
drawingHandler({context: context, tile: this, rendered: rendered}); drawingHandler({context: context, tile: this, rendered: rendered});
context.drawImage( context.drawImage(
@ -318,7 +315,6 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{
this.element = null; this.element = null;
this.imgElement = null; this.imgElement = null;
this.image = null;
this.loaded = false; this.loaded = false;
this.loading = false; this.loading = false;
} }

View File

@ -63,13 +63,19 @@ ImageRecord.prototype = {
}, },
getRenderedContext: function() { getRenderedContext: function() {
if (!this._renderedContext) {
var canvas = document.createElement( 'canvas' );
canvas.width = this._image.width;
canvas.height = this._image.height;
this._renderedContext = canvas.getContext('2d');
this._renderedContext.drawImage( this._image, 0, 0 );
//since we are caching the prerendered image on a canvas
//allow the image to not be held in memory
this._image = null;
}
return this._renderedContext; return this._renderedContext;
}, },
setRenderedContext: function(renderedContext) {
this._renderedContext = renderedContext;
},
addTile: function(tile) { addTile: function(tile) {
$.console.assert(tile, '[ImageRecord.addTile] tile is required'); $.console.assert(tile, '[ImageRecord.addTile] tile is required');
this._tiles.push(tile); this._tiles.push(tile);
@ -135,7 +141,7 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{
$.console.assert( options, "[TileCache.cacheTile] options is required" ); $.console.assert( options, "[TileCache.cacheTile] options is required" );
$.console.assert( options.tile, "[TileCache.cacheTile] options.tile is required" ); $.console.assert( options.tile, "[TileCache.cacheTile] options.tile is required" );
$.console.assert( options.tile.url, "[TileCache.cacheTile] options.tile.url is required" ); $.console.assert( options.tile.url, "[TileCache.cacheTile] options.tile.url is required" );
$.console.assert( options.tile.image, "[TileCache.cacheTile] options.tile.image is required" ); $.console.assert( options.image, "[TileCache.cacheTile] options.image is required" );
$.console.assert( options.tiledImage, "[TileCache.cacheTile] options.tiledImage is required" ); $.console.assert( options.tiledImage, "[TileCache.cacheTile] options.tiledImage is required" );
var cutoff = options.cutoff || 0; var cutoff = options.cutoff || 0;
@ -144,7 +150,7 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{
var imageRecord = this._imagesLoaded[options.tile.url]; var imageRecord = this._imagesLoaded[options.tile.url];
if (!imageRecord) { if (!imageRecord) {
imageRecord = this._imagesLoaded[options.tile.url] = new ImageRecord({ imageRecord = this._imagesLoaded[options.tile.url] = new ImageRecord({
image: options.tile.image image: options.image
}); });
this._imagesLoadedCount++; this._imagesLoadedCount++;

View File

@ -967,10 +967,11 @@ function onTileLoad( tiledImage, tile, time, image ) {
var finish = function() { var finish = function() {
tile.loading = false; tile.loading = false;
tile.loaded = true; tile.loaded = true;
tile.image = image;
var cutoff = Math.ceil( Math.log( tiledImage.source.getTileSize(tile.level) ) / Math.log( 2 ) ); var cutoff = Math.ceil( Math.log(
tiledImage.source.getTileSize(tile.level) ) / Math.log( 2 ) );
tiledImage._tileCache.cacheTile({ tiledImage._tileCache.cacheTile({
image: image,
tile: tile, tile: tile,
cutoff: cutoff, cutoff: cutoff,
tiledImage: tiledImage tiledImage: tiledImage