Created
July 3, 2023 12:23
-
-
Save leaysgur/cd8d6799e2b0586939125206085d8c57 to your computer and use it in GitHub Desktop.
Extend OpenSeadragon internals to support async `getTileUrl()` with custom tile source.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ = window.OpenSeadragon; | |
$.TileSource = class extends $.TileSource { | |
downloadTileStart(context) { | |
// 👼 Keep original code as is | |
var dataStore = context.userData, | |
image = new Image(); | |
dataStore.image = image; | |
dataStore.request = null; | |
var finish = function(error) { | |
if (!image) { | |
context.finish( | |
null, | |
dataStore.request, | |
"Image load failed: undefined Image instance." | |
); | |
return; | |
} | |
image.onload = image.onerror = image.onabort = null; | |
context.finish(error ? null : image, dataStore.request, error); | |
}; | |
image.onload = function() { | |
finish(); | |
}; | |
image.onabort = image.onerror = function() { | |
finish("Image load aborted."); | |
}; | |
// 👻 Replace whole logic here | |
context.src | |
.then((url) => { | |
image.src = url; | |
}) | |
.catch((err) => finish(err.message)); | |
} | |
// 👻 Return fixed value | |
hasTransparency() { | |
return false; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new OpenSeadragon.Viewer({ | |
// ... | |
tileSources: { | |
height: 512 * 256, | |
width: 512 * 256, | |
tileSize: 256, | |
// Now it's async! | |
getTileUrl: async (level, x, y) => fetchImagePath(level, x, y), | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment