<Placement> lets people move its <Placeable> children by hand. Use it to furnish a room, arrange a desk or set up a beach.
| Gesture | Result |
|---|---|
| Tap a thing | Picks it up. A marker appears under it |
| Drag the thing | Moves it along the floor |
| Twist two fingers | Turns it |
| Tap empty space | Puts it down |
A drag that starts on the marker also moves the picked-up thing, which makes thin things (a towel, a frame) easy to grab. Drags that start anywhere else turn the camera (with <CameraRig orbit>).
The layout is plain data. Each thing has an id, a position and a turn. You pass them in as props and get them back through onChange. Keep the layout with useLayout, which adds undo, redo and saving between runs.
A room to furnish#
A complete screen with a sofa, a coffee table with a lamp on it, a rug, and a frame on the wall. Undo and Redo buttons sit under the room.
import {
Box,
CameraRig,
Cone,
Cylinder,
Lamp,
type LayoutItem,
Merged,
type PlaceOn,
Placeable,
Placement,
Scene,
useLayout,
} from "@apricotjs/react-native";
import type { ReactNode } from "react";
import { Button, StyleSheet, View } from "react-native";
type Kind = "sofa" | "table" | "lamp" | "rug" | "frame";
interface Piece extends LayoutItem {
kind: Kind;
}
// Each piece stands on its origin and faces +z. Hung things have their back at z = 0.
const KINDS: Record<Kind, { on?: PlaceOn; surface?: boolean; solid?: boolean; make: () => ReactNode }> = {
sofa: {
make: () => (
<Merged>
<Box size={[1.6, 0.4, 0.8]} position={[0, 0.2, 0]} color="#ee9c9b" />
<Box size={[1.6, 0.5, 0.2]} position={[0, 0.65, -0.3]} color="#e48a8a" />
</Merged>
),
},
table: {
surface: true, // things with on="surface" can stand on it
make: () => (
<Merged>
<Box size={[0.9, 0.05, 0.5]} position={[0, 0.425, 0]} color="#e5c595" />
<Cylinder radius={0.03} height={0.4} position={[-0.38, 0.2, -0.18]} color="#8d5c3d" />
<Cylinder radius={0.03} height={0.4} position={[0.38, 0.2, -0.18]} color="#8d5c3d" />
<Cylinder radius={0.03} height={0.4} position={[-0.38, 0.2, 0.18]} color="#8d5c3d" />
<Cylinder radius={0.03} height={0.4} position={[0.38, 0.2, 0.18]} color="#8d5c3d" />
</Merged>
),
},
lamp: {
on: "surface", // on a table, or on the floor
make: () => (
<>
<Merged>
<Cylinder radius={0.05} height={0.25} position={[0, 0.125, 0]} color="#f2c8a1" />
<Cone radius={0.12} height={0.14} position={[0, 0.3, 0]} color="#fff3e0" />
</Merged>
<Lamp position={[0, 0.28, 0]} glow={0.08} />
</>
),
},
rug: {
solid: false, // things can stand over it
make: () => <Box size={[1.8, 0.01, 1.2]} position={[0, 0.005, 0]} color="#cfc3cb" />,
},
frame: {
on: "wall",
make: () => (
<Merged>
<Box size={[0.5, 0.4, 0.03]} position={[0, 0, 0.015]} color="#fbf7f1" />
<Box size={[0.4, 0.3, 0.01]} position={[0, 0, 0.035]} color="#a2bbcc" />
</Merged>
),
},
};
const START: Piece[] = [
{ id: "rug", kind: "rug", position: [0, 0, 0.2], turn: 0 },
{ id: "sofa", kind: "sofa", position: [0, 0, -1.0], turn: 0 },
{ id: "table", kind: "table", position: [0, 0, 0.2], turn: 0 },
{ id: "lamp", kind: "lamp", position: [0.25, 0.45, 0.2], turn: 0 }, // y = the table's top
{ id: "frame", kind: "frame", position: [0, 1.5, -1.5], turn: 0 }, // on the back wall
];
function Room() {
return (
<>
<Box size={[4, 0.1, 3]} position={[0, -0.05, 0]} color="#ebc79c" />
<Box size={[4, 2.6, 0.1]} position={[0, 1.3, -1.55]} color="#faf1e8" />
<Box size={[0.1, 2.6, 3]} position={[-2.05, 1.3, 0]} color="#faf1e8" />
</>
);
}
export function FurnishRoom() {
const layout = useLayout(START, { save: "my-room" });
return (
<View style={{ flex: 1 }}>
<Scene style={StyleSheet.absoluteFill} look="soft" shadows lamps={1}>
<Room />
<Placement
bounds={[-2, -1.5, 2, 1.5]} // the walls: [minX, minZ, maxX, maxZ]
wallHeight={2.6}
snap={{ grid: 0.05, turn: 15, wall: 0.1 }}
onChange={layout.place}
>
{layout.loaded &&
layout.items.map((p) => {
const kind = KINDS[p.kind];
return (
<Placeable
key={p.id}
id={p.id}
position={p.position}
turn={p.turn}
on={kind.on}
surface={kind.surface}
solid={kind.solid}
>
{kind.make()}
</Placeable>
);
})}
</Placement>
<CameraRig
subject={null}
view={{ azimuth: 35, elevation: 35, distance: 8, target: [0, 0.6, 0] }}
orbit={{ turn: 40, elevation: [12, 70], zoom: [0.5, 1.4] }}
inset={{ bottom: 100 }}
/>
</Scene>
<View style={{ position: "absolute", bottom: 40, left: 0, right: 0, flexDirection: "row", justifyContent: "center" }}>
<Button title="Undo" disabled={!layout.canUndo} onPress={layout.undo} />
<Button title="Redo" disabled={!layout.canRedo} onPress={layout.redo} />
</View>
</View>
);
}In this example:
- Each piece says where it goes with
on, and whether others stand on it (surface) or over it (solid={false}). See Where things go. - A dragged sofa stops against the table and slides along it. See When things meet.
snap.wallpulls furniture flush against a wall once it is within 0.1 m.<Merged>draws each piece's shapes as one mesh, so each piece is one draw call. See Performance.- With
save, draw the pieces only oncelayout.loadedis true, so they don't jump when the kept layout is read.
Where things go: on#
Set on on each <Placeable>:
on |
The thing |
|---|---|
"floor" (default) |
Stands on the floor |
"surface" |
Stands on things marked surface, or on the floor |
"wall" |
Hangs on the walls: the sides of bounds, or the walls you give |
"water" |
Floats. See Ground and water |
Things on a surface move with it. Drag the table and the lamp comes along. Both new places are reported to onChange.
Building pieces#
Each piece is whatever you put inside its <Placeable>: shapes, a <Merged> group of shapes, or a <Model>.
- Build it standing on its origin (its lowest point at y = 0, centred on x and z) and facing +z.
- Build things for walls with their back at z = 0, facing +z (into the room).
- A piece's size comes from what it draws. Its footprint is the floor its parts cover, turned with it. A lamp's glow is left out.
- A
<Model>works the same way. Usegroundto stand it on its lowest point. It is measured once it has loaded. Until then it has no size, so it is not checked and is in no one's way.
import { Placeable } from "@apricotjs/react-native";
import { Model } from "@apricotjs/react-native/model";
export const Armchair = () => (
<Placeable id="armchair" position={[0.75, 0, -0.35]} turn={330}>
<Model src={require("./assets/armchair.glb")} ground />
</Placeable>
);What a piece takes up on the floor#
Leave parts that reach over others (a palm's fronds, a parasol's canopy, a shelf's overhang) out of the footprint, so things can stand under them. There are two ways:
- Put them in
<Group footprint={false}>. They still show and can still be tapped, but take up no floor. - Give the footprint yourself with
<Placeable footprint>:[width, depth]centred on the piece's origin, or its sides{ minX, maxX, minZ, maxZ }, in metres.
import { Cylinder, Group, Placeable, Sphere } from "@apricotjs/react-native";
import { Model } from "@apricotjs/react-native/model";
export const Palm = () => (
<Placeable id="palm" position={[1, 0, 0]}>
<Cylinder radius={0.08} height={2.5} position={[0, 1.25, 0]} color="#8d5c3d" />
{/* Things can stand under the fronds. */}
<Group footprint={false}>
<Sphere radius={1.1} position={[0, 2.6, 0]} color="#5f9b4b" />
</Group>
</Placeable>
);
export const Parasol = () => (
<Placeable id="parasol" footprint={[0.4, 0.4]} position={[-1, 0, 0]}>
<Model src={require("./assets/models/parasol.glb")} ground />
</Placeable>
);For three.js objects of your own, leaveOutOfFootprint(object) does the same (leaveOutOfFootprint(object, false) counts it again).
Positions and turns#
| Field | Meaning |
|---|---|
position |
[x, y, z] in metres. For floor things, y is the floor's level (0). For things on a surface, y is the surface's top. For hung things, the position is a point on the wall's face and y is its height |
turn |
Degrees round the vertical axis. On a wall, the wall sets it to the turn that faces out from the wall. On the sides of bounds: 0 on the back wall (min z), 90 on the min-x wall, 180 on the front wall (max z), 270 on the max-x wall |
The walls are the four sides of bounds, wallHeight tall, or the walls you give. A thing can be dragged onto any wall whose facing side the camera sees.
Walls of any shape#
bounds is the shorthand for a rectangular room. For any other shape, give walls. Each entry is a room's outline or a free-standing wall.
A room's outline#
List its corners [x, z] in order round it, either way round. Use it for an L-shaped room, a bay, or a corner cut at an angle. Each side becomes a wall facing in. Without areas, the outline is also the floor things stay on.
A free-standing wall#
{ from, to, height?, id? } is a partition or a screen. Things can hang on either face. Floor things keep off it and slide along it.
import { Placement } from "@apricotjs/react-native";
import type { ReactNode } from "react";
export const LShapedRoom = ({ children }: { children: ReactNode }) => (
<Placement
walls={[
[[-3, -2], [3, -2], [3, 0], [0, 0], [0, 2], [-3, 2]], // the room's outline
{ id: "partition", from: [-1, -2], to: [-1, -0.5], height: 2 }, // a free-standing wall, 2 m high
]}
wallHeight={2.6} // the room's walls; the partition sets its own
snap={{ grid: 0.05, turn: 15, wall: 0.1 }}
>
{children}
</Placement>
);Things hang, snap flush (snap.wall) and are picked up on walls at any angle. A hung thing's turn faces out from its wall, so on a wall at an angle it is not a multiple of 90.
walls only tells <Placement> where the walls are. Draw them yourself, with <Box>es or a model.
A floor thing touching a free-standing wall counts as touching a zone. In canPlace, the wall's id is in check.zones. Its default id is wall <n>, where n is its index in walls.
Snapping#
snap={{ grid, turn, wall }} sets three snaps. All default to 0 (free).
| Option | Unit | Effect |
|---|---|---|
grid |
metres | Positions snap to a grid of this size |
turn |
degrees | Turns snap to steps of this size |
wall |
metres | Furniture this close to a wall moves flush against it |
When things meet: collide#
Things are solid by default and don't overlap. Give solid={false} to a thing others may stand over, such as a rug. collide on <Placement> sets what happens when a moved thing meets a solid one.
| Mode | A moved thing that meets another |
|---|---|
"block" (default) |
Stops against it and slides along it |
"warn" |
Passes through it, with the marker in its blocked colour. If let go there, it goes back to where it last fitted |
"off" |
Ignores it |
"warn" gives more freedom on a small phone screen. "block" never leaves a layout in a bad state.
Your own rules: canPlace#
canPlace(id, place, check) is called while a thing moves or turns. Return false where it may not go. With "block", the thing stays where it last fitted. Otherwise the marker turns red, and if let go there, the thing goes back.
import { Placement, type Place, type PlaceCheck } from "@apricotjs/react-native";
import type { ReactNode } from "react";
// The doorway: x between -0.5 and 0.5, by the front wall.
const inDoorway = ({ position: [x, , z] }: Place) => Math.abs(x) < 0.5 && z > 1.0;
export const RoomRules = ({ children }: { children: ReactNode }) => (
<Placement
bounds={[-2, -1.5, 2, 1.5]}
collide="warn"
canPlace={(id, place: Place, { standingOn }: PlaceCheck) => standingOn !== null || !inDoorway(place)}
>
{children}
</Placement>
);check (PlaceCheck) describes the place:
| Field | Meaning |
|---|---|
standingOn |
The id of what the thing would stand on, or null |
overlapping |
What it would overlap |
on |
Where it goes |
area |
The area it would stand in. See Ground and water |
zones |
The no-go zones it touches |
A panel for the picked-up thing#
To show your own UI for the picked-up thing, control the selection with selected and onSelect. onFitChange reports whether the thing fits where it is, so you can show a hint. The ref (PlacementHandle) turns things by code and lists what stands on them.
import { Box, CameraRig, type LayoutItem, Placeable, Placement, type PlacementHandle, Scene, useLayout } from "@apricotjs/react-native";
import { useRef, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
const START: LayoutItem[] = [
{ id: "crate-1", position: [-0.6, 0, 0], turn: 0 },
{ id: "crate-2", position: [0.6, 0, 0], turn: 0 },
];
export function PiecePanel() {
const layout = useLayout(START);
const placement = useRef<PlacementHandle>(null);
const [selected, setSelected] = useState<string | null>(null);
const [fits, setFits] = useState(true);
// Clear the selection when undo removes the picked-up piece.
if (selected !== null && !layout.items.some((p) => p.id === selected)) setSelected(null);
return (
<View style={{ flex: 1 }}>
<Scene style={StyleSheet.absoluteFill} look="soft" shadows>
<Box size={[4, 0.1, 3]} position={[0, -0.05, 0]} color="#ebc79c" />
<Placement
ref={placement}
bounds={[-2, -1.5, 2, 1.5]}
collide="warn"
selected={selected}
onSelect={(id) => {
setSelected(id);
setFits(true);
}}
onChange={layout.place}
onFitChange={(_, now) => setFits(now)}
marker={{ color: "#3ccf6e", blocked: "#ff5a4f", radius: 0.08 }}
>
{layout.items.map((p) => (
<Placeable key={p.id} id={p.id} position={p.position} turn={p.turn}>
<Box size={[0.6, 0.5, 0.4]} position={[0, 0.25, 0]} color="#c98b5a" />
</Placeable>
))}
</Placement>
<CameraRig subject={null} view={{ azimuth: 30, elevation: 40, distance: 7 }} orbit={{ turn: 45 }} />
</Scene>
{selected ? (
<View style={{ position: "absolute", bottom: 40, left: 16, right: 16, gap: 8 }}>
<Text>{fits ? "Drag to move, twist to turn" : "Doesn't fit here: let go and it goes back"}</Text>
<View style={{ flexDirection: "row", flexWrap: "wrap" }}>
<Button title="Turn left" onPress={() => placement.current?.turnBy(selected, 45)} />
<Button title="Turn right" onPress={() => placement.current?.turnBy(selected, -45)} />
<Button
title="Remove"
onPress={() => {
// What stands on it goes too, in one undo step.
layout.remove([selected, ...(placement.current?.standingOn(selected) ?? [])]);
setSelected(null);
}}
/>
<Button title="Undo" disabled={!layout.canUndo} onPress={layout.undo} />
<Button title="Done" onPress={() => setSelected(null)} />
</View>
</View>
) : null}
</View>
);
}turnBy(id, degrees) turns a thing with the same checks as a twist, snapped to snap.turn. It returns false if the thing could not turn. Things hung on walls don't turn.
The marker#
marker styles the patch under the picked-up thing, or behind it on a wall. Pass a colour, false for none, or { color, blocked, opacity, margin, radius, outline }. A large radius makes a pill or a disc. outline draws only a ring of that width.
Keeping the layout: useLayout#
useLayout(initial, options) holds a list of LayoutItems with undo and redo. Each item is { id, position, turn } plus your own fields.
const layout = useLayout(START, { save: "living-room", version: 1 });
<Placement onChange={layout.place}>…</Placement>| Returns | Use |
|---|---|
items |
The layout now. Draw a <Placeable> per item |
place |
Pass to <Placement onChange>. The moves of one gesture (a desk and what stands on it) make one undo step. A piece moved by findRoom as it arrives makes no step |
add(item), update(id, change), remove(ids), set(items) |
Change the layout. Each is one step. remove takes one id or several. add, update and remove also take { history: false } (see below) |
undo(), redo(), canUndo, canRedo |
History, up to history steps (default 100) |
clearHistory() |
Forgets the steps to undo and redo. The layout stays as it is |
ensure(items) |
Adds the items not in the layout yet (by id), outside history. Items already there stay as they are. Use it for things kept elsewhere, such as a server's list of purchases |
newId(prefix) |
A free id: chair-1, chair-2… |
loaded |
With save, true once the kept layout has been read. Draw items from then on |
Saving between runs#
With save, the layout is written a moment after each change and read back at the next launch. It is kept:
- on a phone, as a JSON file in the app's documents (needs
expo-file-system); - on the web, in local storage;
- anywhere else, with
storage: { read(name), write(name, text) }.
Adding a piece and picking it up#
Give the new item a free id from newId, add it, then select it.
const add = (kind: Kind) => {
const id = layout.newId(kind);
layout.add({ id, kind, position: [0.3, 0, 0.5], turn: 0 });
setSelected(id);
};Purchases: changes undo can't take back#
When things are bought or sold (with coins, in a shop), undo must not bring a sold sofa back or take a bought one away. Make those changes with { history: false }. They are not a step of their own. They are applied to every step, so undo and redo keep them.
const buy = (kind: Kind, place: Place) => layout.add({ id: layout.newId(kind), kind, ...place }, { history: false });
const sell = (id: string) => layout.remove(id, { history: false });Changing the shape of your items: version and migrate#
A kept layout is JSON:
{ "version": 2, "items": [{ "id": "sofa-1", "position": [0.5, 0, -1], "turn": 90, "kind": "sofa" }] }Raise version when your items change in a way old layouts can't follow, and give migrate to bring old layouts up to date. migrate receives { version, items } as kept, and returns this version's items, or null when it can't convert them.
const layout = useLayout(START, {
save: "living-room",
version: 2,
// Version 1 had no kinds: tell them from the ids.
migrate: ({ version, items }) =>
version === 1 ? (items as Piece[]).map((p) => ({ ...p, kind: p.id.split("-")[0] as Kind })) : null,
});A layout that can't be read (another version with no migration, or a damaged file) is never written over. The screen starts from initial. The old layout is kept beside it as <save>.v<its version> (or <save>.unreadable), where your own storage can still find it.
Finding room for something new#
To add a piece where it fits (a purchase, a new piece from a catalogue), either ask for a free place first with findFreePlace, or add the piece anywhere with findRoom and let it move to where it fits.
Asking first: findFreePlace#
ref.findFreePlace(options) returns the nearest free Place to near, or null when there is no room. When it returns null, tell the user instead of adding the piece. It uses the same checks as moving by hand: other things, walls, areas, zones and canPlace.
import { type PlacementHandle, type PlaceOn } from "@apricotjs/react-native";
import { measureModel } from "@apricotjs/react-native/model";
import type { RefObject } from "react";
import { Alert, Button } from "react-native";
export function BuyButton({
placement,
src,
on,
onBought,
}: {
placement: RefObject<PlacementHandle | null>;
src: number;
on: PlaceOn;
onBought: (place: { position: [number, number, number]; turn: number }) => void;
}) {
const buy = async () => {
const extent = await measureModel(src, { ground: true });
const place = placement.current?.findFreePlace({ extent, on, near: [0, 0] });
if (!place) return Alert.alert("No room for it");
onBought({ position: [...place.position], turn: place.turn });
};
return <Button title="Buy" onPress={buy} />;
}| Option | Default | Meaning |
|---|---|---|
size |
[width, height, depth] in metres, as <Box size>. Standing on its bottom's middle, or (on a wall) hung by its back's middle |
|
extent |
Its exact extent instead of size. For a model, get it from measureModel |
|
on |
"floor" |
"surface" tries the tops of surfaces first, then the floor; "wall" the walls |
id |
"" |
The id it will have, for canPlace. Or the id of a thing already placed. Then size and extent aren't needed (it is measured as it is), on, solid and turn default to its own, and it and what stands on it are not in its own way |
turn |
0 |
Its turn, degrees (on a wall, the wall's) |
near |
the middle | [x, z] to look near first |
step |
the snap grid, or 0.1 |
Metres between the places tried |
height |
1.5 |
On a wall: the height to hang it at, if there is room there |
solid |
true |
false for something others may overlap (a rug) |
Give size, extent, or the id of a thing already placed. Without any of them it throws. For a placed thing whose model is still loading, it returns null.
Letting new pieces find room: findRoom#
Add the new piece anywhere (the middle of the room, where the camera looks) with findRoom on its <Placeable>. When it first appears, or once its model has loaded, it is checked. If it doesn't fit there (in the way of others, outside the space, or refused by canPlace), it moves to the nearest place it fits before it is drawn. If it only fits turned a quarter, it is turned.
- The move reaches
onChangewith{ by: "arrival" }.layout.placekeeps it out of undo, so the piece is where it was bought. onArrive(id, place)reports where it landed, ornullwhen there was no room. With no room, the piece stays where it was put. Use it to show a sparkle or give a refund.
You can set findRoom on every movable piece, even in a large layout. A piece that already fits stays, with no change and no save. One that doesn't (from a layout made on another device, or in a room made smaller) moves to where it fits.
import { Box, type LayoutItem, Placeable, Placement, Scene, useLayout } from "@apricotjs/react-native";
import { useState } from "react";
import { Button, StyleSheet, View } from "react-native";
const START: LayoutItem[] = [];
export function Shop() {
const layout = useLayout(START, { save: "shop-room" });
// Pieces bought this session.
const [fresh, setFresh] = useState(() => new Set<string>());
const buy = () => {
const id = layout.newId("crate");
layout.add({ id, position: [0, 0, 0], turn: 0 }, { history: false });
setFresh((now) => new Set(now).add(id));
};
return (
<View style={{ flex: 1 }}>
<Scene style={StyleSheet.absoluteFill} look="soft" camera={{ position: [0, 6, 6] }}>
<Box size={[4, 0.1, 3]} position={[0, -0.05, 0]} color="#ebc79c" />
<Placement
bounds={[-2, -1.5, 2, 1.5]}
onChange={layout.place}
onArrive={(id, place) => {
if (!place) layout.remove(id, { history: false }); // no room: give the coins back here
}}
>
{layout.items.map((p) => (
<Placeable key={p.id} id={p.id} position={p.position} turn={p.turn} findRoom={fresh.has(p.id)}>
<Box size={[0.6, 0.5, 0.4]} position={[0, 0.25, 0]} color="#c98b5a" />
</Placeable>
))}
</Placement>
</Scene>
<View style={{ position: "absolute", bottom: 40, alignSelf: "center" }}>
<Button title="Buy a crate" onPress={buy} />
</View>
</View>
);
}When the space changes#
bounds, walls, areas, zones and canPlace can change at any time (a room grown or shrunk, a new rule) without a new key. The picked-up piece is checked again at once and reported through onFitChange.
Pieces are not moved for you. Pieces left outside the new space are reported, and you decide what to do. A piece is outside when it is wholly or partly outside the bounds or its area, or hung where there is no wall any more.
| Prop or method | Use |
|---|---|
onOutside(ids) |
Called whenever the set of pieces outside the space changes |
ref.outside() |
The ids outside now |
ref.moveInside(ids?) |
Moves the pieces (by default, all outside ones) with what stands on them to the nearest place they fit, near where they were. Reported to onChange as "code". Returns the ids it found no room for |
import { Box, type LayoutItem, Placeable, Placement, type PlacementHandle, Scene, useLayout } from "@apricotjs/react-native";
import { useRef, useState } from "react";
import { Button, StyleSheet, View } from "react-native";
const START: LayoutItem[] = [
{ id: "crate-1", position: [-2.4, 0, 1.5], turn: 0 },
{ id: "crate-2", position: [0, 0, 0], turn: 0 },
];
export function ResizableRoom() {
const layout = useLayout(START);
const placement = useRef<PlacementHandle>(null);
const [big, setBig] = useState(true);
const [leftOver, setLeftOver] = useState<string[]>([]);
const [w, d] = big ? [3, 2] : [2, 1.5];
return (
<View style={{ flex: 1 }}>
<Scene style={StyleSheet.absoluteFill} look="soft" camera={{ position: [0, 7, 7] }}>
<Box size={[2 * w, 0.1, 2 * d]} position={[0, -0.05, 0]} color="#ebc79c" />
<Placement
ref={placement}
bounds={[-w, -d, w, d]}
onChange={layout.place}
onOutside={setLeftOver}
>
{layout.items.map((p) => (
<Placeable key={p.id} id={p.id} position={p.position} turn={p.turn}>
<Box size={[0.6, 0.5, 0.4]} position={[0, 0.25, 0]} color="#c98b5a" />
</Placeable>
))}
</Placement>
</Scene>
<View style={{ position: "absolute", bottom: 40, alignSelf: "center", flexDirection: "row" }}>
<Button title={big ? "Shrink the room" : "Grow the room"} onPress={() => setBig((b) => !b)} />
{leftOver.length > 0 ? (
<Button title={`Bring ${leftOver.length} inside`} onPress={() => placement.current?.moveInside()} />
) : null}
</View>
</View>
);
}Why a place changed#
onChange(id, place, change) gets a third argument, { by }:
by |
The thing… |
|---|---|
"hand" |
was moved or turned by hand, or carried by what it stands on |
"code" |
was moved by the ref (turnBy, moveInside) |
"arrival" |
found room as it arrived (findRoom) |
layout.place takes all three arguments, so pass it straight to onChange.
Performance#
- Wrap the room and its
<Placement>in a<Bundle>to cut the CPU time per frame on Android. Dragged things inside it still move. - Use
<Merged>for pieces made of many shapes. - Draw each piece in a
memocomponent, so moving one doesn't re-render the others. - A thing casts no shadow while it moves.
See Performance and the furnish a room recipe.
Related#
- Placement reference.
- Ground and water: areas, no-go zones, snap points and floating things.
- Lighting a room: daylight through a window, lamps that come on at dusk.