Merge pull request #2066 from kim-sanghoon/feature/setMaxLevel

Fix #2065 and add setMaxLevel for #2059
This commit is contained in:
Ian Gilman 2021-12-01 11:29:33 -08:00 committed by GitHub
commit d302daae48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View File

@ -275,28 +275,43 @@ $.TileSource.prototype = {
return this._tileHeight; return this._tileHeight;
}, },
/**
* Set the maxLevel to the given level, and perform the memoization of
* getLevelScale with the new maxLevel. This function can be useful if the
* memoization is required before the first call of getLevelScale, or both
* memoized getLevelScale and maxLevel should be changed accordingly.
* @function
* @param {Number} level
*/
setMaxLevel: function( level ) {
this.maxLevel = level;
this._memoizeLevelScale();
},
/** /**
* @function * @function
* @param {Number} level * @param {Number} level
*/ */
getLevelScale: function( level ) { getLevelScale: function( level ) {
// if getLevelScale is not memoized, we generate the memoized version
// at the first call and return the result
this._memoizeLevelScale();
return this.getLevelScale( level );
},
// private
_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 maxLevel = Math.ceil( var levelScaleCache = {},
Math.log( Math.max( this.dimensions.x, this.dimensions.y ) ) /
Math.log( 2 )
),
levelScaleCache = {},
i; i;
for( i = 0; i <= maxLevel; i++ ){ for( i = 0; i <= this.maxLevel; i++ ){
levelScaleCache[ i ] = 1 / Math.pow(2, maxLevel - i); levelScaleCache[ i ] = 1 / Math.pow(2, this.maxLevel - i);
} }
this.getLevelScale = function( _level ){ this.getLevelScale = function( _level ){
return levelScaleCache[ _level ]; return levelScaleCache[ _level ];
}; };
return this.getLevelScale( level );
}, },
/** /**

View File

@ -128,8 +128,6 @@
assert.equal(tileSource.maxLevel, 12, 'The initial max level should be 12.'); assert.equal(tileSource.maxLevel, 12, 'The initial max level should be 12.');
tileSource.maxLevel = 9;
function assertLevelScale(level, expected) { function assertLevelScale(level, expected) {
var actual = tileSource.getLevelScale(level); var actual = tileSource.getLevelScale(level);
assert.ok(Math.abs(actual - expected) < Number.EPSILON, "The scale at level " + level + assert.ok(Math.abs(actual - expected) < Number.EPSILON, "The scale at level " + level +
@ -141,6 +139,13 @@
assertLevelScale(10, 1 / 4); assertLevelScale(10, 1 / 4);
assertLevelScale(8, 1 / 16); assertLevelScale(8, 1 / 16);
assertLevelScale(6, 1 / 64); assertLevelScale(6, 1 / 64);
tileSource.setMaxLevel(9);
assertLevelScale(9, 1);
assertLevelScale(7, 1 / 4);
assertLevelScale(5, 1 / 16);
assertLevelScale(3, 1 / 64);
}); });
}()); }());