Skip to content

Commit 6ee149a

Browse files
committed
Merge remote-tracking branch 'upstream/main' into src-function
2 parents 23c8be0 + df674dc commit 6ee149a

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/getSourceDimensions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function getSourceDimensions(source) {
2+
if (!source) return [0, 0];
3+
4+
const tag = (source.tagName || '').toLowerCase();
5+
if (tag === 'video') return [source.videoWidth, source.videoHeight];
6+
if (tag === 'img') return [source.naturalWidth, source.naturalHeight];
7+
return [source.width, source.height];
8+
}

src/webgl/GeneralTexture.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Texture from './Texture.js';
2+
import getSourceDimensions from '../utils/getSourceDimensions.js';
23

34
const IMAGE = 0;
45
const VIDEO = 1;
@@ -77,7 +78,6 @@ export default function GeneralTexture(
7778
}
7879
} else if (source instanceof HTMLVideoElement) {
7980
type = VIDEO;
80-
videoOnSetup();
8181
} else if (source instanceof HTMLCanvasElement) {
8282
type = CANVAS;
8383
imageOnload();
@@ -119,12 +119,13 @@ export default function GeneralTexture(
119119
source.crossOrigin = 'anonymous';
120120
source.src = src;
121121
addHiddenInDOM(source);
122-
123-
videoOnSetup();
124122
source.play();
125123
}
126124

125+
let hasVideoSetup = false;
127126
function videoOnSetup() {
127+
if (hasVideoSetup) return;
128+
hasVideoSetup = true;
128129
updateResolution();
129130
texture.setParameters([
130131
[gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE],
@@ -147,8 +148,6 @@ export default function GeneralTexture(
147148
source.autoplay = true;
148149
source.srcObject = stream;
149150
addHiddenInDOM(source);
150-
151-
videoOnSetup();
152151
};
153152

154153
const initCam = () => {
@@ -171,8 +170,9 @@ export default function GeneralTexture(
171170

172171
function updateResolution() {
173172
if (source) {
174-
ustate[1].value[0] = source.width;
175-
ustate[1].value[1] = source.height;
173+
const [w, h] = getSourceDimensions(source);
174+
ustate[1].value[0] = w;
175+
ustate[1].value[1] = h;
176176
}
177177
}
178178

@@ -189,6 +189,7 @@ export default function GeneralTexture(
189189
ustate.forEach(updateSingleUniform);
190190

191191
if (shouldUpdate()) {
192+
videoOnSetup();
192193
texture.update({ pixels: source });
193194
} else {
194195
texture.shallow();

src/webgl/Texture.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import getSourceDimensions from '../utils/getSourceDimensions.js';
2+
13
const PIXEL = new Uint8Array([0, 0, 0, 255]);
24

35
const isPow2 = value => !(value & (value - 1)) && !!value;
@@ -99,10 +101,9 @@ export default function Texture(gl, textureUnit, optsParam = {}) {
99101
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
100102

101103
if (pixels) {
102-
if (pixels.width === 0 || pixels.height === 0) {
103-
console.warn(
104-
`Texture size is invalid ${pixels.width} x ${pixels.height}. Update is skipped;`
105-
);
104+
const [w, h] = getSourceDimensions(pixels);
105+
if (w === 0 || h === 0) {
106+
console.warn(`Texture size is invalid ${w} x ${h}. Update is skipped;`);
106107

107108
return;
108109
}

0 commit comments

Comments
 (0)