Skip to content

Commit d455330

Browse files
committed
fix background(image) support in WEBGL #7917
1 parent ff2e5c3 commit d455330

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/color/setting.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,34 @@ 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)
669697
this._renderer.background(...args);
670698
return this;
671699
};

test/unit/color/setting.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ suite('color/Setting', function() {
163163
});
164164
});
165165

166+
suite('p5.prototype.background (WEBGL)', function() {
167+
test('does not throw when given an image-like argument', function() {
168+
const g = my3D.createGraphics(32, 32);
169+
g.background(255, 0, 0);
170+
171+
assert.doesNotThrow(function() {
172+
my3D.background(g);
173+
});
174+
});
175+
});
176+
166177
suite('p5.prototype.colorMode', function() {
167178
test('should be a function', function() {
168179
assert.ok(myp5.colorMode);

0 commit comments

Comments
 (0)