UNPKG

4.9 kBJavaScriptView Raw
1import { ALPHA_MODES } from "@pixi/constants";
2import { settings } from "@pixi/settings";
3import { BaseImageResource } from "./BaseImageResource.mjs";
4class ImageResource extends BaseImageResource {
5 /**
6 * @param source - image source or URL
7 * @param options
8 * @param {boolean} [options.autoLoad=true] - start loading process
9 * @param {boolean} [options.createBitmap=PIXI.settings.CREATE_IMAGE_BITMAP] - whether its required to create
10 * a bitmap before upload
11 * @param {boolean} [options.crossorigin=true] - Load image using cross origin
12 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.UNPACK] - Premultiply image alpha in bitmap
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 * Returns a promise when image will be loaded and processed.
23 * @param createBitmap - whether process image into bitmap
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 * Called when we need to convert image into BitmapImage.
39 * Can be called multiple times, real promise is cached inside.
40 * @returns - Cached promise to fill that bitmap
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 * Upload the image resource to GPU.
67 * @param renderer - Renderer to upload to
68 * @param baseTexture - BaseTexture for this resource
69 * @param glTexture - GLTexture to use
70 * @returns {boolean} true is success
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 /** Destroys this resource. */
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 * Used to auto-detect the type of resource.
97 * @param {*} source - The source object
98 * @returns {boolean} `true` if current environment support HTMLImageElement, and source is string or HTMLImageElement
99 */
100 static test(source) {
101 return typeof HTMLImageElement < "u" && (typeof source == "string" || source instanceof HTMLImageElement);
102 }
103}
104export {
105 ImageResource
106};
107//# sourceMappingURL=ImageResource.mjs.map