Modify the TileSource constructor and _memoizeLevelScale

This commit is contained in:
Sanghoon Kim 2021-11-30 11:40:06 +09:00
parent de39597c14
commit f8c97143e8

View File

@ -219,13 +219,12 @@ $.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.setMaxLevel( ( undefined !== options.maxLevel && null !== options.maxLevel ) ? this.maxLevel = ( 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 );
@ -279,12 +278,10 @@ $.TileSource.prototype = {
/** /**
* @function * @function
* @param {Number} level * @param {Number} level
* @return {OpenSeadragon.TileSource} Chainable.
*/ */
setMaxLevel: function( level ) { setMaxLevel: function( level ) {
this.maxLevel = level; this.maxLevel = level;
this._memoizeLevelScale( level ); this._memoizeLevelScale();
return this;
}, },
/** /**
@ -292,19 +289,21 @@ $.TileSource.prototype = {
* @param {Number} level * @param {Number} level
*/ */
getLevelScale: function( level ) { getLevelScale: function( level ) {
this._memoizeLevelScale( this.maxLevel ); // if getLevelScale is not memoized, we generate the memoized version
// at the first call and return the result
this._memoizeLevelScale();
return this.getLevelScale( level ); return this.getLevelScale( level );
}, },
// private // private
_memoizeLevelScale: function( level ) { _memoizeLevelScale: function() {
// 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 <= level; i++ ){ for( i = 0; i <= this.maxlevel; i++ ){
levelScaleCache[ i ] = 1 / Math.pow(2, level - i); levelScaleCache[ i ] = 1 / Math.pow(2, this.maxlevel - i);
} }
this.getLevelScale = function( _level ){ this.getLevelScale = function( _level ){
return levelScaleCache[ _level ]; return levelScaleCache[ _level ];