1 | "use strict";
|
2 | var constants = require("@pixi/constants"), settings = require("@pixi/settings"), BaseImageResource = require("./BaseImageResource.js");
|
3 | class ImageResource extends BaseImageResource.BaseImageResource {
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | constructor(source, options) {
|
14 | if (options = options || {}, typeof source == "string") {
|
15 | const imageElement = new Image();
|
16 | BaseImageResource.BaseImageResource.crossOrigin(imageElement, source, options.crossorigin), imageElement.src = source, source = imageElement;
|
17 | }
|
18 | super(source), !source.complete && this._width && this._height && (this._width = 0, this._height = 0), this.url = source.src, this._process = null, this.preserveBitmap = !1, 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, options.autoLoad !== !1 && this.load();
|
19 | }
|
20 | |
21 |
|
22 |
|
23 |
|
24 | load(createBitmap) {
|
25 | return this._load ? this._load : (createBitmap !== void 0 && (this.createBitmap = createBitmap), this._load = new Promise((resolve, reject) => {
|
26 | const source = this.source;
|
27 | this.url = source.src;
|
28 | const completed = () => {
|
29 | this.destroyed || (source.onload = null, source.onerror = null, this.update(), this._load = null, this.createBitmap ? resolve(this.process()) : resolve(this));
|
30 | };
|
31 | source.complete && source.src ? completed() : (source.onload = completed, source.onerror = (event) => {
|
32 | reject(event), this.onError.emit(event);
|
33 | });
|
34 | }), this._load);
|
35 | }
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | process() {
|
42 | const source = this.source;
|
43 | if (this._process !== null)
|
44 | return this._process;
|
45 | if (this.bitmap !== null || !globalThis.createImageBitmap)
|
46 | return Promise.resolve(this);
|
47 | const createImageBitmap = globalThis.createImageBitmap, cors = !source.crossOrigin || source.crossOrigin === "anonymous";
|
48 | return this._process = fetch(
|
49 | source.src,
|
50 | {
|
51 | mode: cors ? "cors" : "no-cors"
|
52 | }
|
53 | ).then((r) => r.blob()).then((blob) => createImageBitmap(
|
54 | blob,
|
55 | 0,
|
56 | 0,
|
57 | source.width,
|
58 | source.height,
|
59 | {
|
60 | premultiplyAlpha: this.alphaMode === null || this.alphaMode === constants.ALPHA_MODES.UNPACK ? "premultiply" : "none"
|
61 | }
|
62 | )).then((bitmap) => this.destroyed ? Promise.reject() : (this.bitmap = bitmap, this.update(), this._process = null, Promise.resolve(this))), this._process;
|
63 | }
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | upload(renderer, baseTexture, glTexture) {
|
72 | if (typeof this.alphaMode == "number" && (baseTexture.alphaMode = this.alphaMode), !this.createBitmap)
|
73 | return super.upload(renderer, baseTexture, glTexture);
|
74 | if (!this.bitmap && (this.process(), !this.bitmap))
|
75 | return !1;
|
76 | if (super.upload(renderer, baseTexture, glTexture, this.bitmap), !this.preserveBitmap) {
|
77 | let flag = !0;
|
78 | const glTextures = baseTexture._glTextures;
|
79 | for (const key in glTextures) {
|
80 | const otherTex = glTextures[key];
|
81 | if (otherTex !== glTexture && otherTex.dirtyId !== baseTexture.dirtyId) {
|
82 | flag = !1;
|
83 | break;
|
84 | }
|
85 | }
|
86 | flag && (this.bitmap.close && this.bitmap.close(), this.bitmap = null);
|
87 | }
|
88 | return !0;
|
89 | }
|
90 |
|
91 | dispose() {
|
92 | this.source.onload = null, this.source.onerror = null, super.dispose(), this.bitmap && (this.bitmap.close(), this.bitmap = null), this._process = null, this._load = null;
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | static test(source) {
|
100 | return typeof HTMLImageElement < "u" && (typeof source == "string" || source instanceof HTMLImageElement);
|
101 | }
|
102 | }
|
103 | exports.ImageResource = ImageResource;
|
104 |
|