Lazy-loading three.js only when it is visible

Defer a decorative three.js scene until it is near the viewport, keep its code out of the eager bundle and manage loading, animation and cleanup separately.

A decorative footer scene doesn’t need to compete with the first screen. Some visitors will never scroll far enough to see it.

A footer implementation can load its scene module when the canvas approaches the viewport, after the page load event. It’s a useful example of separating a page’s content from a nonessential effect.

Delaying initialisation isn’t enough

If the main script imports three.js eagerly, postponing the call that creates the scene saves some execution work but not the library download.

A dynamic import gives the bundler a point to split the code. Verify the actual output and the request timeline, because shared dependencies, preloads and other imports can change when the browser receives each chunk.

Inspect the assets as well. Textures and environment maps can outweigh the module that loads them. A delayed JavaScript file is only part of the loading policy.

Watch before the scene becomes visible

An IntersectionObserver can start loading shortly before the canvas reaches the viewport. The margin gives the browser time to fetch and initialise the effect.

This example assumes your own footer-scene.js exports an async mountScene function and that the canvas is decorative:

const canvas = document.querySelector('[data-footer-scene]');

if (canvas && !matchMedia('(prefers-reduced-motion: reduce)').matches) {
  const observer = new IntersectionObserver((entries) => {
    if (!entries.some((entry) => entry.isIntersecting)) return;
    observer.disconnect();

    import('./footer-scene.js')
      .then(({ mountScene }) => mountScene(canvas))
      .catch(() => {
        canvas.hidden = true;
      });
  }, { rootMargin: '200px 0px' });

  observer.observe(canvas);
}

The example hides a failed decoration, which is fine for decorative content and not for anything essential. Add a suitable fallback and lifecycle handling for your application. It also needs a different strategy if your supported browsers lack IntersectionObserver.

Short pages are an important edge case

On a short contact page, the footer may already be visible when the document opens. Near-viewport loading alone would then start immediately.

That approach combines viewport proximity with waiting for the page load and scheduling at low priority. That expresses a priority choice: the effect is allowed to wait. It doesn’t guarantee that the later work is cheap or that it can’t cause a long task.

Tune the loading margin on real devices and assets. A wide margin loads sooner; a narrow one risks a visible wait. A placeholder should make either outcome look intentional.

Downloading once and rendering continuously are different costs

After loading, pause the decorative animation when it’s offscreen or the document is hidden. Keep one animation loop per scene and make resuming predictable.

Reduced motion can use a static alternative or leave out a nonessential scene. If preferences change while the page is open, decide how the active effect responds.

Rendering resolution matters too. Doubling each buffer dimension creates four times as many pixels. Set the resolution to what the effect needs, rather than automatically using the highest screen density available. The WebGL article covers the same resolution trade-off for a raw shader.

Dispose when the scene goes away

Disconnect observers and remove listeners when the component is destroyed. Stop the animation loop. Release the three.js geometries, materials, textures and renderer resources, because removing a canvas from the DOM doesn’t do that work for you.

This matters most with client-side navigation or repeated modal mounts. A full page reload can hide leaks that show up during a long session.

Verify what changed

Compare the first-load Network panel before and after the split. Confirm that the scene chunk and its assets stay absent until the loading conditions are met. Then scroll quickly to the footer and test the failure path.

Record the early JavaScript transfer and the experience of the scene itself. A later download is a useful prioritisation improvement, but it isn’t the same as reducing the total work for visitors who do view the effect.

References

  • Performance