UNPKG

@pixi/core

Version:
62 lines (59 loc) 2.35 kB
import { ALPHA_MODES } from '@pixi/constants'; import { determineCrossOrigin } from '@pixi/utils'; import { Resource } from './Resource.mjs'; class BaseImageResource extends Resource { constructor(source) { const sourceAny = source; const width = sourceAny.naturalWidth || sourceAny.videoWidth || sourceAny.width; const height = sourceAny.naturalHeight || sourceAny.videoHeight || sourceAny.height; super(width, height); this.source = source; this.noSubImage = false; } static crossOrigin(element, url, crossorigin) { if (crossorigin === void 0 && !url.startsWith("data:")) { element.crossOrigin = determineCrossOrigin(url); } else if (crossorigin !== false) { element.crossOrigin = typeof crossorigin === "string" ? crossorigin : "anonymous"; } } upload(renderer, baseTexture, glTexture, source) { const gl = renderer.gl; const width = baseTexture.realWidth; const height = baseTexture.realHeight; source = source || this.source; if (typeof HTMLImageElement !== "undefined" && source instanceof HTMLImageElement) { if (!source.complete || source.naturalWidth === 0) { return false; } } else if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) { if (source.readyState <= 1 && source.buffered.length === 0) { return false; } } gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.alphaMode === ALPHA_MODES.UNPACK); if (!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); } else { glTexture.width = width; glTexture.height = height; gl.texImage2D(baseTexture.target, 0, glTexture.internalFormat, baseTexture.format, glTexture.type, source); } return true; } update() { if (this.destroyed) { return; } const source = this.source; const width = source.naturalWidth || source.videoWidth || source.width; const height = source.naturalHeight || source.videoHeight || source.height; this.resize(width, height); super.update(); } dispose() { this.source = null; } } export { BaseImageResource }; //# sourceMappingURL=BaseImageResource.mjs.map