Images, videos and saving
Crop and copy images with createImageBitmap(), draw a video's frame, and save what a <canvas> shows as a JPEG or PNG. It all works as on the web, and the phone handles the pixels.
Save as an image
toBlob() and toDataURL() work as on an HTML canvas. They encode what the canvas shows:
- The type: a PNG, or a JPEG for
'image/jpeg'. Any other type gives a PNG, as in browsers. - The quality: from 0 to 1, for a JPEG. It's 0.92 when you don't give one.
- The
Blobis 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. The callback has the Blob about 15 ms after that. A PNG of that size is slower: around 100 ms on the iOS Simulator and 300 ms on the Android emulator. It's several times larger, too.
Things to know
- With WebGL, call it right after you draw. A 2D canvas keeps what's drawn on it. WebGL clears the drawing buffer once a frame is on screen, as in a browser. So call
toBlob()in the 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.
- A canvas without a context encodes as a transparent image of its size, as in browsers.
- Nothing to encode gives
nullfromtoBlob(), or'data:,'fromtoDataURL(). That's a canvas with no width or height, 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 it's built all at once. - Encoding makes the main thread wait while the phone reads and encodes the canvas: tens of milliseconds for a photo. A browser's
toBlob()encodes in the background.
Draw a video frame
A <video> is an image too. Draw its frame for a thumbnail or a filter, or play it on a three.js mesh with VideoTexture:
For a mesh, pass the same element to three.js: new THREE.VideoTexture(video). It waits for the frame by itself.
- The pixels never pass through JavaScript. The phone reads the frame the video shows.
createImageBitmap(video)keeps a copy of it, as on the web. - Before its first frame,
videoWidthis 0,readyStateisHAVE_NOTHING, and nothing is drawn, as in a browser. - Check
readyStatebefore you draw, and try again if there's no frame yet, as above. A video draws its frame only once it has one (readyStateat leastHAVE_CURRENT_DATA), whether it's shown or hidden withdisplay: none. - A turned video is drawn turned. When the file says to turn it (a phone recording in portrait), it's drawn as it plays.
videoWidthandvideoHeightare its turned size, as in a browser.
When bindfirstframe arrives
- On the phone, the frame is usually there when
bindfirstframearrives. iOS waits for it for half a second at most, and Android for 5 seconds. Then each sends the event anyway. - The web preview never sends
bindfirstframe. Draw from a tap or abindtimeupdatethere.
In the web preview
The web preview takes a template's <video> and <canvas> too. The browser's drawImage(), createPattern(), texImage2D(), texSubImage2D() and createImageBitmap() read the element itself.
There, as on the phone, instanceof checks pass:
- The video is
instanceof HTMLVideoElement. three.js sizes aVideoTextureby it. - The canvas is
instanceof HTMLCanvasElement. - Its 2D context is
instanceof CanvasRenderingContext2D. - Its WebGL2 context is
instanceof WebGL2RenderingContext. The objects it makes are instances ofWebGLBuffer,WebGLTexture,WebGLProgram,WebGLUniformLocationand the rest, as WebGL helper libraries such as twgl.js check.
Crop with createImageBitmap
createImageBitmap() takes the same sources as on the web. With the crop arguments, it makes a bitmap of a part of one:
- Any source can be cropped: a
Blob, a loadedImage, a canvas, anImageBitmapor anImageData. A canvas is a<canvas>,document.createElement('canvas')or anOffscreenCanvas. - Outside the source is transparent. The part of the crop rectangle past the source's edge comes out clear.
- An
ImageBitmapis copied with the options you pass: flipped, premultiplied or resized. - The phone copies the pixels. A canvas, a bitmap or an
Imageis copied from the pixels the phone already has. Nothing crosses to JavaScript, and no file is decoded again. AnImageData's pixels go to the phone once. OffscreenCanvas.transferToImageBitmap()hands over its image and clears the canvas, as on the web.- Make a WebGL canvas's bitmap in the function that draws, as with
toBlob(). Once the frame is on screen, its drawing buffer is cleared, unless you asked forpreserveDrawingBuffer: true.
Show an ImageBitmap
getContext('bitmaprenderer') gives an ImageBitmapRenderingContext, as on the web. Its transferFromImageBitmap() shows an ImageBitmap in the canvas. That's how web code shows what an OffscreenCanvas drew:
- The bitmap is scaled to the canvas's CSS size, whatever its
widthandheight. The canvas draws, uploads and encodes as that bitmap. - The bitmap is handed over, not copied. Afterwards its
widthandheightare 0, and it can't be drawn or shown again. transferFromImageBitmap(null)clears the canvas to transparent. It's black for a context made with{ alpha: false }.
In an ejected app
An ejected app keeps the native shell it was ejected with. An older shell lacks some of this:
createImageBitmap()rejects anImageBitmap, anImageData, a loadedImageand a crop.getContext('bitmaprenderer')staysnull: the shell has no bitmaprenderer context.
To take these in without losing your own native changes, merge the new canvas files from node_modules/@blotch/angular-lynx-native/templates into your copy:
- iOS:
App/AngularLynxWebGL.h,App/AngularLynxWebGL.mandApp/AngularLynxBitmapRendererContext.m - Android:
WebGLModule.kt,Canvas2DContext.kt,BitmapRendererContext.ktandBitmapPresenter.kt
The module uses the bitmaprenderer context's classes, so copying it alone doesn't build.
Related
- Draw on a canvas: 2D drawing, and what the build includes
- Draw in 3D: three.js, angular-three, textures and models
- Share Files and Links: send a canvas's image to another app, or save it to Photos
- Darkroom: a photo editor that develops in WebGL and exports with
toBlob() <canvas>and<video>
