Skip to content

Light version: pages open quickly, without the 3D graphics. You are seeing the light version because the 3D graphics would be slow on this device. The content is the same.

Turn on 3D

Preparing the graphics. This can take a moment on the first visit.

Henry Lõiv

Web design & development

GuideDevelopment 3 min read

Three.js in Vue: dispose resources when a view unmounts

Stop animation, remove the canvas and dispose owned Three.js resources when a Vue view unmounts. Download a tested example with resize and high-DPI handling.

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⁠

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.

Written by Henry Lõiv

Keep reading

All articles⁠
  1. Best AI agents in 2026: Grok Bot vs Dots and EU availability Compare AI agents by the work they can handle and whether you can use them in Estonia or the EU. Artificial Intelligence 5 min read
  2. AI Agent, Chatbot or Automation: What Does Your Business Need? A chatbot is an interface; an agent is one way to handle the work behind it. Follow a fictional customer enquiry to choose a practical system for your business. Artificial Intelligence 8 min read
  3. Claude Code vs Codex for Frontend Development: A Practical Comparison A documentation-based comparison with a runnable frontend exercise and a blank scorecard. No measured winner is claimed. Artificial Intelligence 9 min read

Hi!

I really love the sea, yachts, sunsets and starry skies.

Through this little 3D world, I can share a small part of myself with you.

Thank you for opening this bottle and reading my letter.

Henry

Let’s talk about your project

Tell me about your project

Tell me what you’d like to build or improve. You don’t need a finished brief — we’ll work out the scope and next steps together.

01 How can I help?
02 How many pages do you need?
03 Which extras do you need? Choose all that apply
04 When does it need to be ready?
05 Project details
06 Your contact details

Enquiry received

Thank you!

Your enquiry has been received. I usually reply by email within 2 working days.