> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# `<canvas>`

A drawing surface with a WebGL2 context, for 3D scenes with [three.js](https://threejs.org/), [angular-three](https://github.com/angular-threejs/angular-three) or plain [WebGL](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API). Lynx has no canvas of its own: this one is AngularLynx's.


## Where it works

| Where                                                                                      | `getContext('webgl2')`                                                                                                    |
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| iOS apps built with [`ng build --platform ios`](/guide/building-for-production.md)         | Draws with OpenGL ES 3.0 on Metal, through [ANGLE](https://chromium.googlesource.com/angle/angle), as Safari's WebGL does |
| Android apps built with [`ng build --platform android`](/guide/building-for-production.md) | Draws with the phone's OpenGL ES 3.0                                                                                      |
| The web preview                                                                            | The browser's WebGL2: the element is an HTML `<canvas>`                                                                   |
| Lynx Explorer, and native apps that embed Lynx themselves                                  | `null`: the canvas is an empty view. Their apps don't have the canvas element                                             |

## Usage

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

```typescript title="src/app/scene.ts"
import {
  afterNextRender,
  Component,
  type ElementRef,
  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() {
    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.
      new ResizeObserver(([entry]) => {
        const { width, height } = entry.contentRect;
        renderer.setSize(width, height, false);
        camera.aspect = width / height;
        camera.updateProjectionMatrix();
      }).observe(canvas);

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

Like an HTML canvas:

- `width` and `height` are the drawing buffer's size in pixels, 300 by 150 to start. The element's CSS size is how big it's shown.
- `getContext('webgl2')` returns the same context every time. There's no `'2d'` context, and `'webgl'` (WebGL 1) is `null`: three.js needs WebGL2 anyway.
- Pointer events (`pointerdown`, `pointermove`, `pointerup` and the rest) work, from the finger's touches. They work on every element, not only a canvas: see [Element events](/api/angular-lynx/element-events.md).
- `clientWidth`, `clientHeight` and `getBoundingClientRect()` answer from the element's last layout, and `ResizeObserver` reports its size as it changes.

## angular-three

[angular-three](https://github.com/angular-threejs/angular-three) works with its own renderer, as on the web:

```typescript title="src/app/app.config.ts"
import { provideZonelessChangeDetection } from '@angular/core';
import { provideRenderer } from '@blotch/angular-lynx';
import { provideNgtRenderer } from 'angular-three/dom';

export const appConfig = {
  providers: [
    provideZonelessChangeDetection(),
    provideRenderer(),
    provideNgtRenderer(),
  ],
};
```

`provideRenderer()` comes first: angular-three's renderer wraps Angular's, which on Lynx is AngularLynx's. `<ngt-canvas>` sizes itself, and its events (`(click)`, `(pointerover)` on a mesh) come from touches.

:::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`](https://docs.npmjs.com/cli/configuring-npm/package-json#overrides) entry. Pin `three` to `0.182.x`.
:::

## Images and models

Textures and models load as on the web:

- three.js's `TextureLoader` (an `Image`), `ImageBitmapLoader` (`createImageBitmap()`) and `GLTFLoader`, including `.glb` files with their textures inside.
- From `https://` URLs, `data:` URLs, object URLs (`URL.createObjectURL()`), and files your app bundles, by their path in `dist/`: copy a model in with [`output.copy`](https://rsbuild.rs/config/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. Loaders that need Web Workers or WebAssembly, such as `DRACOLoader` and `KTX2Loader`, don't work.

## Save it as an image

`toBlob()` and `toDataURL()` work as on an HTML canvas: they encode what the canvas shows as a PNG, or a JPEG for `'image/jpeg'`, with a quality from 0 to 1 (0.92 when you don't give one). Any other type gives a PNG, as in browsers.

```typescript
canvas.toBlob(
  (blob) => {
    if (blob) this.photo.set(URL.createObjectURL(blob));
  },
  'image/jpeg',
  0.9,
);
```

The `Blob` is ready to show in an `<image>`, upload, or [share](/guide/share.md). The phone encodes the image, so it's quick: a 2048 by 1365 JPEG takes about 20 ms on the iOS Simulator and 35 ms on the Android emulator, plus 15 ms before the callback has the `Blob`. A PNG of that size is slower to encode, around 100 ms on the iOS Simulator and 300 ms on the Android emulator, and is several times larger.

- **Call it right after you draw.** It reads the drawing buffer, which WebGL clears once a frame is on screen, as in a browser. So call `toBlob()` in the same function that draws, such as right after three.js's `renderer.render()`, or make the context with `preserveDrawingBuffer: true` (three.js: `new WebGLRenderer({ preserveDrawingBuffer: true })`). Called later without it, you get a transparent image.
- **The callback runs later,** as on the web: `toBlob()` reads the canvas when you call it, and the callback gets the `Blob` once it's ready. `toDataURL()` returns at once.
- **A JPEG has no transparency:** transparent pixels come out black, as in browsers. A PNG keeps them.
- **Nothing to encode gives `null`** (`toBlob()`) or `'data:,'` (`toDataURL()`): a canvas that never called `getContext('webgl2')`, one whose context was lost, or Lynx Explorer, which has no canvas.
- **Prefer `toBlob()`.** A `data:` URL holds the image as text a third larger than the file, and building it is all at once.

## Attributes

| Attribute | Type     | Default | Description                             |
| --------- | -------- | ------- | --------------------------------------- |
| `width`   | `number` | `300`   | The drawing buffer's width, in pixels.  |
| `height`  | `number` | `150`   | The drawing buffer's height, in pixels. |

Plus the [common attributes](/api/elements/view.md#attributes), such as `id`, `class` and `style`.

## Events

| Event                       | Description                                                                                                 |
| --------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `webglcontextlost`          | The context was lost: `WEBGL_lose_context`'s `loseContext()` was called (three.js does when it's disposed). |
| `webglcontextcreationerror` | `getContext('webgl2')` couldn't make a context.                                                             |

Plus pointer events and the [common events](/api/elements/view.md#events), such as `bindtap`.

## Performance

Your scene's JavaScript runs on the thread Lynx lays the page out on, and 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. Keep the drawing buffer to the screen's pixel ratio, at most 2, and stop the loop (`setAnimationLoop(null)`) when the scene is off screen. The first time an app runs after it's installed, each material's shaders compile when it's first drawn (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, so a scene that's smooth there is a fair guide, but check frame rates on a phone before you ship: a phone's GPU is slower than a Mac's.
:::

## What's different from the web

- No 2D context: libraries that draw with `getContext('2d')` don't work.
- `toBlob()` and `toDataURL()` encode only a WebGL canvas, and the main thread waits while the phone reads and encodes it (tens of milliseconds for a photo), where a browser's `toBlob()` encodes in the background. A canvas without a context gives `null` rather than a blank image.
- A `<canvas>` or `<video>` can't be a texture: use images, `ImageBitmap`s or typed arrays.
- A lost context isn't restored: make a new renderer.
- `crypto.randomUUID()`, which AngularLynx adds for angular-three, isn't cryptographically secure: don't use it for secrets.

## Related

- [`<svg>`](/api/elements/svg.md): vector drawings
- [Share Files and Links](/guide/share.md): send a canvas's image to another app, or save it to Photos
- [The JavaScript environment](/guide/concepts/javascript-environment.md): the browser APIs AngularLynx adds
