loading.js uses canvas-based animations and sketch instances have their own indicator - #9119
loading.js uses canvas-based animations and sketch instances have their own indicator#9119Jextic wants to merge 4 commits into
Conversation
…have its own loading indicator centered on the canvas
|
@Jextic The test is failing because of https://github.com/processing/p5.js/blob/main/test/unit/core/loading.js#L9 passing |
|
@limzykenneth Thank you. I changed the test files to use mockP5 and mockP5Prototype so the setup should work correctly now. I ran the test and everything passed except for the typography one. |
limzykenneth
left a comment
There was a problem hiding this comment.
One inline and a couple related things:
- Is there a way for the user to opt out of having the default loading animation?
- Currently the loading animation will still draw on top of what the user already drawn in
setup, is there an easy way to avoid it? eg.
async function setup() {
createCanvas(400, 400);
background(200);
ellipse(width / 2, height / 2, 200);
await new Promise((resolve) => setTimeout(resolve, 2000));
}This will draw the loading animation on top of the background and ellipse which may not be ideal. Possibly the capability to disable this would be enough but if it can be done relatively manually it would be great as well.
| if (typeof window === 'undefined') { | ||
| return; | ||
| } | ||
|
|
||
| const canvasParent = this.canvas?.parentElement; | ||
| let container = this._userNode || canvasParent || document.body; | ||
|
|
||
| if (typeof container === 'string') { | ||
| container = document.getElementById(container) || document.body; | ||
| } | ||
|
|
||
| this._loadingIndicator = createLoadingIndicator(container); | ||
| this._isSketchLoading = true; |
There was a problem hiding this comment.
I think if this module explicitly expects to be running in the browser environment, rather than checking window here, it can instead be conditionally registered in main.js based on whether window is defined or not, so the decorator will never be registered if they are not needed as well.
Resolves #8922
Changes:
_createLoadingIndicator()to_createLoadingOverlay._createLoadingOverlaycreates another canvas (which will contain the loading indicator) right on top of the user's canvas (or default canvas)._positionCanvas(),_removeLoadingOverlay(), anddrawLoadingIndicator()were added to help center the loading indicator, remove it when it's no longer loading, and use draw the p5 logo using canvas functions._handleLoadingIndicator()to intercept the canvas whenever canvas-related functions (createCanvas,resizeCanvas, andnoCanvas) are called on.Screenshots of the change:
Video of instance mode sketches:
instance-mode-loading.mp4
This uses the instance mode sketch provided in the issue comment thread plus a modified version of the issue's first sketch example to show that the loading indicator will resize itself respectively to when the canvas resizes.
PR Checklist
npm run lintpasses