@pixi/core
Version:
Core PixiJS
89 lines (86 loc) • 2.57 kB
JavaScript
import { ALPHA_MODES } from '@pixi/constants';
import { settings } from '@pixi/settings';
import { BaseImageResource } from './BaseImageResource.mjs';
class ImageBitmapResource extends BaseImageResource {
constructor(source, options) {
options = options || {};
let baseSource;
let url;
if (typeof source === "string") {
baseSource = ImageBitmapResource.EMPTY;
url = source;
} else {
baseSource = source;
url = null;
}
super(baseSource);
this.url = url;
this.crossOrigin = options.crossOrigin ?? true;
this.alphaMode = typeof options.alphaMode === "number" ? options.alphaMode : null;
this._load = null;
if (options.autoLoad !== false) {
this.load();
}
}
load() {
if (this._load) {
return this._load;
}
this._load = new Promise(async (resolve, reject) => {
if (this.url === null) {
resolve(this);
return;
}
try {
const response = await settings.ADAPTER.fetch(this.url, {
mode: this.crossOrigin ? "cors" : "no-cors"
});
if (this.destroyed)
return;
const imageBlob = await response.blob();
if (this.destroyed)
return;
const imageBitmap = await createImageBitmap(imageBlob, {
premultiplyAlpha: this.alphaMode === null || this.alphaMode === ALPHA_MODES.UNPACK ? "premultiply" : "none"
});
if (this.destroyed)
return;
this.source = imageBitmap;
this.update();
resolve(this);
} catch (e) {
if (this.destroyed)
return;
reject(e);
this.onError.emit(e);
}
});
return this._load;
}
upload(renderer, baseTexture, glTexture) {
if (!(this.source instanceof ImageBitmap)) {
this.load();
return false;
}
if (typeof this.alphaMode === "number") {
baseTexture.alphaMode = this.alphaMode;
}
return super.upload(renderer, baseTexture, glTexture);
}
dispose() {
if (this.source instanceof ImageBitmap) {
this.source.close();
}
super.dispose();
this._load = null;
}
static test(source) {
return !!globalThis.createImageBitmap && typeof ImageBitmap !== "undefined" && (typeof source === "string" || source instanceof ImageBitmap);
}
static get EMPTY() {
ImageBitmapResource._EMPTY = ImageBitmapResource._EMPTY ?? settings.ADAPTER.createCanvas(0, 0);
return ImageBitmapResource._EMPTY;
}
}
export { ImageBitmapResource };
//# sourceMappingURL=ImageBitmapResource.mjs.map