From 2538f2023cc8be3f433573c7e0058cae4b48974b Mon Sep 17 00:00:00 2001 From: Antoine Vandecreme Date: Fri, 29 May 2015 11:49:58 -0400 Subject: [PATCH] Move tile caching code inside tilecache.js. --- src/tile.js | 34 +++++++++++++++------------------- src/tilecache.js | 18 ++++++++++++------ src/tiledimage.js | 5 +++-- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/tile.js b/src/tile.js index 3fe642df..3ad71ad3 100644 --- a/src/tile.js +++ b/src/tile.js @@ -190,7 +190,14 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{ * @param {Element} 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( "Attempting to draw tile %s when it's not yet loaded.", this.toString() @@ -239,17 +246,18 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{ var position = this.position, size = this.size, - rendered, - canvas; + rendered; 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; } rendered = this.cacheImageRecord.getRenderedContext(); - if ( !this.loaded || !( this.image || rendered) ){ + if ( !this.loaded || !rendered ){ $.console.warn( "Attempting to draw tile %s when it's not yet loaded.", this.toString() @@ -276,19 +284,8 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{ } - if(!rendered){ - canvas = document.createElement( 'canvas' ); - 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 + // This gives the application a chance to make image manipulation + // changes as we are rendering the image drawingHandler({context: context, tile: this, rendered: rendered}); context.drawImage( @@ -318,7 +315,6 @@ $.Tile.prototype = /** @lends OpenSeadragon.Tile.prototype */{ this.element = null; this.imgElement = null; - this.image = null; this.loaded = false; this.loading = false; } diff --git a/src/tilecache.js b/src/tilecache.js index 45344a05..297d4081 100644 --- a/src/tilecache.js +++ b/src/tilecache.js @@ -63,13 +63,19 @@ ImageRecord.prototype = { }, 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; }, - setRenderedContext: function(renderedContext) { - this._renderedContext = renderedContext; - }, - addTile: function(tile) { $.console.assert(tile, '[ImageRecord.addTile] tile is required'); 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.tile, "[TileCache.cacheTile] options.tile 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" ); var cutoff = options.cutoff || 0; @@ -144,7 +150,7 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{ var imageRecord = this._imagesLoaded[options.tile.url]; if (!imageRecord) { imageRecord = this._imagesLoaded[options.tile.url] = new ImageRecord({ - image: options.tile.image + image: options.image }); this._imagesLoadedCount++; diff --git a/src/tiledimage.js b/src/tiledimage.js index faa49cc9..4fcbab9e 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -967,10 +967,11 @@ function onTileLoad( tiledImage, tile, time, image ) { var finish = function() { tile.loading = false; 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({ + image: image, tile: tile, cutoff: cutoff, tiledImage: tiledImage