Skip to content

Add a read-only flag to indicate if the current canvas is Web GL #5844

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ p5.prototype.createCanvas = function(w, h, renderer) {
}
this._renderer.resize(w, h);
this._renderer._applyDefaults();

//isWebGL flag is set and made read-only
if (this.isWebGL == false) {
Object.defineProperty(this,"isWebGL" , {
value: this._renderer.drawingContext instanceof WebGLRenderingContext,
Copy link
Contributor

@davepagurek davepagurek Oct 27, 2022

Choose a reason for hiding this comment

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

If we use a get accessor here instead of value directly, then we can put this in the constructor for p5 and not have to worry about handling createCanvas not being called or being called more than once, since the value will be determined at runtime. i.e., Object.defineProperty(this, 'webgl', { get: () => this._renderer.drawingContext instanceof WebGLRenderingContext, writable: false }).

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also add the same accessor to the p5.Graphics constructor?

writable: false
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, it looks like the spacing and equal signs aren't quite lined up here. Can you double check that npm run lint passes after making changes?

//createCanvas is run automatically with the 2D renderer.
//Auto-set isWebGL to false if undefined because of this
if (this.isWebGL === undefined) {
this.isWebGL = false;
}

return this._renderer;
};

Expand Down