@pixi/core
Version:
Core PixiJS
93 lines (88 loc) • 2.71 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var constants = require('@pixi/constants');
var settings = require('@pixi/settings');
var BaseImageResource = require('./BaseImageResource.js');
class ImageBitmapResource extends BaseImageResource.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.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 === constants.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.settings.ADAPTER.createCanvas(0, 0);
return ImageBitmapResource._EMPTY;
}
}
exports.ImageBitmapResource = ImageBitmapResource;
//# sourceMappingURL=ImageBitmapResource.js.map