@allmaps/render
Version:
Render functions for WebGL and image buffers
51 lines (50 loc) • 1.57 kB
JavaScript
import { fetchUrl } from "@allmaps/stdlib";
import { CacheableTile } from "./CacheableTile.js";
import { WarpedMapEvent, WarpedMapEventType } from "../shared/events.js";
class CacheableImageDataTile extends CacheableTile {
/**
* Fetch the tile and create its ImageData object.
*
* @returns
*/
async fetch() {
try {
const response = await fetchUrl(
this.tileUrl,
{
signal: this.abortController.signal
},
this.fetchFn
);
const width = this.tile.tileZoomLevel.width;
const height = this.tile.tileZoomLevel.height;
const blob = await response.blob();
const bitmap = await createImageBitmap(blob);
const canvas = new OffscreenCanvas(width, height);
const context = canvas.getContext("2d");
if (!context) {
throw new Error("Could not create OffscreenCanvas context");
}
context.drawImage(bitmap, 0, 0);
this.data = context.getImageData(0, 0, width, height);
this.dispatchEvent(
new WarpedMapEvent(WarpedMapEventType.TILEFETCHED, this.tileUrl)
);
} catch (err) {
if (err instanceof Error && err.name === "AbortError") ;
else {
this.dispatchEvent(
new WarpedMapEvent(WarpedMapEventType.TILEFETCHERROR, this.tileUrl)
);
}
}
return this.data;
}
static createFactory() {
return (fetchableTile, fetchFn) => new CacheableImageDataTile(fetchableTile, fetchFn);
}
}
export {
CacheableImageDataTile
};
//# sourceMappingURL=CacheableImageDataTile.js.map