Skip to content

Commit 8e87a23

Browse files
authored
Merge pull request #8572 from processing/fix/mediaelement-texture-perf
Avoid unnecessary texture copies and fix flipped webcams in WebGL
2 parents 8773a49 + 23cb69f commit 8e87a23

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/dom/p5.MediaElement.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,13 @@ class MediaElement extends Element {
713713
duration() {
714714
return this.elt.duration;
715715
}
716+
_checkIfTextureNeedsUpdate() {
717+
const needsUpdate = this._frameOnTexture !== this._pInst.frameCount;
718+
if (needsUpdate) {
719+
this.setModified(true);
720+
this._frameOnTexture = this._pInst.frameCount;
721+
}
722+
}
716723
_ensureCanvas() {
717724
if (!this.canvas) {
718725
this.canvas = document.createElement('canvas');
@@ -1647,13 +1654,13 @@ function media(p5, fn){
16471654
if (domElement.width) {
16481655
videoEl.width = domElement.width;
16491656
videoEl.height = domElement.height;
1650-
if (flipped) {
1651-
videoEl.elt.style.transform = 'scaleX(-1)';
1652-
}
16531657
} else {
16541658
videoEl.width = videoEl.elt.width = domElement.videoWidth;
16551659
videoEl.height = videoEl.elt.height = domElement.videoHeight;
16561660
}
1661+
if (flipped) {
1662+
videoEl.elt.style.transform = 'scaleX(-1)';
1663+
}
16571664
videoEl.loadedmetadata = true;
16581665

16591666
if (callback) callback(domElement.srcObject);

src/webgl/p5.Texture.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,18 @@ class Texture {
102102
this.isSrcMediaElement ||
103103
this.isSrcHTMLElement
104104
) {
105-
// if param is a video HTML element
106-
if (this.src._ensureCanvas) {
105+
// createCapture elements that are flipped need
106+
// to go through a canvas
107+
if (this.isSrcMediaElement && this.src.flipped) {
107108
this.src._ensureCanvas();
109+
textureData = this.src.canvas;
110+
} else {
111+
// if param is a video HTML element
112+
if (this.src._checkIfTextureNeedsUpdate) {
113+
this.src._checkIfTextureNeedsUpdate();
114+
}
115+
textureData = this.src.elt;
108116
}
109-
textureData = this.src.elt;
110117
} else if (this.isSrcP5Graphics || this.isSrcP5Renderer) {
111118
textureData = this.src.canvas;
112119
} else if (this.isImageData) {

0 commit comments

Comments
 (0)