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