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