UNPKG

@allmaps/render

Version:

Render functions for WebGL and image buffers

85 lines (84 loc) 2.49 kB
import { doBboxesIntersect, bufferBboxByRatio } from "@allmaps/stdlib"; import { computeBboxTile } from "../shared/tiles.js"; class CacheableTile extends EventTarget { fetchableTile; fetchFn; abortController; data; cachedTilesFromSprites; /** * Creates an instance of CacheableTile. * * Note that there can be multiple FetchableTiles with the same tileUrl, but only one CachedTile per tileUrl. * * @constructor * @param fetchableTile - The FetchableTile which created this CachedTile. * @param fetchFn - Optional fetch function to use */ constructor(fetchableTile, fetchFn) { super(); this.fetchableTile = fetchableTile; this.fetchFn = fetchFn; this.abortController = new AbortController(); } /** * Whether a tile has fetched its data * * @returns */ isCachedTile() { return this.data !== void 0; } isTileFromSprites() { return this.fetchableTile.options?.spritesInfo != void 0; } getCachedTilesFromSprites() { return this.cachedTilesFromSprites; } /** * Abort the fetch */ abort() { if (!this.abortController.signal.aborted) { this.abortController.abort(); } } shouldPrune(mapPruneInfo, mapPruneConstants) { const tile = this.fetchableTile.tile; if (this.isTileFromSprites()) { return false; } if (!mapPruneInfo) { return true; } if (mapPruneInfo.overviewTileZoomLevelForViewport && tile.tileZoomLevel.scaleFactor == mapPruneInfo.overviewTileZoomLevelForViewport.scaleFactor) { return false; } if (mapPruneInfo.resourceViewportRingBboxForViewport === void 0 || mapPruneInfo.tileZoomLevelForViewport === void 0) { return true; } const log2ScaleFactorDiff = Math.log2(tile.tileZoomLevel.scaleFactor) - Math.log2(mapPruneInfo.tileZoomLevelForViewport.scaleFactor); const tileScaleFactorTooHigh = log2ScaleFactorDiff > mapPruneConstants.maxHigherLog2ScaleFactorDiff; if (tileScaleFactorTooHigh) { return true; } const tileScaleFactorTooLow = -log2ScaleFactorDiff > mapPruneConstants.maxLowerLog2ScaleFactorDiff; if (tileScaleFactorTooLow) { return true; } if (!doBboxesIntersect( bufferBboxByRatio( computeBboxTile(tile), Math.pow(2, log2ScaleFactorDiff) ), mapPruneInfo.resourceViewportRingBboxForViewport )) { return true; } return false; } } export { CacheableTile }; //# sourceMappingURL=CacheableTile.js.map