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

Fix: prevent unnecessary webgl canvas recreation in noSmooth() #7557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HarshitaKatariya
Copy link

Resolve #7548

Issue
Calling noSmooth() inside draw() in WebGL mode recreates the canvas because setAttributes('antialias', false) is called every time. This causes unexpected behavior, including loss of canvas positioning and state.

Fix:
Before setting antialias to false, we check if it's already false.
If it's already disabled, we skip calling setAttributes(), preventing unnecessary canvas recreation.

Why This Fix?
Optimizes performance: Avoids reinitializing the WebGL canvas every frame.
Prevents unintended side effects: Keeps canvas position and other properties intact.
Aligns with expected behavior: In 2D mode, noSmooth() doesn’t recreate the canvas, so WebGL should behave similarly.

Before
Output with noSmooth()
Not display any output..

Output without noSmooth()
After

After
After fix issue.
Output with noSmooth() as well as without noSmooth().
After

Alternative Solution Considered
Updating the documentation to explicitly state that calling noSmooth() after initialization will recreate the canvas. This can still be done in addition to this fix.

Testing
✅ Verified in both 2D and WebGL renderers.
✅ noSmooth() works correctly in setup() and draw().
✅ No canvas recreation occurs unless truly necessary.

@davepagurek
Copy link
Contributor

Thanks for making a PR! We're currently working on developing p5 2.0, which is in the dev-2.0 branch and not the main branch, and have temporarily frozen merges into main. Could you base your changes off of that branch and make your PR's base branch dev-2.0?

@HarshitaKatariya
Copy link
Author

Oh, yup! I forgot about that. I'll do that right away. Thanks for the heads-up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

noSmooth() breaks positioned WEBGL canvas
2 participants