1 | "use strict";
|
2 | var constants = require("@pixi/constants"), settings = require("@pixi/settings"), BaseImageResource = require("./BaseImageResource.js");
|
3 | class ImageBitmapResource extends BaseImageResource.BaseImageResource {
|
4 | |
5 |
|
6 |
|
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 |
|
45 |
|
46 |
|
47 |
|
48 |
|
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 |
|
54 | dispose() {
|
55 | this.ownsImageBitmap && this.source instanceof ImageBitmap && this.source.close(), super.dispose(), this._load = null;
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | static test(source) {
|
63 | return !!globalThis.createImageBitmap && typeof ImageBitmap < "u" && (typeof source == "string" || source instanceof ImageBitmap);
|
64 | }
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 | static get EMPTY() {
|
71 | return ImageBitmapResource._EMPTY = ImageBitmapResource._EMPTY ?? settings.settings.ADAPTER.createCanvas(0, 0), ImageBitmapResource._EMPTY;
|
72 | }
|
73 | }
|
74 | exports.ImageBitmapResource = ImageBitmapResource;
|
75 |
|