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