1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var constants = require('@pixi/constants');
|
6 | var utils = require('@pixi/utils');
|
7 | var Resource = require('./Resource.js');
|
8 |
|
9 | class BaseImageResource extends Resource.Resource {
|
10 | constructor(source) {
|
11 | const sourceAny = source;
|
12 | const width = sourceAny.naturalWidth || sourceAny.videoWidth || sourceAny.width;
|
13 | const height = sourceAny.naturalHeight || sourceAny.videoHeight || sourceAny.height;
|
14 | super(width, height);
|
15 | this.source = source;
|
16 | this.noSubImage = false;
|
17 | }
|
18 | static crossOrigin(element, url, crossorigin) {
|
19 | if (crossorigin === void 0 && !url.startsWith("data:")) {
|
20 | element.crossOrigin = utils.determineCrossOrigin(url);
|
21 | } else if (crossorigin !== false) {
|
22 | element.crossOrigin = typeof crossorigin === "string" ? crossorigin : "anonymous";
|
23 | }
|
24 | }
|
25 | upload(renderer, baseTexture, glTexture, source) {
|
26 | const gl = renderer.gl;
|
27 | const width = baseTexture.realWidth;
|
28 | const height = baseTexture.realHeight;
|
29 | source = source || this.source;
|
30 | if (typeof HTMLImageElement !== "undefined" && source instanceof HTMLImageElement) {
|
31 | if (!source.complete || source.naturalWidth === 0) {
|
32 | return false;
|
33 | }
|
34 | } else if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) {
|
35 | if (source.readyState <= 1 && source.buffered.length === 0) {
|
36 | return false;
|
37 | }
|
38 | }
|
39 | gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.alphaMode === constants.ALPHA_MODES.UNPACK);
|
40 | if (!this.noSubImage && baseTexture.target === gl.TEXTURE_2D && glTexture.width === width && glTexture.height === height) {
|
41 | gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, baseTexture.format, glTexture.type, source);
|
42 | } else {
|
43 | glTexture.width = width;
|
44 | glTexture.height = height;
|
45 | gl.texImage2D(baseTexture.target, 0, glTexture.internalFormat, baseTexture.format, glTexture.type, source);
|
46 | }
|
47 | return true;
|
48 | }
|
49 | update() {
|
50 | if (this.destroyed) {
|
51 | return;
|
52 | }
|
53 | const source = this.source;
|
54 | const width = source.naturalWidth || source.videoWidth || source.width;
|
55 | const height = source.naturalHeight || source.videoHeight || source.height;
|
56 | this.resize(width, height);
|
57 | super.update();
|
58 | }
|
59 | dispose() {
|
60 | this.source = null;
|
61 | }
|
62 | }
|
63 |
|
64 | exports.BaseImageResource = BaseImageResource;
|
65 |
|