For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/canvas/3d.md.

Draw in 3D

Draw 3D scenes in a <canvas> with three.js, angular-three or plain WebGL. The canvas has a WebGL2 context on iOS, Android and the web preview. Where it works has the details.

Draw a three.js scene

Give the canvas a size with CSS, and draw once it's laid out:

src/app/scene.ts
import {
  afterNextRender,
  Component,
  DestroyRef,
  type ElementRef,
  inject,
  viewChild,
} from '@angular/core';
import { LYNX_ELEMENTS, type LynxCanvasElement } from '@blotch/angular-lynx';
import * as THREE from 'three';

@Component({
  selector: 'app-scene',
  imports: [LYNX_ELEMENTS],
  template: `<canvas #canvas class="h-[300px] w-full"></canvas>`,
})
export class Scene {
  readonly canvas = viewChild.required<ElementRef<LynxCanvasElement>>('canvas');

  constructor() {
    const destroyRef = inject(DestroyRef);

    afterNextRender(() => {
      const canvas = this.canvas()
        .nativeElement as unknown as HTMLCanvasElement;
      const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
      renderer.setPixelRatio(devicePixelRatio);
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(50, 1, 0.1, 100);
      camera.position.z = 3;
      const cube = new THREE.Mesh(
        new THREE.BoxGeometry(),
        new THREE.MeshNormalMaterial(),
      );
      scene.add(cube);

      // Fit the drawing buffer to the canvas's size.
      const resize = new ResizeObserver(([entry]) => {
        const { width, height } = entry.contentRect;
        renderer.setSize(width, height, false);
        camera.aspect = width / height;
        camera.updateProjectionMatrix();
      });
      resize.observe(canvas);

      renderer.setAnimationLoop(() => {
        cube.rotation.y += 0.01;
        renderer.render(scene, camera);
      });

      // Stop drawing when the screen goes away. Otherwise the loop keeps
      // running, and each visit adds another.
      destroyRef.onDestroy(() => {
        renderer.setAnimationLoop(null);
        resize.disconnect();
        cube.geometry.dispose();
        cube.material.dispose();
        renderer.dispose();
      });
    });
  }
}
  • Stop the loop in onDestroy. setAnimationLoop() runs until you pass it null, even after the component is gone.
  • WebGL2 only. getContext('webgl') (WebGL 1) returns null. three.js needs WebGL2 anyway.
  • One kind of context per canvas. After getContext('2d'), getContext('webgl2') is null, and the other way round.
  • The build includes WebGL2 only if a module names 'webgl2'. three.js does. See What the build includes.

Use angular-three

angular-three works with its own renderer, as on the web:

src/app/app.config.ts
import { provideRenderer } from '@blotch/angular-lynx';
import { provideNgtRenderer } from 'angular-three/dom';

export const appConfig = {
  providers: [provideRenderer(), provideNgtRenderer()],
};
  • provideRenderer() comes first. angular-three's renderer wraps Angular's, which on Lynx is AngularLynx's.
  • <ngt-canvas> sizes itself.
  • Its events come from touches, such as (click) and (pointerover) on a mesh.
  • crypto.randomUUID(), which AngularLynx adds for angular-three, isn't cryptographically secure. Don't use it for secrets.
Warning

angular-three 4.2 lists Angular 20 and 21, and three.js below 0.183, as its peer dependencies. On Angular 22, install it with npm install angular-three --legacy-peer-deps, or add an overrides entry. Pin three to 0.182.x.

Load textures and models

Textures and models load as on the web. These three.js loaders work:

  • TextureLoader (an Image)
  • ImageBitmapLoader (createImageBitmap())
  • GLTFLoader, including .glb files with their textures inside

They load from:

  • https:// URLs
  • data: URLs
  • object URLs (URL.createObjectURL())
  • files your app bundles, by their path in dist/

To bundle a model, copy it in with output.copy in lynx.config.ts. Then load '/assets/models/frame.glb'. A build has no loader for import model from './frame.glb'.

The phone decodes images, and the pixels go straight to the GPU. A canvas, drawn in 2D or with WebGL, uploads as a texture too (CanvasTexture, or texImage2D() with the canvas). The phone reads its pixels and hands them to the GPU.

Warning

Loaders that need Web Workers or WebAssembly, such as DRACOLoader and KTX2Loader, don't work.

For videos on a mesh, see Draw a video frame.

Keep it smooth

Your scene's JavaScript runs on the thread Lynx lays the page out on. Each frame's WebGL calls go to the phone's GPU together, once a frame.

A scene of a few meshes, lit and shadowed, draws at 60 frames a second in the iOS Simulator and on the Android emulator. To keep it there:

  • Keep the drawing buffer to the screen's pixel ratio, at most 2.
  • Stop the loop when the scene is off screen: setAnimationLoop(null).

The first time an app runs after it's installed, each material's shaders compile when it's first drawn. That's up to 2 seconds for the first one in the iOS Simulator. Later launches reuse them.

Tip

The iOS Simulator and the Android emulator both draw on the Mac's GPU. A scene that's smooth there is a fair guide. Still, check frame rates on a phone before you ship: a phone's GPU is slower than a Mac's.

When the context is lost

A canvas fires webglcontextlost when WEBGL_lose_context's loseContext() is called. three.js does that when it's disposed. A lost context isn't restored: make a new renderer.