UNPKG

3.37 kBJavaScriptView Raw
1"use strict";
2var constants = require("@pixi/constants"), settings = require("@pixi/settings"), BaseImageResource = require("./BaseImageResource.js");
3class ImageBitmapResource extends BaseImageResource.BaseImageResource {
4 /**
5 * @param source - ImageBitmap or URL to use.
6 * @param options - Options to use.
7 */
8 constructor(source, options) {
9 options = options || {};
10 let baseSource, url, ownsImageBitmap;
11 typeof source == "string" ? (baseSource = ImageBitmapResource.EMPTY, url = source, ownsImageBitmap = !0) : (baseSource = source, url = null, ownsImageBitmap = !1), super(baseSource), this.url = url, this.crossOrigin = options.crossOrigin ?? !0, this.alphaMode = typeof options.alphaMode == "number" ? options.alphaMode : null, this.ownsImageBitmap = options.ownsImageBitmap ?? ownsImageBitmap, this._load = null, options.autoLoad !== !1 && this.load();
12 }
13 load() {
14 return this._load ? this._load : (this._load = new Promise(async (resolve, reject) => {
15 if (this.url === null) {
16 resolve(this);
17 return;
18 }
19 try {
20 const response = await settings.settings.ADAPTER.fetch(this.url, {
21 mode: this.crossOrigin ? "cors" : "no-cors"
22 });
23 if (this.destroyed)
24 return;
25 const imageBlob = await response.blob();
26 if (this.destroyed)
27 return;
28 const imageBitmap = await createImageBitmap(imageBlob, {
29 premultiplyAlpha: this.alphaMode === null || this.alphaMode === constants.ALPHA_MODES.UNPACK ? "premultiply" : "none"
30 });
31 if (this.destroyed) {
32 imageBitmap.close();
33 return;
34 }
35 this.source = imageBitmap, this.update(), resolve(this);
36 } catch (e) {
37 if (this.destroyed)
38 return;
39 reject(e), this.onError.emit(e);
40 }
41 }), this._load);
42 }
43 /**
44 * Upload the image bitmap resource to GPU.
45 * @param renderer - Renderer to upload to
46 * @param baseTexture - BaseTexture for this resource
47 * @param glTexture - GLTexture to use
48 * @returns {boolean} true is success
49 */
50 upload(renderer, baseTexture, glTexture) {
51 return this.source instanceof ImageBitmap ? (typeof this.alphaMode == "number" && (baseTexture.alphaMode = this.alphaMode), super.upload(renderer, baseTexture, glTexture)) : (this.load(), !1);
52 }
53 /** Destroys this resource. */
54 dispose() {
55 this.ownsImageBitmap && this.source instanceof ImageBitmap && this.source.close(), super.dispose(), this._load = null;
56 }
57 /**
58 * Used to auto-detect the type of resource.
59 * @param {*} source - The source object
60 * @returns {boolean} `true` if current environment support ImageBitmap, and source is string or ImageBitmap
61 */
62 static test(source) {
63 return !!globalThis.createImageBitmap && typeof ImageBitmap < "u" && (typeof source == "string" || source instanceof ImageBitmap);
64 }
65 /**
66 * ImageBitmap cannot be created synchronously, so a empty placeholder canvas is needed when loading from URLs.
67 * Only for internal usage.
68 * @returns The cached placeholder canvas.
69 */
70 static get EMPTY() {
71 return ImageBitmapResource._EMPTY = ImageBitmapResource._EMPTY ?? settings.settings.ADAPTER.createCanvas(0, 0), ImageBitmapResource._EMPTY;
72 }
73}
74exports.ImageBitmapResource = ImageBitmapResource;
75//# sourceMappingURL=ImageBitmapResource.js.map