diff --git a/src/core/p5.Renderer3D.js b/src/core/p5.Renderer3D.js index 8d96778fac..a335b878cf 100644 --- a/src/core/p5.Renderer3D.js +++ b/src/core/p5.Renderer3D.js @@ -728,6 +728,27 @@ export class Renderer3D extends Renderer { } background(...args) { + const a0 = args[0]; + + const isImageLike = + a0 != null && + typeof a0 === 'object' && + typeof a0.width === 'number' && + typeof a0.height === 'number' && + (a0.canvas != null || a0.elt != null); + + // WEBGL / 3D: support background(image-like) + if (isImageLike) { + this._pInst.clear(); + this._pInst.push(); + this._pInst.resetMatrix(); + this._pInst.imageMode(constants.CENTER); + this._pInst.image(a0, 0, 0, this._pInst.width, this._pInst.height); + this._pInst.pop(); + return; + } + + // Default: background(color) const _col = this._pInst.color(...args); this.clear(..._col._getRGBA()); } diff --git a/test/unit/visual/cases/webgl.js b/test/unit/visual/cases/webgl.js index b0a68e56d6..c2157b3d18 100644 --- a/test/unit/visual/cases/webgl.js +++ b/test/unit/visual/cases/webgl.js @@ -929,4 +929,22 @@ visualSuite('WebGL', function() { screenshot(); }); }); + + visualSuite('background()', function () { + visualTest('background(image) works in WEBGL', function (p5, screenshot) { + p5.createCanvas(50, 50, p5.WEBGL); + + const g = p5.createGraphics(50, 50); + g.background(255, 0, 0); + g.fill(0); + g.noStroke(); + g.circle(25, 25, 20); + + p5.background(0, 0, 255); + p5.background(g); + + screenshot(); + }); + }); + }); diff --git a/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/000.png b/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/000.png new file mode 100644 index 0000000000..dc1feedc07 Binary files /dev/null and b/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/000.png differ diff --git a/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/metadata.json b/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/metadata.json new file mode 100644 index 0000000000..2d4bfe30da --- /dev/null +++ b/test/unit/visual/screenshots/WebGL/background()/background(image) works in WEBGL/metadata.json @@ -0,0 +1,3 @@ +{ + "numScreenshots": 1 +} \ No newline at end of file