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