Skip to content
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

Refactored the createGraphics function in rendering.js #6557

Merged
16 changes: 10 additions & 6 deletions src/core/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,17 @@ p5.prototype.noCanvas = function() {
* @param {HTMLCanvasElement} [canvas]
* @return {p5.Graphics} offscreen graphics buffer
*/
p5.prototype.createGraphics = function(w, h, renderer, canvas) {
if (arguments[2] instanceof HTMLCanvasElement) {
renderer = constants.P2D;
canvas = arguments[2];
p5.prototype.createGraphics = function(w, h, ...args) {
/**
* args[0] is expected to be renderer
* args[1] is expected to be canvas
*/
if (args[0] instanceof HTMLCanvasElement) {
args[1] = args[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a bug: args[1] will also be constants.P2D because you are setting it equal to args[0] after resetting that to be constants.P2D.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! I was thinking the same. Maybe using a temporary variable could do the trick? What do you reckon?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, or alternatively just setting args[1] before args[0].

args[0] = constants.P2D;
}
p5._validateParameters('createGraphics', arguments);
return new p5.Graphics(w, h, renderer, this, canvas);
p5._validateParameters('createGraphics', [w, h, ...args]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing, I think we might want to leave this one as arguments. I think the parameter validation method is intended to make sure what the user passes in matches the signatures in the docs. If something doesn't match, we want to give an error that corresponds to what the user has actually typed, not how we rearrange things, to avoid confusion.

return new p5.Graphics(w, h, args[0], this, args[1]);
};

/**
Expand Down