Web engineering

The canvas loads last: adding WebGL to a page that already works

On this site the WebGL orb arrives last: after the text, after first paint, and only once the browser is idle. That load order is most of the method.

A WebGL scene can fail for reasons the page can't control: a blocklisted GPU, a driver reset, an old phone in a tunnel. We plan for the scene to be missing, and treat it as a bonus.

Write the page as if the canvas never arrives

Headline, copy, navigation and the next step are static HTML in the first response. The canvas never carries words: no headline drawn into a texture, no text injected by the scene's script.

The test: turn JavaScript off and load the page. Our colophon claims 52 public pages readable without JavaScript; if one needed the scene, that would be false.

Mount on idle, and only in view

The homepage orb starts as a stand-in holding a drawn CSS orb. The live version arrives by dynamic import(), once an IntersectionObserver sees the stand-in and the browser reports idle time:

const orbs = document.querySelectorAll('[data-orb]');
const load = () => import('./orb.js').then((m) => m.mountOrbs(orbs)).catch(() => {});
const io = new IntersectionObserver((entries) => {
  if (!entries.some((e) => e.isIntersecting)) return;
  io.disconnect();
  ('requestIdleCallback' in window ? requestIdleCallback : setTimeout)(load, { timeout: 1200 });
});
orbs.forEach((o) => io.observe(o));

The timeout matters: a page that never goes idle still gets its scene after 1.2 seconds.

The CSS orb stays until the live one has drawn two frames, then a class swaps them. Nothing shifts, and the Largest Contentful Paint element is always text.

Choose the size once, by device

We set density at mount and keep it for the visit. Four cores or fewer, 4 GB of memory or less, or a coarse pointer get 3,200 particles; everything else gets 6,400.

const modest = (navigator.hardwareConcurrency || 8) <= 4
  || (navigator.deviceMemory || 8) <= 4
  || matchMedia('(pointer: coarse)').matches;
el.setAttribute('particles', modest ? '3200' : '6400');

The adapted orb component can cut particles by 40% after 30 slow frames in a row. We pass a fixed count to turn that off: a layer that thins out mid-visit looks like a fault.

deviceMemory exists only in Chromium, so the fallback decides what Safari and Firefox get. Ours defaults to the larger scene. Pick a default you can defend and write it down.

Cap the pixel ratio too: 2× for the orb, 1.25× for the canvas art on our service pages. Soft gradients gain little from extra resolution, and fill cost grows with its square.

Decorative canvases are hidden from assistive technology

Every canvas here carries aria-hidden="true". We don't write alt text for ambience; the HTML already says what the scene only alludes to.

The rule flips when a scene carries information. A configurator or data view is content, so it needs an accessible name and an HTML equivalent. If you can't write one, the scene is decoration.

Paint the still with the same code

The orb art on our service pages is painted in code by one function, paintStudy(). The function that animates it can also draw a single frame at any moment, so the still and the moving version cannot drift apart.

So the fallback is the design at rest: no second artwork to sync, no grey box when the canvas fails or motion is reduced.

Put contrast in CSS, never in the shader. If a headline only reads because the scene darkens behind it, the still loses the headline too.

Stop, release, survive a lost context

An idle scene should cost nothing. Our orb stops its loop off screen or on a hidden tab, and releases its context through WEBGL_lose_context on leaving the page. Pause motion removes the live orb and keeps the drawn one.

Browsers also drop contexts on their own, after a GPU process restart for example. A scene with state worth rebuilding should handle both events:

canvas.addEventListener('webglcontextlost', (e) => {
  e.preventDefault();               // lets the browser restore it
  stopLoop();
  host.classList.remove('is-live'); // the still shows again
});
canvas.addEventListener('webglcontextrestored', () => {
  rebuildGpuResources();            // buffers, textures, programs
  startLoop();
});

A decorative layer can skip restoring and let the still stand, if the still is a finished design.

Our signal orb Lab page lists what we changed in the adapted component.

Before the scene ships

  • With JavaScript off, the page reads and the next step is reachable.
  • The Largest Contentful Paint element is HTML text or a poster image, never the canvas.
  • The scene arrives by dynamic import() after first paint, with an idle timeout.
  • Density and pixel-ratio cap are set per device, with the deviceMemory fallback written down.
  • Decorative canvases carry aria-hidden="true"; informative ones have an HTML equivalent.
  • The loop stops off screen, on a hidden tab, under prefers-reduced-motion: reduce and when paused.
  • A forced context loss leaves a page that looks finished.

We scope scenes like this in our creative technology service. The cost side is in Motion is a budget, and most pages get almost none.

  • WebGL
  • Progressive enhancement
  • Three.js
  • Resilience

Written by

SIMONVILSTECH Studio

Notes from the studio’s practice in web engineering, motion and real-time interfaces, led by SIMONVILDONELOHIM.

Turn this into a project brief

Tell us what you need and by when. We read every enquiry and reply in writing, with questions, a next step, or a plain no if we’re the wrong studio.