diff --git a/src/tile.js b/src/tile.js index 5b380087..71ccbb11 100644 --- a/src/tile.js +++ b/src/tile.js @@ -114,10 +114,7 @@ $.Tile.prototype = { var containerSize = $.getElementSize( container ); if ( !this.loaded || !this.image ) { - $.console.warn( - "Attempting to draw tile %s when it's not yet loaded.", - this.toString() - ); + $.console.warn("Attempting to draw tile " + this.toString() + " when it's not yet loaded."); return; } @@ -171,7 +168,7 @@ $.Tile.prototype = { * @function * @param {Canvas} context */ - drawCanvas: function( context ) { + drawCanvas: function (context, tileSource) { var position = this.position, size = this.size, @@ -179,10 +176,7 @@ $.Tile.prototype = { canvas; if ( !this.loaded || !( this.image || TILE_CACHE[ this.url ] ) ){ - $.console.warn( - "Attempting to draw tile %s when it's not yet loaded.", - this.toString() - ); + $.console.warn("Attempting to draw tile " + this.toString() + " when it's not yet loaded."); return; } context.globalAlpha = this.opacity; @@ -206,12 +200,21 @@ $.Tile.prototype = { } if( !TILE_CACHE[ this.url ] ){ - canvas = document.createElement( 'canvas' ); - canvas.width = this.image.width; - canvas.height = this.image.height; - rendered = canvas.getContext('2d'); - rendered.drawImage( this.image, 0, 0 ); + // Is this already a canvas context ? + if ( this.image && this.image.lineTo ) { + rendered = this.image; + } else { + canvas = document.createElement( 'canvas' ); + canvas.width = this.image.width; + canvas.height = this.image.height; + rendered = canvas.getContext( '2d' ); + rendered.drawImage( this.image, 0, 0 ); + } TILE_CACHE[ this.url ] = rendered; + // give the application the opportunity to cache this tile + if ( this.cacheTile ) { + this.cacheTile( TILE_CACHE, this.url, tileSource, this ); + } //since we are caching the prerendered image on a canvas //allow the image to not be held in memory this.image = null; @@ -226,10 +229,10 @@ $.Tile.prototype = { 0, rendered.canvas.width, rendered.canvas.height, - position.x, - position.y, - size.x, - size.y + this.position.x, + this.position.y, + this.size.x, + this.size.y ); //rendered.restore();