UNPKG

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