Skip to content

Commit efe2ca2

Browse files
committed
move webgl background(image) handling to renderergl
1 parent 861031c commit efe2ca2

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/color/setting.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -666,34 +666,6 @@ p5.prototype.clip = function(callback, options) {
666666
* @chainable
667667
*/
668668
p5.prototype.background = function(...args) {
669-
670-
// WEBGL: support background(image)
671-
if (this._renderer && this._renderer.isP3D && args.length > 0) {
672-
const img = args[0];
673-
674-
const isImageLike =
675-
img instanceof p5.Image ||
676-
img instanceof p5.Graphics ||
677-
(typeof HTMLImageElement !== 'undefined' && img instanceof HTMLImageElement) ||
678-
(typeof HTMLVideoElement !== 'undefined' && img instanceof HTMLVideoElement) ||
679-
(typeof p5.MediaElement !== 'undefined' && img instanceof p5.MediaElement);
680-
681-
if (isImageLike) {
682-
// Clear WebGL buffers
683-
this.clear();
684-
685-
// Draw image in screen space
686-
this.push();
687-
this.resetMatrix();
688-
this.imageMode(this.CENTER);
689-
this.image(img, 0, 0, this.width, this.height);
690-
this.pop();
691-
692-
return this;
693-
}
694-
}
695-
696-
// Default behavior (2D + color backgrounds)
697669
this._renderer.background(...args);
698670
return this;
699671
};

src/webgl/p5.RendererGL.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,30 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
934934
* [background description]
935935
*/
936936
background(...args) {
937+
const a0 = args[0];
938+
939+
const isImageLike =
940+
a0 instanceof p5.Image ||
941+
a0 instanceof p5.Graphics ||
942+
(typeof HTMLImageElement !== 'undefined' && a0 instanceof HTMLImageElement) ||
943+
(typeof HTMLVideoElement !== 'undefined' && a0 instanceof HTMLVideoElement) ||
944+
(typeof p5.MediaElement !== 'undefined' && a0 instanceof p5.MediaElement);
945+
946+
// WEBGL: support background(image)
947+
if (args.length > 0 && isImageLike) {
948+
// Clear WebGL buffers (color + depth)
949+
this._pInst.clear();
950+
951+
// Draw background image in screen space (ignore camera)
952+
this._pInst.push();
953+
this._pInst.resetMatrix();
954+
this._pInst.imageMode(this._pInst.CENTER);
955+
this._pInst.image(a0, 0, 0, this._pInst.width, this._pInst.height);
956+
this._pInst.pop();
957+
return;
958+
}
959+
960+
// Default WEBGL background(color)
937961
const _col = this._pInst.color(...args);
938962
const _r = _col.levels[0] / 255;
939963
const _g = _col.levels[1] / 255;

0 commit comments

Comments
 (0)