Skip to content

Rendering Contexts

@nativescript/canvas exposes multiple context types, each suited for different workloads.

ContextBest forAPI style
2dUI drawing, charts, image compositionCanvas 2D spec
webglPortable GPU renderingWebGL 1 spec
webgl2Modern GL featuresWebGL 2 spec
webgpuNew GPU pipelines and compute-style workflowsWebGPU spec

Choosing a context

  • Use 2d for simple rendering and draw operations.
  • Use webgl or webgl2 for 3D scenes and shader-heavy visuals.
  • Use webgpu when you need newer GPU capabilities and explicit pipeline control.

Creating a context

ts
const ctx2d = canvas.getContext('2d');
const gl = canvas.getContext('webgl');
const gl2 = canvas.getContext('webgl2');
const gpu = canvas.getContext('webgpu');

Spec alignment

The package follows web API behavior where possible:

Sample guides