* Destroy this application instance and release all associated resources. * Removes the canvas from the DOM, destroys the world, and unregisters * all event listeners. * @param removeCanvas - if true, the canvas element is removed from the DOM (default: true) * @example * // clean up when
(removeCanvas: boolean = true)
| 758 | * app.destroy(); |
| 759 | */ |
| 760 | destroy(removeCanvas: boolean = true): void { |
| 761 | // stop the render loop and remove all event listeners |
| 762 | /* eslint-disable @typescript-eslint/unbound-method */ |
| 763 | off(TICK, this._tick, this); |
| 764 | off(BLUR, this._onBlur, this); |
| 765 | off(FOCUS, this._onFocus, this); |
| 766 | off(STATE_CHANGE, this.repaint, this); |
| 767 | off(STATE_RESTART, this.repaint, this); |
| 768 | off(STATE_RESUME, this.repaint, this); |
| 769 | off(STAGE_RESET, this.reset, this); |
| 770 | /* eslint-enable @typescript-eslint/unbound-method */ |
| 771 | |
| 772 | // remove DOM event listeners |
| 773 | if (this._onResize) { |
| 774 | globalThis.removeEventListener("resize", this._onResize); |
| 775 | globalThis.removeEventListener( |
| 776 | "orientationchange", |
| 777 | this._onOrientationChange!, |
| 778 | ); |
| 779 | globalThis.removeEventListener("scroll", this._onScroll!); |
| 780 | if (device.screenOrientation) { |
| 781 | globalThis.screen.orientation.onchange = null; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // destroy the world and all its children |
| 786 | if (this.world) { |
| 787 | this.world.destroy(); |
| 788 | } |
| 789 | |
| 790 | // tear down the renderer's batchers so their `event.on(...)` |
| 791 | // subscriptions (e.g. `GPU_TEXTURE_CACHE_RESET` on |
| 792 | // `MaterialBatcher`) don't leak across the disposal — every |
| 793 | // otherwise-discarded `Application` would otherwise keep the |
| 794 | // batchers (and their renderer reference) alive for the rest |
| 795 | // of the page's lifetime. |
| 796 | this.renderer?.destroy(); |
| 797 | |
| 798 | // remove the canvas from the DOM |
| 799 | if (removeCanvas && this.renderer) { |
| 800 | const canvas = this.renderer.getCanvas(); |
| 801 | if (canvas.parentElement) { |
| 802 | canvas.parentElement.removeChild(canvas); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | this.isInitialized = false; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * force the redraw (not update) of all objects |
nothing calls this directly
no test coverage detected