UNPKG

2.84 kBJavaScriptView Raw
1import { ALPHA_MODES } from "@pixi/constants";
2import { determineCrossOrigin } from "@pixi/utils";
3import { Resource } from "./Resource.mjs";
4class BaseImageResource extends Resource {
5 /**
6 * @param {PIXI.ImageSourcee} source
7 */
8 constructor(source) {
9 const sourceAny = source, width = sourceAny.naturalWidth || sourceAny.videoWidth || sourceAny.displayWidth || sourceAny.width, height = sourceAny.naturalHeight || sourceAny.videoHeight || sourceAny.displayHeight || sourceAny.height;
10 super(width, height), this.source = source, this.noSubImage = !1;
11 }
12 /**
13 * Set cross origin based detecting the url and the crossorigin
14 * @param element - Element to apply crossOrigin
15 * @param url - URL to check
16 * @param crossorigin - Cross origin value to use
17 */
18 static crossOrigin(element, url, crossorigin) {
19 crossorigin === void 0 && !url.startsWith("data:") ? element.crossOrigin = determineCrossOrigin(url) : crossorigin !== !1 && (element.crossOrigin = typeof crossorigin == "string" ? crossorigin : "anonymous");
20 }
21 /**
22 * Upload the texture to the GPU.
23 * @param renderer - Upload to the renderer
24 * @param baseTexture - Reference to parent texture
25 * @param glTexture
26 * @param {PIXI.ImageSourcee} [source] - (optional)
27 * @returns - true is success
28 */
29 upload(renderer, baseTexture, glTexture, source) {
30 const gl = renderer.gl, width = baseTexture.realWidth, height = baseTexture.realHeight;
31 if (source = source || this.source, typeof HTMLImageElement < "u" && source instanceof HTMLImageElement) {
32 if (!source.complete || source.naturalWidth === 0)
33 return !1;
34 } else if (typeof HTMLVideoElement < "u" && source instanceof HTMLVideoElement && source.readyState <= 1)
35 return !1;
36 return gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.alphaMode === ALPHA_MODES.UNPACK), !this.noSubImage && baseTexture.target === gl.TEXTURE_2D && glTexture.width === width && glTexture.height === height ? gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, baseTexture.format, glTexture.type, source) : (glTexture.width = width, glTexture.height = height, gl.texImage2D(baseTexture.target, 0, glTexture.internalFormat, baseTexture.format, glTexture.type, source)), !0;
37 }
38 /**
39 * Checks if source width/height was changed, resize can cause extra baseTexture update.
40 * Triggers one update in any case.
41 */
42 update() {
43 if (this.destroyed)
44 return;
45 const source = this.source, width = source.naturalWidth || source.videoWidth || source.width, height = source.naturalHeight || source.videoHeight || source.height;
46 this.resize(width, height), super.update();
47 }
48 /** Destroy this {@link PIXI.BaseImageResource} */
49 dispose() {
50 this.source = null;
51 }
52}
53export {
54 BaseImageResource
55};
56//# sourceMappingURL=BaseImageResource.mjs.map