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:
- Stop the loop in
onDestroy.setAnimationLoop()runs until you pass itnull, even after the component is gone. - WebGL2 only.
getContext('webgl')(WebGL 1) returnsnull. three.js needs WebGL2 anyway. - One kind of context per canvas. After
getContext('2d'),getContext('webgl2')isnull, 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:
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.
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(anImage)ImageBitmapLoader(createImageBitmap())GLTFLoader, including.glbfiles with their textures inside
They load from:
https://URLsdata: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.
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.
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.
Related
- Draw on a canvas: 2D drawing, and what the build includes
- Images, videos and saving: bitmaps, video frames and
toBlob() <canvas>: the element's reference- Orrery: planets drawn with three.js and GLSL shaders
