UNPKG

2.71 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var constants = require('@pixi/constants');
6var settings = require('@pixi/settings');
7var BaseImageResource = require('./BaseImageResource.js');
8
9class ImageBitmapResource extends BaseImageResource.BaseImageResource {
10 constructor(source, options) {
11 options = options || {};
12 let baseSource;
13 let url;
14 if (typeof source === "string") {
15 baseSource = ImageBitmapResource.EMPTY;
16 url = source;
17 } else {
18 baseSource = source;
19 url = null;
20 }
21 super(baseSource);
22 this.url = url;
23 this.crossOrigin = options.crossOrigin ?? true;
24 this.alphaMode = typeof options.alphaMode === "number" ? options.alphaMode : null;
25 this._load = null;
26 if (options.autoLoad !== false) {
27 this.load();
28 }
29 }
30 load() {
31 if (this._load) {
32 return this._load;
33 }
34 this._load = new Promise(async (resolve, reject) => {
35 if (this.url === null) {
36 resolve(this);
37 return;
38 }
39 try {
40 const response = await settings.settings.ADAPTER.fetch(this.url, {
41 mode: this.crossOrigin ? "cors" : "no-cors"
42 });
43 if (this.destroyed)
44 return;
45 const imageBlob = await response.blob();
46 if (this.destroyed)
47 return;
48 const imageBitmap = await createImageBitmap(imageBlob, {
49 premultiplyAlpha: this.alphaMode === null || this.alphaMode === constants.ALPHA_MODES.UNPACK ? "premultiply" : "none"
50 });
51 if (this.destroyed)
52 return;
53 this.source = imageBitmap;
54 this.update();
55 resolve(this);
56 } catch (e) {
57 if (this.destroyed)
58 return;
59 reject(e);
60 this.onError.emit(e);
61 }
62 });
63 return this._load;
64 }
65 upload(renderer, baseTexture, glTexture) {
66 if (!(this.source instanceof ImageBitmap)) {
67 this.load();
68 return false;
69 }
70 if (typeof this.alphaMode === "number") {
71 baseTexture.alphaMode = this.alphaMode;
72 }
73 return super.upload(renderer, baseTexture, glTexture);
74 }
75 dispose() {
76 if (this.source instanceof ImageBitmap) {
77 this.source.close();
78 }
79 super.dispose();
80 this._load = null;
81 }
82 static test(source) {
83 return !!globalThis.createImageBitmap && typeof ImageBitmap !== "undefined" && (typeof source === "string" || source instanceof ImageBitmap);
84 }
85 static get EMPTY() {
86 ImageBitmapResource._EMPTY = ImageBitmapResource._EMPTY ?? settings.settings.ADAPTER.createCanvas(0, 0);
87 return ImageBitmapResource._EMPTY;
88 }
89}
90
91exports.ImageBitmapResource = ImageBitmapResource;
92//# sourceMappingURL=ImageBitmapResource.js.map