const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined; import {Resource} from '@avocado/resource'; export class Image extends Resource { constructor() { super(); this.texture = null; } static load(uri) { return this.loadBaseTexture(uri).then((baseTexture) => { const image = new Image(); image.uri = uri; image.texture = new PIXI.Texture(baseTexture); return image; }); } static loadBaseTexture(uri) { if (!this.baseTextureCache) { this.baseTextureCache = {}; } if (this.baseTextureCache[uri]) { return Promise.resolve(this.baseTextureCache[uri]); } return new Promise((resolve, reject) => { const baseTexture = PIXI.BaseTexture.fromImage(uri); baseTexture.on('error', () => { reject(new Error(`Couldn't load image "${uri}"`)); }); baseTexture.on('loaded', () => { resolve(this.baseTextureCache[uri] = baseTexture); }); }); } get height() { if (!this.texture) { return 0; } return this.texture.height; } get size() { return [this.width, this.height]; } get width() { if (!this.texture) { return 0; } return this.texture.width; } }