Snap zones
Updated
A Snap Zone is an invisible box that attracts flat media dragged near it and lays it out in a grid: a gallery wall, a mood board, a shelf of documents. A Snappable on an image, a video or a PDF is what allows it to snap in. Players simply drag items with a Grabbable; the zone shows where the item will go, catches it on release and keeps the grid tidy as items come and go. Your scripts hear about every item that snaps in or out.
Before you start
Section titled “Before you start”You should know:
- How Grabbable works, since snapping reacts only to Grabbable drags; see Selection and grabbing.
- How events reach a script through a Signal Emitter; see Events and signals.
- What media entities (Image, Video, PDF…) are; see Media and text.
Setting up a zone
Section titled “Setting up a zone”-
Add a Snap Zone entity. Its Snap Zone component (
comp-snap-zone) is required. Place and turn it so its front (its local XY plane) faces the room. -
On each media entity that may snap in, add a Snappable (
comp-snappable) and a Grabbable. Dragging needs the Grabbable. -
Optionally give the zone and the items
groups, so a zone takes only some items. -
To hear about changes, add a Signal Emitter to the zone, the items, or both.
Media entities placed as children of the zone in the saved scene are its items when the scene loads.
How snapping works
Section titled “How snapping works”- Hint. While the player drags an item the zone could take, the zone shows a frame and a ghost of the slot the item would take.
- Candidate. When the item’s origin comes within the zone’s bounds, grown by
snap, it becomes the candidate. Drop it there and it snaps in: it moves under the zone, its rotation is squared up, and the grid lays it out. - Unsnap. Dragging an item out past the same distance unsnaps it at once, and the grid closes up. When the drag ends, it goes back under the parent it came from.
- Staying put. Pressing an item without dragging it out keeps its slot.
- Layout. Each item keeps its own size. Columns and rows take the widest and tallest item in them:
columnsitems per row,gapbetween items, on the zone’s local XY plane. - Bounds. The bounds grow to wrap the items, never smaller than
width×height, withmarginaround them anddepthdeep, centred on the zone’s origin. - Capacity. A zone takes at most
capacityitems. Overlapping zones never share an item: the first to claim it wins.
Snap Zone
Section titled “Snap Zone”| Prop | Type | Default | Description |
|---|---|---|---|
appearance |
'hint', 'frame' or 'invisible' |
'hint' |
How it shows in play; see Appearance. |
capacity |
integer | 6 |
Most items it holds, 1 to 256. |
groups |
string array | [] |
Groups it takes. Empty takes any Snappable. |
columns |
integer | 3 |
Items in a row before the next row starts, 1 to 256. |
gap |
number | 0.05 |
Space between items, in local units. |
margin |
number | 0.05 |
Space between the items and the bounds, in local units. |
depth |
number | 0.1 |
Thickness of the bounds along local Z. |
width |
number | 1 |
Smallest width of the bounds before margin: an empty zone’s. |
height |
number | 1 |
Smallest height of the bounds before margin: an empty zone’s. |
snap |
number | 0.25 |
How far past the bounds a dragged item is still pulled in. |
color |
string | '#7a8cff' |
Frame colour at rest. |
highlight |
string | '#00ffa3' |
Frame and ghost colour while an item is about to snap in. |
animation |
string | 'sweep' |
How the frame and ghost animate while a takeable item is held: sweep, fade, blink, stripes, rainbow or none. |
Appearance
Section titled “Appearance”hintdraws a glowing frame only while an item it could take is being dragged.framealways draws it.invisibledraws nothing of its own in play. It still snaps, lays out and sends its events.
With hint and frame, the frame is color at rest. While a takeable item is held, it animates as animation says; once the item is about to snap in, the frame and ghost turn highlight. rainbow flows through the hues and ignores highlight. Animations run in play only.
To draw the zone your own way, set appearance: 'invisible' and put your own object on the zone as a child: any child that is not a Snappable it takes. Fit it to the bounds yourself, using size and the SnapZone::resize event.
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Snap |
Event |
{ entityId } |
A media entity was dropped in. |
Snap |
Event |
{ entityId } |
A media entity was dragged out. |
Snap |
Event |
{ entity |
Right after either, snapped saying which. |
Snap |
Event |
{ width, height, depth } |
The bounds changed, the first layout included. |
data.entityId is the item; event.entityId is the zone. Items already in the zone when the scene loads are not announced.
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
contents |
The snapped entities, in layout order. |
size |
The bounds { width, height, depth } in local units, centred on the zone’s origin. |
Snappable
Section titled “Snappable”Snappable marks a flat media entity as something a zone may take. It is offered on Image, KTX2, GIF, SVG, Video, PDF and Lottie entities. Zones only attract enabled Snappables.
| Prop | Type | Default | Description |
|---|---|---|---|
groups |
string array | [] |
The groups it belongs to. |
How groups match:
- A zone that lists no groups takes every Snappable.
- A Snappable that lists no groups snaps only into zones that list none.
- Otherwise, the zone and the item need at least one group in common.
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Snappable::snap |
Event |
{ zoneId } |
It snapped into the zone with that entity id. |
Snappable::unsnap |
Event |
{ zoneId } |
It was dragged out of it. |
They are sent on the item’s own Signal Emitter, after the zone’s events.
Examples
Section titled “Examples”1. Count what is on the wall
Section titled “1. Count what is on the wall”#pragma lifecycle(startup)
// The Gallery is a Snap Zone with a Signal Emitter on 'gallery'.function startup() { const zone = findEntity('Gallery').getComponent(SnapZoneComponent); const label = findEntity('Gallery Label').getComponent(UILabelComponent);
const show = () => label.setProps({ text: `${zone.contents.length} of ${zone.props.capacity} pictures` }); show();
on('gallery', (event) => { if (event.eventType === EventType.SnapZone.Change) show(); });}A label counts the pictures on a Snap Zone: it reads the zone's contents and capacity at startup and after every SnapZone::change.
Drag the pictures onto the frame, and off again: the label counts them. Drag elsewhere to orbit the camera, scroll to zoom.
contentsis the list of snapped entities, so itslengthis how many are on the wall.zone.props.capacityreads the zone’s limit.SnapZone::changefollows every snap and unsnap, so one check keeps the label up to date.show()also runs once at startup, because items placed in the zone in the saved scene are not announced.
2. A custom backdrop that fits the zone
Section titled “2. A custom backdrop that fits the zone”#pragma lifecycle(startup)
// The Board zone is invisible, with a Signal Emitter on 'board'.// Its child Backdrop is a plane mesh one unit wide and one unit tall.function startup() { const zone = findEntity('Board').getComponent(SnapZoneComponent); const backdrop = findObject('Backdrop');
const fit = ({ width, height }) => backdrop.scale.set(width, height, 1); fit(zone.size);
on('board', (event) => { if (event.eventType === EventType.SnapZone.Resize) fit(event.data); });}An invisible Snap Zone wears a backdrop of its own: a one-by-one plane the script scales to the zone's size at startup and on every SnapZone::resize, as items fill the grid.
Drag the pictures onto the backdrop: it grows as the grid fills, and shrinks when you take them off. Drag elsewhere to orbit the camera, scroll to zoom.
- With
appearance: 'invisible', the zone draws nothing, and the Backdrop child is its look. sizegives the current bounds;SnapZone::resizearrives whenever they change, with the newwidth,heightanddepth.- Scaling a one-by-one plane to the bounds makes the backdrop grow and shrink with the grid.
findObjectfinds any object by name, including a plain mesh.
3. A sorting puzzle
Section titled “3. A sorting puzzle”#pragma lifecycle(startup)
const TOTAL = 6;
function startup() { const warm = findEntity('Warm').getComponent(SnapZoneComponent); const cool = findEntity('Cool').getComponent(SnapZoneComponent);
const correct = (zone, prefix) => zone.contents.filter((item) => item.name.startsWith(prefix)).length;
on('sorting', (event) => { if (event.eventType !== EventType.SnapZone.Change) return; const right = correct(warm, 'Warm') + correct(cool, 'Cool'); log(`${right} of ${TOTAL} sorted`); if (right === TOTAL) emit('puzzle/solved'); });}Two Snap Zones that take every swatch, and a script that counts on each change how many swatches sit in the zone their name matches.
Drag the warm swatches into the left frame and the cool ones into the right. Scroll to zoom.
- Both zones accept every swatch, so the player has to decide where each goes.
- On every change in either zone, the script counts the swatches in the right zone by their names.
- When all six are correctly placed, it emits
puzzle/solved, which another script (or an Enable on Signal component) can react to.
4. Items that react to being placed
Section titled “4. Items that react to being placed”#pragma lifecycle(startup)
// This Behavior sits on a Video entity with a Snappable, a Grabbable,// and a Signal Emitter on 'clip'.function startup() { const video = self.parent.getComponent(VideoComponent);
on('clip', (event) => { if (event.entityId !== self.parent.entityId) return; if (event.eventType === EventType.Snappable.Snap) { log(`Placed on ${findEntity(event.data.zoneId)?.name}`); video.play(); } if (event.eventType === EventType.Snappable.Unsnap) video.pause(); });}A video clip with a Snappable plays when it snaps onto a Snap Zone and pauses when it is taken off, from its own Snappable events.
Drag the clip onto the frame to play it, and off again to pause it. Drag elsewhere to orbit the camera, scroll to zoom.
- The Snappable sends its own events on the item’s Signal Emitter, with
zoneIdnaming the zone it snapped into. - Comparing
event.entityIdwithself.parent.entityIdlets several videos share the signalclipwhile each script handles only its own. - Placed on a wall, the video plays; taken off, it pauses. See Media and text for video playback.
Common mistakes
Section titled “Common mistakes”- Items without a Grabbable. A Snappable alone cannot be dragged. Add a Grabbable to each item.
- Groups that never match. A Snappable with no groups snaps only into zones with no groups. Give both sides a common group, or leave both empty.
- Using a Handle. Zones react to Grabbable drags only; a Handle drag never snaps.
- Waiting for events about saved items. Items already in the zone when the scene loads are not announced. Read
contentsat startup. - Snapping 3D models. Snappable is offered on flat media only: images, videos, PDFs, GIFs, SVGs, KTX2 textures and Lottie animations.