<canvas>
A drawing surface with a WebGL2 context, for 3D scenes with three.js, angular-three or plain WebGL. Lynx has no canvas of its own: this one is AngularLynx's.
Where it works
Usage
Give the canvas a size with CSS, and draw once it's laid out:
Like an HTML canvas:
widthandheightare 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) isnull: three.js needs WebGL2 anyway.- Pointer events (
pointerdown,pointermove,pointerupand the rest) work, from the finger's touches. They work on every element, not only a canvas: see Element events. clientWidth,clientHeightandgetBoundingClientRect()answer from the element's last layout, andResizeObserverreports its size as it changes.
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, and its events ((click), (pointerover) on a mesh) come from touches.
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.
Images and models
Textures and models load as on the web:
- three.js's
TextureLoader(anImage),ImageBitmapLoader(createImageBitmap()) andGLTFLoader, including.glbfiles with their textures inside. - From
https://URLs,data:URLs, object URLs (URL.createObjectURL()), and files your app bundles, by their path indist/: copy a model in withoutput.copyinlynx.config.ts, then load'/assets/models/frame.glb'. A build has no loader forimport 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.
The Blob is ready to show in an <image>, upload, or share. 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'srenderer.render(), or make the context withpreserveDrawingBuffer: 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 theBlobonce 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 calledgetContext('webgl2'), one whose context was lost, or Lynx Explorer, which has no canvas. - Prefer
toBlob(). Adata:URL holds the image as text a third larger than the file, and building it is all at once.
Attributes
Plus the common attributes, such as id, class and style.
Events
Plus pointer events and the common 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.
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()andtoDataURL()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'stoBlob()encodes in the background. A canvas without a context givesnullrather than a blank image.- A
<canvas>or<video>can't be a texture: use images,ImageBitmaps 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>: vector drawings- Share Files and Links: send a canvas's image to another app, or save it to Photos
- The JavaScript environment: the browser APIs AngularLynx adds
