fix: texture wonkery

This commit is contained in:
cha0s 2022-03-15 14:31:34 -05:00
parent 6761183563
commit e20c2c8a4d

View File

@ -50,7 +50,9 @@ export default class Image extends Resource {
return;
}
if (cache.has(uri)) {
this.texture = await this.constructor.pixiTextureFromElement(await cache.get(uri));
this.texture = await this.constructor.pixiTextureFromElementWithCaching(
await cache.get(uri),
);
return;
}
const image = window.document.createElement('img');
@ -63,7 +65,9 @@ export default class Image extends Resource {
});
});
cache.set(uri, promise);
this.texture = await this.constructor.pixiTextureFromElement(await promise);
this.texture = await this.constructor.pixiTextureFromElementWithCaching(
await promise,
);
}
else {
this.texture = {
@ -74,28 +78,34 @@ export default class Image extends Resource {
}
}
// This is a little wonky, but basically we're reaching into PIXI's guts a bit.
// @todo this will probably break image loading at some point.
static pixiTextureFromElement(element) {
static async pixiTextureFromElement(element) {
// eslint-disable-next-line global-require
const {Texture} = require('@pixi/core');
const clone = element.cloneNode();
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
clone._pixiId = element._pixiId;
const texture = Texture.from(clone);
const texture = Texture.from(element);
// Cache hit.
if (texture.valid) {
return texture;
}
return new Promise((resolve) => {
texture.once('update', () => {
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
element._pixiId = clone._pixiId;
resolve(texture);
});
});
}
// This is a little wonky, but basically we're reaching into PIXI's guts a bit for better
// perf.
// @todo this will probably break image loading at some point.
static async pixiTextureFromElementWithCaching(element) {
const clone = element.cloneNode();
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
clone._pixiId = element._pixiId;
const texture = await this.pixiTextureFromElement(clone);
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
element._pixiId = clone._pixiId;
return texture;
}
get size() {
return [this.width, this.height];
}