Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created July 3, 2023 12:23
Show Gist options
  • Save leaysgur/cd8d6799e2b0586939125206085d8c57 to your computer and use it in GitHub Desktop.
Save leaysgur/cd8d6799e2b0586939125206085d8c57 to your computer and use it in GitHub Desktop.
Extend OpenSeadragon internals to support async `getTileUrl()` with custom tile source.
$ = 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;
}
};
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