Skip to content

Created a loading indicator when the canvas is being set up - #8997

Merged
ksen0 merged 12 commits into
processing:mainfrom
Jextic:issue-8922
Jul 30, 2026
Merged

Created a loading indicator when the canvas is being set up#8997
ksen0 merged 12 commits into
processing:mainfrom
Jextic:issue-8922

Conversation

@Jextic

@Jextic Jextic commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Resolves #8922

Changes:

  • Created loading.js file in p5.js/src/core. loading.js handles the logic for creating a loading indicator when the canvas is being set up. When showLoadingIndicator() is called, a div element with a "Loading..." text will be created. When hideLoadingIndicator() is called, the div element will be removed from the DOM.
  • Modified main.js so that showLoadingIndicator() and hideLoadingIndicator() are called before and after await context.setup(). The loading indicators will only appear while the canvas is loading the setup() function in sketch.js.
  • Created a loading.js unit test file in p5.js/test/unit/core. The test is was created based on the sample code from issue [p5.js 2.0+ Bug Report]: Loading indication #8922 to see if the loading indicator works as intended.

Screenshots of the change:

image image

All tests, including loading.js, pass. The only exception is a typography which always failed, even before changes were made to the project.

PR Checklist

Also worked with @joshin on this issue.

@ksen0

ksen0 commented Jul 21, 2026

Copy link
Copy Markdown
Member

Hi @Jextic thanks for putting this together and sorry for the long time to get to your comment, but please check the comment on the issue. If possible, to keep (1) non text based and (2) decoupled. I will be able to review this more deeply next week as well. Thanks for your work on this!

@Jextic

Jextic commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ksen0,
I decoupled loading.js by registering it as a p5 addon. Now, the loading indicator will be called and removed using lifecycles.presetup and lifecycles.postsetup. I also replaced the "loading..." text with a basic spinning circle animation. The test unit for loading.js was also updated accordingly.

loading-animation-test.mp4

@ksen0

ksen0 commented Jul 22, 2026

Copy link
Copy Markdown
Member

Nice, thanks! Looks good to me.

@ksen0

ksen0 commented Jul 22, 2026

Copy link
Copy Markdown
Member

All tests, including loading.js, pass. The only exception is a typography which always failed, even before changes were made to the project.

Yes that one is known to be flaky, on CI it will pass when rerunning.

Comment thread src/core/loading.js Outdated
if (!document.getElementById('loading-style')) {
const loadingStyle = document.createElement('style');
loadingStyle.id = 'loading-style';
loadingStyle.textContent = '@keyframes loading-spin { to { transform: rotate(360deg); } }';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would like this animation name to be namespaced a bit more, something like p5-loading-spin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I've changed the name from loading-spin to p5-loading-spin in my most recent commits.

Comment thread src/core/loading.js Outdated
lifecycles.presetup = function() {
if (typeof window === 'undefined' || loadingIndicator) return;

const container = this.canvas?.parentElement || document.body;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've not tested this out yet but how does this look on instance mode sketches with contents already on the page and the sketch attached via canvas.parent()?

Also what about multiple sketches in instance mode?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking the time to review. I have tested it out and if there is already existing contents on the page, they won't be affected. A loading indicator will appear during presetup and will only remove itself during postsetup. I also updated presetup so that the loading indicator is now independent for each sketch so it is compatible if there are multiple sketches in instance mode.

I realize that this.canvas?.parentElement might never be called on since the canvas is created during setup() so I removed it. If the sketch is created using new p5(sketch);, a loading indicator will appear at the center of webpage. If the sketch is created with a <div> element as the second parameter (Ex. new p5(sketch, container);), a loading indicator will appear at the center of the <div> container instead. I looked at the p5.js web editor and thought this would be useful in case you wanted to only embed the loading indictor onto the preview. Is there another approach to the loading indicators that you have in mind?

Jextic and others added 4 commits July 28, 2026 12:43
…ing indicator, "this" is referenced so that every p5 instance has their own loading indicator.

Also changed the animation and style names to include "p5"
@ksen0
ksen0 changed the base branch from main to stable July 30, 2026 12:42
@ksen0
ksen0 changed the base branch from stable to main July 30, 2026 12:43
@ksen0

ksen0 commented Jul 30, 2026

Copy link
Copy Markdown
Member

Hello! So after some discussion with @limzykenneth and @davepagurek, I'll go ahead and merge this but keep the issue open. I will also release a patch with this loading indication, so that it's available sooner. However I'll keep the parent issue open and add a comment there describing a followup to address the instance mode comments, which includes using a canvas-based animation rather than css.

@ksen0
ksen0 merged commit b144ffa into processing:main Jul 30, 2026
5 checks passed
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.

[p5.js 2.0+ Bug Report]: Loading indication

3 participants