captureScene takes the scene's next frame, exactly as the screen shows it (look, shadows, occlusion), and saves it as a PNG:
- on a phone, as a file in the app's cache (needs
expo-file-system); - on the web, as a blob URL (with WebGPU or WebGL 2).
captureScene is imported from @apricotjs/react-native/capture.
Share a picture#
import { type ApricotScene, Box, Scene } from "@apricotjs/react-native";
import { captureScene } from "@apricotjs/react-native/capture";
import * as Sharing from "expo-sharing";
import { useState } from "react";
import { Button, View } from "react-native";
export function ShareScene() {
const [scene, setScene] = useState<ApricotScene | null>(null);
const share = async () => {
if (!scene) return;
const { uri } = await captureScene(scene, { maxSize: 1080 });
await Sharing.shareAsync(uri);
};
return (
<View style={{ flex: 1 }}>
<Scene style={{ flex: 1 }} look="studio" onCreated={setScene}>
<Box radius={0.1} material="car-paint" />
</Scene>
<Button title="Share" disabled={!scene} onPress={share} />
</View>
);
}Get the scene from onCreated, from onReady, or from useScene() inside the scene.
Options#
| Option | Default | Meaning |
|---|---|---|
maxSize |
as drawn | The largest size in pixels, as [width, height] or one number for both. The picture is scaled down to fit and never scaled up |
name |
capture-<time> |
The file's name, without .png. Saving again under the same name replaces the file |
directory |
"cache" |
Where it is kept on a phone: "cache" (the system may clear it when space runs low) or "documents" (kept until the app deletes it or is removed) |
It resolves to { uri, width, height } (pixels).
Thumbnails#
Saving a full-screen picture takes a moment on a phone. A 256-pixel thumbnail is quick. Give each thumbnail a fixed name, so a new picture replaces the old one:
const thumbnail = await captureScene(scene, { maxSize: 256, name: `room-${roomId}` });For a gallery of saved rooms or designs, capture each one when it changes and show the pictures with <Image>. Avoid running many live scenes at once.
Finding a picture again: captureUri#
Store the picture's name and look up its URI when you need it. On iOS the app's folders can move between launches, so a stored URI goes stale. captureUri(name, { directory }) returns where the picture is now, or null if there is none. On the web it always returns null, because blob URLs last only as long as the page.
import { captureUri } from "@apricotjs/react-native/capture";
import { Image } from "react-native";
export function RoomThumbnail({ roomId }: { roomId: string }) {
const uri = captureUri(`room-${roomId}`, { directory: "documents" });
return uri ? <Image source={{ uri }} style={{ width: 128, height: 128 }} /> : null;
}Save it with the same name and directory:
await captureScene(scene, { maxSize: 256, name: `room-${roomId}`, directory: "documents" });Good to know#
captureScenewaits for a frame that shows everything just added, so a picture taken right after a swap has the new piece. Models still loading are not in it: capture after theironReady.- Delete a picture with
deleteCapture(name, { directory }), which returns whether there was one. Delete all of them withclearCaptures({ directory }), which returns how many it deleted. Do this on sign-out or when an account is deleted. - Files go in an
apricotfolder in the app's cache, or its documents withdirectory: "documents". - Without
expo-file-systemon a phone,captureScenethrows an error that names it.
Related#
- Scene reference.
- Placement: saving layouts.