From f2e2a6ff59b3c4a6a670fdce59836132055096ec Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 6 Mar 2024 16:54:46 +0100 Subject: [PATCH] Add Teardown code --- src/components/3d/PublicPreview.svelte | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/components/3d/PublicPreview.svelte b/src/components/3d/PublicPreview.svelte index 03183c3..87bfa44 100644 --- a/src/components/3d/PublicPreview.svelte +++ b/src/components/3d/PublicPreview.svelte @@ -21,7 +21,7 @@ import * as THREE from "three"; import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js"; import { OrbitControls } from "three/addons/controls/OrbitControls.js"; - import {onMount} from "svelte"; + import {onDestroy, onMount} from "svelte"; export let file: string; export let fov: number = 60; @@ -72,18 +72,29 @@ new ResizeObserver(handleResize).observe(div); + + loaded = true; + function animate() { - requestAnimationFrame( animate ); + if (loaded) { + requestAnimationFrame(animate); + } renderer.render( scene, camera ); controls.update(); } animate(); - - loaded = true; }); + onDestroy(() => { + loaded = false; + + renderer.dispose(); + scene.clear(); + light.dispose(); + }) + function handleResize() { let width = div.clientWidth; let height = div.clientHeight;