Skip to content

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.

You should know:

  1. 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.

  2. On each media entity that may snap in, add a Snappable (comp-snappable) and a Grabbable. Dragging needs the Grabbable.

  3. Optionally give the zone and the items groups, so a zone takes only some items.

  4. 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.

  • 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: columns items per row, gap between items, on the zone’s local XY plane.
  • Bounds. The bounds grow to wrap the items, never smaller than width × height, with margin around them and depth deep, centred on the zone’s origin.
  • Capacity. A zone takes at most capacity items. Overlapping zones never share an item: the first to claim it wins.
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.
  • hint draws a glowing frame only while an item it could take is being dragged.
  • frame always draws it.
  • invisible draws 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.

eventType EventType constant data When
SnapZone::snap EventType.SnapZone.Snap { entityId } A media entity was dropped in.
SnapZone::unsnap EventType.SnapZone.Unsnap { entityId } A media entity was dragged out.
SnapZone::change EventType.SnapZone.Change { entityId, snapped } Right after either, snapped saying which.
SnapZone::resize EventType.SnapZone.Resize { 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.

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 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.
eventType EventType constant data When
Snappable::snap EventType.Snappable.Snap { zoneId } It snapped into the zone with that entity id.
Snappable::unsnap EventType.Snappable.Unsnap { zoneId } It was dragged out of it.

They are sent on the item’s own Signal Emitter, after the zone’s events.

GalleryCounter.jx
#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();
});
}
  • contents is the list of snapped entities, so its length is how many are on the wall. zone.props.capacity reads the zone’s limit.
  • SnapZone::change follows 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.
FitBackdrop.jx
#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);
});
}
  • With appearance: 'invisible', the zone draws nothing, and the Backdrop child is its look.
  • size gives the current bounds; SnapZone::resize arrives whenever they change, with the new width, height and depth.
  • Scaling a one-by-one plane to the bounds makes the backdrop grow and shrink with the grid. findObject finds any object by name, including a plain mesh.
SortingPuzzle.jx
#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');
});
}
  • 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.
SnappedVideo.jx
#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();
});
}
  • The Snappable sends its own events on the item’s Signal Emitter, with zoneId naming the zone it snapped into.
  • Comparing event.entityId with self.parent.entityId lets several videos share the signal clip while each script handles only its own.
  • Placed on a wall, the video plays; taken off, it pauses. See Media and text for video playback.
  • 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 contents at startup.
  • Snapping 3D models. Snappable is offered on flat media only: images, videos, PDFs, GIFs, SVGs, KTX2 textures and Lottie animations.