UNPKG

@pixi/core

Version:
146 lines (141 loc) 4.31 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var constants = require('@pixi/constants'); var settings = require('@pixi/settings'); var BaseImageResource = require('./BaseImageResource.js'); class ImageResource extends BaseImageResource.BaseImageResource { constructor(source, options) { options = options || {}; if (typeof source === "string") { const imageElement = new Image(); BaseImageResource.BaseImageResource.crossOrigin(imageElement, source, options.crossorigin); imageElement.src = source; source = imageElement; } super(source); if (!source.complete && !!this._width && !!this._height) { this._width = 0; this._height = 0; } this.url = source.src; this._process = null; this.preserveBitmap = false; this.createBitmap = (options.createBitmap ?? settings.settings.CREATE_IMAGE_BITMAP) && !!globalThis.createImageBitmap; this.alphaMode = typeof options.alphaMode === "number" ? options.alphaMode : null; this.bitmap = null; this._load = null; if (options.autoLoad !== false) { this.load(); } } load(createBitmap) { if (this._load) { return this._load; } if (createBitmap !== void 0) { this.createBitmap = createBitmap; } this._load = new Promise((resolve, reject) => { const source = this.source; this.url = source.src; const completed = () => { if (this.destroyed) { return; } source.onload = null; source.onerror = null; this.resize(source.width, source.height); this._load = null; if (this.createBitmap) { resolve(this.process()); } else { resolve(this); } }; if (source.complete && source.src) { completed(); } else { source.onload = completed; source.onerror = (event) => { reject(event); this.onError.emit(event); }; } }); return this._load; } process() { const source = this.source; if (this._process !== null) { return this._process; } if (this.bitmap !== null || !globalThis.createImageBitmap) { return Promise.resolve(this); } const createImageBitmap = globalThis.createImageBitmap; const cors = !source.crossOrigin || source.crossOrigin === "anonymous"; this._process = fetch(source.src, { mode: cors ? "cors" : "no-cors" }).then((r) => r.blob()).then((blob) => createImageBitmap(blob, 0, 0, source.width, source.height, { premultiplyAlpha: this.alphaMode === null || this.alphaMode === constants.ALPHA_MODES.UNPACK ? "premultiply" : "none" })).then((bitmap) => { if (this.destroyed) { return Promise.reject(); } this.bitmap = bitmap; this.update(); this._process = null; return Promise.resolve(this); }); return this._process; } upload(renderer, baseTexture, glTexture) { if (typeof this.alphaMode === "number") { baseTexture.alphaMode = this.alphaMode; } if (!this.createBitmap) { return super.upload(renderer, baseTexture, glTexture); } if (!this.bitmap) { this.process(); if (!this.bitmap) { return false; } } super.upload(renderer, baseTexture, glTexture, this.bitmap); if (!this.preserveBitmap) { let flag = true; const glTextures = baseTexture._glTextures; for (const key in glTextures) { const otherTex = glTextures[key]; if (otherTex !== glTexture && otherTex.dirtyId !== baseTexture.dirtyId) { flag = false; break; } } if (flag) { if (this.bitmap.close) { this.bitmap.close(); } this.bitmap = null; } } return true; } dispose() { this.source.onload = null; this.source.onerror = null; super.dispose(); if (this.bitmap) { this.bitmap.close(); this.bitmap = null; } this._process = null; this._load = null; } static test(source) { return typeof HTMLImageElement !== "undefined" && (typeof source === "string" || source instanceof HTMLImageElement); } } exports.ImageResource = ImageResource; //# sourceMappingURL=ImageResource.js.map