diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/Spritesheet.js b/src/core/textures/Spritesheet.js index d469c62..9b24f27 100644 --- a/src/core/textures/Spritesheet.js +++ b/src/core/textures/Spritesheet.js @@ -208,6 +208,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage functions TextureCache[i] = this.textures[i]; + this.textures[i].textureCacheId = i; } frameIndex++; diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/Spritesheet.js b/src/core/textures/Spritesheet.js index d469c62..9b24f27 100644 --- a/src/core/textures/Spritesheet.js +++ b/src/core/textures/Spritesheet.js @@ -208,6 +208,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage functions TextureCache[i] = this.textures[i]; + this.textures[i].textureCacheId = i; } frameIndex++; diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index e70804e..bfaca9b 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -156,6 +156,15 @@ * @type {Object} */ this.transform = null; + + /** + * The id under which this Texture has been added to the texture cache. This is + * automatically set in certain cases, but may not always be accurate, particularly if + * the texture is in the cache under multiple ids. + * + * @member {string} + */ + this.textureCacheId = null; } /** @@ -238,11 +247,17 @@ this._uvs = null; this.trim = null; this.orig = null; + this.textureCacheId = null; this.valid = false; - this.off('dispose', this.dispose, this); - this.off('update', this.update, this); + for (const prop in TextureCache) + { + if (TextureCache[prop] === this) + { + delete TextureCache[prop]; + } + } } /** @@ -439,6 +454,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage fucntions BaseTextureCache[name] = baseTexture; TextureCache[name] = texture; + texture.textureCacheId = name; // also add references by url if they are different. if (name !== imageUrl) @@ -459,6 +475,10 @@ */ static addTextureToCache(texture, id) { + if (!texture.textureCacheId) + { + texture.textureCacheId = id; + } TextureCache[id] = texture; } diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/Spritesheet.js b/src/core/textures/Spritesheet.js index d469c62..9b24f27 100644 --- a/src/core/textures/Spritesheet.js +++ b/src/core/textures/Spritesheet.js @@ -208,6 +208,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage functions TextureCache[i] = this.textures[i]; + this.textures[i].textureCacheId = i; } frameIndex++; diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index e70804e..bfaca9b 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -156,6 +156,15 @@ * @type {Object} */ this.transform = null; + + /** + * The id under which this Texture has been added to the texture cache. This is + * automatically set in certain cases, but may not always be accurate, particularly if + * the texture is in the cache under multiple ids. + * + * @member {string} + */ + this.textureCacheId = null; } /** @@ -238,11 +247,17 @@ this._uvs = null; this.trim = null; this.orig = null; + this.textureCacheId = null; this.valid = false; - this.off('dispose', this.dispose, this); - this.off('update', this.update, this); + for (const prop in TextureCache) + { + if (TextureCache[prop] === this) + { + delete TextureCache[prop]; + } + } } /** @@ -439,6 +454,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage fucntions BaseTextureCache[name] = baseTexture; TextureCache[name] = texture; + texture.textureCacheId = name; // also add references by url if they are different. if (name !== imageUrl) @@ -459,6 +475,10 @@ */ static addTextureToCache(texture, id) { + if (!texture.textureCacheId) + { + texture.textureCacheId = id; + } TextureCache[id] = texture; } diff --git a/test/core/BaseTexture.js b/test/core/BaseTexture.js index 8b93478..bb65068 100644 --- a/test/core/BaseTexture.js +++ b/test/core/BaseTexture.js @@ -14,4 +14,28 @@ expect(baseTexture.imageType).to.be.equals('png'); }); }); + + it('should remove Canvas BaseTexture from cache on destroy', function () + { + const canvas = document.createElement('canvas'); + const texture = PIXI.BaseTexture.fromCanvas(canvas); + const _pixiId = canvas._pixiId; + + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(texture); + texture.destroy(); + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(undefined); + }); + + it('should remove Image BaseTexture from cache on destroy', function () + { + const URL = 'foo.png'; + const NAME = 'bar'; + const image = new Image(); + + const texture = PIXI.Texture.fromLoader(image, URL, NAME); + + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(texture.baseTexture); + texture.destroy(true); + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(undefined); + }); }); diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/Spritesheet.js b/src/core/textures/Spritesheet.js index d469c62..9b24f27 100644 --- a/src/core/textures/Spritesheet.js +++ b/src/core/textures/Spritesheet.js @@ -208,6 +208,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage functions TextureCache[i] = this.textures[i]; + this.textures[i].textureCacheId = i; } frameIndex++; diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index e70804e..bfaca9b 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -156,6 +156,15 @@ * @type {Object} */ this.transform = null; + + /** + * The id under which this Texture has been added to the texture cache. This is + * automatically set in certain cases, but may not always be accurate, particularly if + * the texture is in the cache under multiple ids. + * + * @member {string} + */ + this.textureCacheId = null; } /** @@ -238,11 +247,17 @@ this._uvs = null; this.trim = null; this.orig = null; + this.textureCacheId = null; this.valid = false; - this.off('dispose', this.dispose, this); - this.off('update', this.update, this); + for (const prop in TextureCache) + { + if (TextureCache[prop] === this) + { + delete TextureCache[prop]; + } + } } /** @@ -439,6 +454,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage fucntions BaseTextureCache[name] = baseTexture; TextureCache[name] = texture; + texture.textureCacheId = name; // also add references by url if they are different. if (name !== imageUrl) @@ -459,6 +475,10 @@ */ static addTextureToCache(texture, id) { + if (!texture.textureCacheId) + { + texture.textureCacheId = id; + } TextureCache[id] = texture; } diff --git a/test/core/BaseTexture.js b/test/core/BaseTexture.js index 8b93478..bb65068 100644 --- a/test/core/BaseTexture.js +++ b/test/core/BaseTexture.js @@ -14,4 +14,28 @@ expect(baseTexture.imageType).to.be.equals('png'); }); }); + + it('should remove Canvas BaseTexture from cache on destroy', function () + { + const canvas = document.createElement('canvas'); + const texture = PIXI.BaseTexture.fromCanvas(canvas); + const _pixiId = canvas._pixiId; + + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(texture); + texture.destroy(); + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(undefined); + }); + + it('should remove Image BaseTexture from cache on destroy', function () + { + const URL = 'foo.png'; + const NAME = 'bar'; + const image = new Image(); + + const texture = PIXI.Texture.fromLoader(image, URL, NAME); + + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(texture.baseTexture); + texture.destroy(true); + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(undefined); + }); }); diff --git a/test/core/Spritesheet.js b/test/core/Spritesheet.js index 283adf5..7601a0c 100644 --- a/test/core/Spritesheet.js +++ b/test/core/Spritesheet.js @@ -18,6 +18,7 @@ expect(textures[id]).to.be.an.instanceof(PIXI.Texture); expect(textures[id].width).to.equal(spritesheet.data.frames[id].frame.w / spritesheet.resolution); expect(textures[id].height).to.equal(spritesheet.data.frames[id].frame.h / spritesheet.resolution); + expect(textures[id].textureCacheId).to.equal(id); spritesheet.destroy(true); expect(spritesheet.textures).to.be.null; expect(spritesheet.baseTexture).to.be.null; diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index bbee141..61feccf 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -588,7 +588,6 @@ { if (this.imageUrl) { - delete BaseTextureCache[this.imageUrl]; delete TextureCache[this.imageUrl]; this.imageUrl = null; @@ -598,16 +597,18 @@ this.source.src = ''; } } - // An svg source has both `imageUrl` and `__pixiId`, so no `else if` here - if (this.source && this.source._pixiId) - { - delete BaseTextureCache[this.source._pixiId]; - } this.source = null; this.dispose(); + for (const prop in BaseTextureCache) + { + if (BaseTextureCache[prop] === this) + { + delete BaseTextureCache[prop]; + } + } this._destroyed = true; } diff --git a/src/core/textures/Spritesheet.js b/src/core/textures/Spritesheet.js index d469c62..9b24f27 100644 --- a/src/core/textures/Spritesheet.js +++ b/src/core/textures/Spritesheet.js @@ -208,6 +208,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage functions TextureCache[i] = this.textures[i]; + this.textures[i].textureCacheId = i; } frameIndex++; diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index e70804e..bfaca9b 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -156,6 +156,15 @@ * @type {Object} */ this.transform = null; + + /** + * The id under which this Texture has been added to the texture cache. This is + * automatically set in certain cases, but may not always be accurate, particularly if + * the texture is in the cache under multiple ids. + * + * @member {string} + */ + this.textureCacheId = null; } /** @@ -238,11 +247,17 @@ this._uvs = null; this.trim = null; this.orig = null; + this.textureCacheId = null; this.valid = false; - this.off('dispose', this.dispose, this); - this.off('update', this.update, this); + for (const prop in TextureCache) + { + if (TextureCache[prop] === this) + { + delete TextureCache[prop]; + } + } } /** @@ -439,6 +454,7 @@ // lets also add the frame to pixi's global cache for fromFrame and fromImage fucntions BaseTextureCache[name] = baseTexture; TextureCache[name] = texture; + texture.textureCacheId = name; // also add references by url if they are different. if (name !== imageUrl) @@ -459,6 +475,10 @@ */ static addTextureToCache(texture, id) { + if (!texture.textureCacheId) + { + texture.textureCacheId = id; + } TextureCache[id] = texture; } diff --git a/test/core/BaseTexture.js b/test/core/BaseTexture.js index 8b93478..bb65068 100644 --- a/test/core/BaseTexture.js +++ b/test/core/BaseTexture.js @@ -14,4 +14,28 @@ expect(baseTexture.imageType).to.be.equals('png'); }); }); + + it('should remove Canvas BaseTexture from cache on destroy', function () + { + const canvas = document.createElement('canvas'); + const texture = PIXI.BaseTexture.fromCanvas(canvas); + const _pixiId = canvas._pixiId; + + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(texture); + texture.destroy(); + expect(PIXI.utils.BaseTextureCache[_pixiId]).to.equal(undefined); + }); + + it('should remove Image BaseTexture from cache on destroy', function () + { + const URL = 'foo.png'; + const NAME = 'bar'; + const image = new Image(); + + const texture = PIXI.Texture.fromLoader(image, URL, NAME); + + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(texture.baseTexture); + texture.destroy(true); + expect(PIXI.utils.BaseTextureCache[NAME]).to.equal(undefined); + }); }); diff --git a/test/core/Spritesheet.js b/test/core/Spritesheet.js index 283adf5..7601a0c 100644 --- a/test/core/Spritesheet.js +++ b/test/core/Spritesheet.js @@ -18,6 +18,7 @@ expect(textures[id]).to.be.an.instanceof(PIXI.Texture); expect(textures[id].width).to.equal(spritesheet.data.frames[id].frame.w / spritesheet.resolution); expect(textures[id].height).to.equal(spritesheet.data.frames[id].frame.h / spritesheet.resolution); + expect(textures[id].textureCacheId).to.equal(id); spritesheet.destroy(true); expect(spritesheet.textures).to.be.null; expect(spritesheet.baseTexture).to.be.null; diff --git a/test/core/Texture.js b/test/core/Texture.js index 8c4ef98..241ec3e 100644 --- a/test/core/Texture.js +++ b/test/core/Texture.js @@ -16,4 +16,20 @@ expect(PIXI.utils.TextureCache[URL]).to.equal(texture); expect(PIXI.utils.BaseTextureCache[URL]).to.equal(texture.baseTexture); }); + + it('should remove Texture from cache on destroy', function () + { + const NAME = 'foo'; + const NAME2 = 'bar'; + const texture = new PIXI.Texture(new PIXI.BaseTexture()); + + PIXI.Texture.addTextureToCache(texture, NAME); + PIXI.Texture.addTextureToCache(texture, NAME2); + expect(texture.textureCacheId).to.equal(NAME); + expect(PIXI.utils.TextureCache[NAME]).to.equal(texture); + expect(PIXI.utils.TextureCache[NAME2]).to.equal(texture); + texture.destroy(); + expect(PIXI.utils.TextureCache[NAME]).to.equal(undefined); + expect(PIXI.utils.TextureCache[NAME2]).to.equal(undefined); + }); });