Skip to main content

Capturing views

There are two entry points. Both resolve to the same nitro-ui-renderer Image.

Entry pointUse it when
ViewShotView + ref.capture()You own the content and can wrap it.
ViewShot.capture(view)You already hold a ViewRef to a mounted NitroUI View.

ViewShotView is a thin wrapper: it renders a View, keeps its ViewRef, and its capture() calls ViewShot.capture on it.

What ends up in the image​

  • The view and its descendants. The capture starts at the target view and draws its subtree.
  • Not what is drawn over it. Sibling views, overlays or modals on top of the target are left out.
  • Hidden parts of the subtree. Parts an ancestor scrolls or clips out of sight are included, so a view half off-screen in a scroll container is captured whole.
  • The view's own clipping. A rounded view with overflow: 'hidden' clips its children to its corners, and pixels outside the corners stay transparent.
  • Its background and shadows. Backgrounds, gradients, rounded corners, text and box shadows are rendered natively.

Size and color​

The image renders at the display's pixel density: a 300-point-wide view on a 3× screen gives a 900-pixel-wide image. image.width and image.height are always in pixels on both platforms.

Pixels are 8-bit sRGB with alpha. Areas the view does not paint stay transparent; give the view a background if you need an opaque image (JPEG has no alpha).

Capturing from an event​

A capture is asynchronous. Guard against double taps and surface failures, as the example app does:

const capture = () => {
const target = cards.current
if (target == null || isCapturing) return
setIsCapturing(true)
setFailure(undefined)
target.capture().then(
(image) => {
setShot(image)
setIsCapturing(false)
},
(error: unknown) => {
setFailure(String(error))
setIsCapturing(false)
},
)
}

Empty views​

A view with zero width or height cannot be captured: the promise rejects with Cannot capture a view with an empty size. Wait until layout has given the view a size, for example by capturing from a user action rather than on mount. See Errors.