diff --git a/src/tilesource.js b/src/tilesource.js index aab7f134..78732626 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -219,13 +219,14 @@ $.TileSource = function( width, height, tileSize, tileOverlap, minLevel, maxLeve this.tileOverlap = options.tileOverlap ? options.tileOverlap : 0; this.minLevel = options.minLevel ? options.minLevel : 0; - this.maxLevel = ( undefined !== options.maxLevel && null !== options.maxLevel ) ? + this.setMaxLevel( ( undefined !== options.maxLevel && null !== options.maxLevel ) ? options.maxLevel : ( ( options.width && options.height ) ? Math.ceil( Math.log( Math.max( options.width, options.height ) ) / Math.log( 2 ) ) : 0 - ); + ) + ); if( this.success && $.isFunction( this.success ) ){ this.success( this ); } @@ -275,24 +276,39 @@ $.TileSource.prototype = { return this._tileHeight; }, + /** + * @function + * @param {Number} level + * @return {OpenSeadragon.TileSource} Chainable. + */ + setMaxLevel: function( level ) { + this.maxLevel = level; + this._memoizeLevelScale( level ); + return this; + }, + /** * @function * @param {Number} level */ getLevelScale: function( level ) { + this._memoizeLevelScale( this.maxLevel ); + return this.getLevelScale( level ); + }, + // private + _memoizeLevelScale: function( level ) { // see https://github.com/openseadragon/openseadragon/issues/22 // we use the tilesources implementation of getLevelScale to generate // a memoized re-implementation var levelScaleCache = {}, i; - for( i = 0; i <= this.maxLevel; i++ ){ - levelScaleCache[ i ] = 1 / Math.pow(2, this.maxLevel - i); + for( i = 0; i <= level; i++ ){ + levelScaleCache[ i ] = 1 / Math.pow(2, level - i); } this.getLevelScale = function( _level ){ return levelScaleCache[ _level ]; }; - return this.getLevelScale( level ); }, /**