A Three.js scene can disappear from the screen while its animation loop, resize listener and GPU resources stay allocated. This practical guide shows how to give a view-owned renderer a clear lifecycle in Vue 3. It also explains why these cleanup steps change when your application shares one renderer across routes.
Outcome: mount a scene, navigate away or toggle the view, and confirm owned resources are released. Requirements: Vue 3, Three.js and a browser with WebGL. Plan around 40 minutes.
1. Identify who owns each resource
For a view-owned scene, the component owns the renderer, canvas, geometry, material, any textures/render targets it creates, animation loop and event listeners. When the view ends, these should end too. For an app-level shared renderer, the parent app owns the renderer; do not dispose it on every child route change.
2. Create on mount, stop on unmount
import { onMounted, onUnmounted, ref } from 'vue'
import * as THREE from 'three'
const container = ref(null)
let renderer, scene, geometry, material, raf = 0
function animate() {
raf = requestAnimationFrame(animate)
renderer.render(scene, camera)
}
onMounted(() => {
scene = new THREE.Scene()
geometry = new THREE.BoxGeometry()
material = new THREE.MeshNormalMaterial()
scene.add(new THREE.Mesh(geometry, material))
renderer = new THREE.WebGLRenderer({ antialias: true })
container.value.appendChild(renderer.domElement)
// Define camera, size, resize handling, then start animation.
})
onUnmounted(() => {
cancelAnimationFrame(raf)
window.removeEventListener('resize', handleResize)
geometry.dispose()
material.dispose()
renderer.dispose()
renderer.domElement.remove()
})
Illustrative fragment: camera and handleResize are intentionally omitted here. Use the complete working source inside threejs-vue-lifecycle-demo.zip, not this shortened fragment, for copy-paste implementation.
3. Dispose what Three.js does not dispose automatically
Removing objects from the scene does not dispose geometries, materials or textures. Dispose BufferGeometry, Material, textures (including material-specific maps), render targets and extra controls/post-processing passes when you own them. Shared textures or skeletons must be disposed by their actual owner. If an ImageBitmap is owned by your app, its CPU-side resource may require close() separately.
4. Check resizing and high-density screens
Keep renderer size synchronized with the canvas container. Limit pixel ratio for performance where appropriate, for example Math.min(window.devicePixelRatio, 2) based on a measured tradeoff. A ResizeObserver on the container is often more precise than window resize alone; disconnect it on unmount.
5. Prove cleanup with repeated cycles
Start the provided example in a WebGL-capable browser. Toggle the scene 10–20 times. Confirm one canvas appears while active and none remains after unmount. Verify that no animation loop keeps running, no resize observer remains and graphics resources are released. renderer.info.memory is a diagnostic indicator, not a complete GPU memory accounting, and may retain internal caches.
If you use KeepAlive, a hidden view may be deactivated rather than unmounted: explicitly decide whether to pause rendering in onDeactivated and resume in onActivated. For asynchronous asset loaders, guard against late callbacks after disposal.
Official sources: Three.js disposal · Vue lifecycle hooks.
Downloads and next steps
Download the complete Vue + Three.js lifecycle demo. With Node 22.12+ or a supported newer release, run npm ci, npm run check:source, npm run build, then npm run dev. This component owns its renderer. Router transitions, KeepAlive and late asynchronous loaders need separate tests in your application.
Verified on macOS in Chromium 156.0.8078.4 on 12 November 2026: 12 mount/unmount cycles, resizing from desktop to a 390 px viewport, and device pixel ratio 2. Each unmount left zero canvases, scheduled animation callbacks, active resize observers, tracked WebGL buffers and programs; draw calls stopped. No JavaScript errors were observed. This is an instrumented lifecycle check, not a complete GPU-memory or performance benchmark. Router, KeepAlive and late async-loader paths were not exercised.
Building a feature for your web application?
The useful part of a technical approach is how it works for your users. I build interfaces, connect them to existing APIs and help product teams improve features, including loading, empty and error states.
Explore frontend development for your application
Related work and examples
Dashboard
A COVID data explorer grew into a personal dashboard and a collection of everyday web tools. A long-running React project where I learned to connect interfaces, interactive charts and real data.