Skip to content

Commit

Permalink
Merge pull request #6355 from limzykenneth/orphan-canvas
Browse files Browse the repository at this point in the history
Fix orphan canvas when sketch is removed before canvas creation
  • Loading branch information
limzykenneth authored Oct 13, 2023
2 parents 0f9f832 + 04f2c8c commit bd770c4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import './shim';

// Core needs the PVariables object
import * as constants from './constants';

/**
* This is the p5 instance constructor.
*
Expand Down Expand Up @@ -178,6 +177,7 @@ class p5 {
this._preloadCount = 0;
this._isGlobal = false;
this._loop = true;
this._startListener = null;
this._initializeInstanceVariables();
this._defaultCanvasSize = {
width: 100,
Expand Down Expand Up @@ -470,6 +470,10 @@ class p5 {
*
*/
this.remove = () => {
// Remove start listener to prevent orphan canvas being created
if(this._startListener){
window.removeEventListener('load', this._startListener, false);
}
const loadingScreen = document.getElementById(this._loadingScreenId);
if (loadingScreen) {
loadingScreen.parentNode.removeChild(loadingScreen);
Expand Down Expand Up @@ -604,7 +608,8 @@ class p5 {
if (document.readyState === 'complete') {
this._start();
} else {
window.addEventListener('load', this._start.bind(this), false);
this._startListener = this._start.bind(this);
window.addEventListener('load', this._startListener, false);
}
}

Expand Down

0 comments on commit bd770c4

Please sign in to comment.