Add setMaxLevel to initialize getLevelScale memoization

This commit is contained in:
Sanghoon Kim 2021-11-23 16:58:00 +09:00
parent 91793ce752
commit de39597c14

View File

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