-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix background(img) handling in WEBGL mode in RendererGL #7963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -934,12 +934,24 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
* [background description] | ||
*/ | ||
background(...args) { | ||
const _col = this._pInst.color(...args); | ||
const _r = _col.levels[0] / 255; | ||
const _g = _col.levels[1] / 255; | ||
const _b = _col.levels[2] / 255; | ||
const _a = _col.levels[3] / 255; | ||
this.clear(_r, _g, _b, _a); | ||
// If the first argument is an image, draw it as background | ||
if (args.length === 1 && args[0] instanceof p5.Image) { | ||
const img = args[0]; | ||
this.clear(); // clear previous drawing | ||
this.push(); | ||
this._renderer._setDefaultCamera(); // reset camera to default so image fits canvas | ||
imageMode(CENTER); | ||
image(img, this.width / 2, this.height / 2, this.width, this.height); | ||
this.pop(); | ||
} else { | ||
// Else treat it as color input (default behavior) | ||
const _col = this._pInst.color(...args); | ||
const _r = _col.levels[0] / 255; | ||
const _g = _col.levels[1] / 255; | ||
const _b = _col.levels[2] / 255; | ||
const _a = _col.levels[3] / 255; | ||
this.clear(_r, _g, _b, _a); | ||
} | ||
} | ||
|
||
////////////////////////////////////////////// | ||
|
@@ -2545,4 +2557,24 @@ p5.prototype._assert3d = function (name) { | |
|
||
p5.RendererGL.prototype.tessyVertexSize = 12; | ||
|
||
p5.RendererGL.prototype.background = function (...args) { | ||
if (args.length === 1 && args[0] instanceof p5.Image) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are some other image-like things that we probably want to work here, e.g. |
||
const img = args[0]; | ||
this.clear(); // clear previous frame | ||
this._pInst.push(); | ||
this._renderer._setDefaultCamera(); // reset camera | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
this._pInst.imageMode(this._pInst.CENTER); | ||
this._pInst.image(img, 0, 0, this.width, this.height); // centered on WEBGL canvas | ||
this._pInst.pop(); | ||
} else { | ||
const _col = this._pInst.color(...args); | ||
const _r = _col.levels[0] / 255; | ||
const _g = _col.levels[1] / 255; | ||
const _b = _col.levels[2] / 255; | ||
const _a = _col.levels[3] / 255; | ||
this.clear(_r, _g, _b, _a); | ||
} | ||
}; | ||
|
||
|
||
export default p5.RendererGL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we have a duplicate implementation here that overrides the method inside the class above, are we able to merge this with the implementation above? It looks like this is the code currently being executed, so we could replace the implementation above with the body of this function and then remove it.