* Removes the sketch from the web page. * * Calling `remove()` stops the draw loop and removes any HTML elements * created by the sketch, including the canvas. A new sketch can be * created by using the p5() constructor, as in * `new p5()`. * * @example
()
| 366 | * } |
| 367 | */ |
| 368 | async remove() { |
| 369 | // Remove start listener to prevent orphan canvas being created |
| 370 | if(this._startListener){ |
| 371 | window.removeEventListener('load', this._startListener, false); |
| 372 | } |
| 373 | |
| 374 | if (this._curElement) { |
| 375 | // stop draw |
| 376 | this._loop = false; |
| 377 | if (this._requestAnimId) { |
| 378 | window.cancelAnimationFrame(this._requestAnimId); |
| 379 | } |
| 380 | |
| 381 | // Send sketch remove signal |
| 382 | this._removeAbortController.abort(); |
| 383 | |
| 384 | // remove DOM elements created by p5 |
| 385 | for (const e of this._elements) { |
| 386 | if (e.elt && e.elt.parentNode) { |
| 387 | e.elt.parentNode.removeChild(e.elt); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Run `remove` hooks |
| 392 | await this._runLifecycleHook('remove'); |
| 393 | } |
| 394 | |
| 395 | // remove window bound properties and methods |
| 396 | if (this._isGlobal) { |
| 397 | for (const p in p5.prototype) { |
| 398 | try { |
| 399 | delete window[p]; |
| 400 | } catch (x) { |
| 401 | window[p] = undefined; |
| 402 | } |
| 403 | } |
| 404 | for (const p2 in this) { |
| 405 | if (this.hasOwnProperty(p2)) { |
| 406 | try { |
| 407 | delete window[p2]; |
| 408 | } catch (x) { |
| 409 | window[p2] = undefined; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | p5.instance = null; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | async _runLifecycleHook(hookName) { |
| 418 | await Promise.all(p5.lifecycleHooks[hookName].map(hook => { |
no test coverage detected