iwmlib/lib/pixi/deepzoom/tile.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
export const deepZoomTileCache = new Map()
/** The current Tile implementation simply uses PIXI.Sprites.
*
* BTW: PIXI.extras.TilingSprite is not appropriate. It should be used for
* repeating patterns.
**/
export class Tile extends PIXI.Sprite {
constructor(texture, url) {
super(texture)
this.url = url
this.register(url)
}
static fromImage(imageId, crossorigin, scaleMode) {
return new Tile(PIXI.Texture.fromImage(imageId, crossorigin, scaleMode), imageId)
}
/**
* Registers the tile in the global reference counter for textures
*
* @param {*} url
* @param {boolean} [debug=false]
* @memberof Tile
*/
register(url, debug = false) {
if (deepZoomTileCache.has(url)) {
let tiles = deepZoomTileCache.get(url)
tiles.add(this)
if (debug) console.log("Tile.register", url, tiles.size)
}
else {
deepZoomTileCache.set(url, new Set([this]))
if (debug) console.log("Tile.register", url, 1)
}
}
/**
* Unregisters the rile in the global reference counter for textures
*
* @returns {number} The number of how often a texture is used.
* @memberof Tile
*/
unregister() {
let tiles = deepZoomTileCache.get(this.url)
tiles.delete(this)
if (tiles.size == 0) {
deepZoomTileCache.delete(this.url)
}
return tiles.size
}
/**
* Destroys this sprite and optionally its texture and children
*
* @param {*} options Part of the PIXI API, but ignored in the implementation
* @memberof Tile
*/
destroy(options, debug = false) {
if (this.parent != null) {
}
let count = this.unregister()
if (count <= 0) {
let opts = { children: true, texture: true, baseTexture: true }
super.destroy(opts)
if (debug) console.log("Tile.destroy", deepZoomTileCache.size, opts)
}
else {
let opts = { children: true, texture: false, baseTexture: false }
if (debug) console.log("Tile.destroy", deepZoomTileCache.size, opts)
super.destroy(opts)
}
}
}