refactor: cleanup code

This commit is contained in:
ronnymikalsen 2021-02-05 11:48:08 +01:00
parent c410b82e65
commit eb113662e8
2 changed files with 29 additions and 28 deletions

View File

@ -918,12 +918,11 @@ function OpenSeadragon( options ){
}; };
/** /**
* A ratio comparing the device screen's pixel density to the canvas's backing store pixel density, * @returns {Number} Return a ratio comparing the device screen's pixel
* clamped to a minimum of 1. Defaults to 1 if canvas isn't supported by the browser. * densityto the canvas's backing store pixel density, clamped to a
* @member {Number} pixelDensityRatio * minimum of 1. Defaults to 1 if canvas isn't supported by the browser.
* @memberof OpenSeadragon
*/ */
$.pixelDensityRatio = (function () { $.getCurrentPixelDensityRatio = function() {
if ( $.supportsCanvas ) { if ( $.supportsCanvas ) {
var context = document.createElement('canvas').getContext('2d'); var context = document.createElement('canvas').getContext('2d');
var devicePixelRatio = window.devicePixelRatio || 1; var devicePixelRatio = window.devicePixelRatio || 1;
@ -936,7 +935,13 @@ function OpenSeadragon( options ){
} else { } else {
return 1; return 1;
} }
}()); };
/**
* @member {Number} pixelDensityRatio
* @memberof OpenSeadragon
*/
$.pixelDensityRatio = $.getCurrentPixelDensityRatio();
}( OpenSeadragon )); }( OpenSeadragon ));

View File

@ -401,28 +401,6 @@ $.Viewer = function( options ) {
debugGridColor: this.debugGridColor debugGridColor: this.debugGridColor
}); });
function resize() {
console.log("Windows is resized...");
$.pixelDensityRatio = (function () {
if ( $.supportsCanvas ) {
var context = document.createElement('canvas').getContext('2d');
var devicePixelRatio = window.devicePixelRatio || 1;
var backingStoreRatio = context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return Math.max(devicePixelRatio, 1) / backingStoreRatio;
} else {
return 1;
}
}());
console.log("$.pixelDensityRatio", $.pixelDensityRatio);
_this.world.resetItems();
_this.forceRedraw();
}
$.addEvent( window, 'resize', resize );
// Overlay container // Overlay container
this.overlaysContainer = $.makeNeutralElement( "div" ); this.overlaysContainer = $.makeNeutralElement( "div" );
this.canvas.appendChild( this.overlaysContainer ); this.canvas.appendChild( this.overlaysContainer );
@ -442,6 +420,9 @@ $.Viewer = function( options ) {
} }
} }
// Add updatePixelDensityRatio to resize event
$.addEvent( window, 'resize', this.updatePixelDensityRatio.bind(this) );
//Instantiate a navigator if configured //Instantiate a navigator if configured
if ( this.showNavigator){ if ( this.showNavigator){
this.navigator = new $.Navigator({ this.navigator = new $.Navigator({
@ -1620,6 +1601,21 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype,
return this.world.removeItem(drawer); return this.world.removeItem(drawer);
}, },
/**
* Update pixel density ration, clears all tiles and triggers updates for
* all items.
*/
updatePixelDensityRatio: function() {
var previusPixelDensityRatio = $.pixelDensityRatio;
var currentPixelDensityRatio = $.getCurrentPixelDensityRatio();
if (previusPixelDensityRatio !== currentPixelDensityRatio) {
$.pixelDensityRatio = currentPixelDensityRatio;
this.world.resetItems();
this.forceRedraw();
}
},
/** /**
* Force the viewer to redraw its contents. * Force the viewer to redraw its contents.
* @returns {OpenSeadragon.Viewer} Chainable. * @returns {OpenSeadragon.Viewer} Chainable.