Skip to content

Snap Zone

Updated

A box that snaps media into a neat grid.

Type
ent-snap-zone
Script class
SnapZoneEntity

A Snap Zone is a box that pulls flat media into a neat grid when the player drops it nearby: a gallery wall where photos line up, a mood board, a shelf of documents, a slot where a video card must be placed. While the player drags an item the zone can take, the zone shows a glowing frame and a ghost of the slot the item would land in. Dropped close enough, the item snaps in: it moves under the zone, its rotation is squared up, and the grid lays it out. Dragged back out, it leaves the grid, which closes up, and returns to the parent it came from.

Its behaviour comes from the required Snap Zone component (SnapZoneComponent in scripts). The items it takes are media entities (image, KTX2, GIF, SVG, video, PDF, Lottie) that carry a Snappable component, which marks them as snappable, and a Grabbable, which lets the player drag them. Media entities placed as children of the zone in the saved scene are its items when the scene loads. Snapping happens in play only; in the editor the frame shows steady so you can place and size the zone.

Use it whenever players arrange media by hand. For dragging objects freely, a Grabbable alone is enough; for moving, turning and scaling with a gizmo, use a Pivotable.

The props that matter most:

  • capacity: the most items the zone holds, 1 to 256.
  • columns and gap: how many items go in a row before the next row starts, and the space between them, in metres. Each row and column takes the size of its largest item.
  • width and height: the smallest size of the zone, which is the size of an empty zone. The bounds grow to wrap the items, with margin around them, and are depth thick.
  • snap: how far past the bounds a dragged item is still pulled in, in metres.
  • groups: the groups the zone takes. Empty takes any Snappable; otherwise it takes only Snappables that share a group with it. A Snappable with no groups snaps only into zones that list none.
  • appearance: hint (the default) shows the frame only while an item it could take is dragged, frame always shows it, invisible draws nothing of its own. color, highlight and animation style the frame.

From a script, the component also exposes contents, the snapped entities in layout order, and size, the current bounds { width, height, depth }.

  1. Add a Snap Zone entity to the scene and place it where the items should line up, facing the player: the grid is laid out on the zone’s local XY plane.
  2. Set width and height to the empty size you want, columns and gap for the layout, and capacity for the number of items.
  3. On each media entity that may snap in, add a Snappable and a Grabbable.
  4. To sort items between several zones, give the zones and the Snappables matching groups, such as photos and documents.
  5. Choose an appearance, and pick color and highlight to suit the scene.
  6. To react to items coming and going, add a Signal Emitter to the zone and set its signal, for example gallery.

Report how many items the gallery holds each time one is added or removed.

GalleryCount.jx
#pragma lifecycle(startup)
// Scene: a Snap Zone "Gallery" with a Signal Emitter on the signal 'gallery'.
// Images around the room have a Snappable and a Grabbable.
function startup() {
const zone = findEntity('Gallery').getComponent(SnapZoneComponent);
log('The gallery starts with', zone.contents.length, 'of', zone.props.capacity, 'items');
on('gallery', (event) => {
if (event.eventType !== EventType.SnapZone.Change) return;
const item = findEntity(event.data.entityId);
const verb = event.data.snapped ? 'added' : 'removed';
log(item ? item.name : event.data.entityId, verb, '-', zone.contents.length, 'on the wall');
});
}

How it works:

  • zone.contents lists the snapped entities, including those placed in the zone in the saved scene; props.capacity is the limit.
  • SnapZone::change follows every snap and unsnap, with data.entityId naming the item and data.snapped saying which happened.
  • Items already in the zone when the scene loads are not announced, which is why the script reads contents once at startup.

Lock each item in place once it snaps in, and announce when the board is full.

PuzzleBoard.jx
#pragma lifecycle(startup)
// Scene: a Snap Zone "Board" (capacity 4) with a Signal Emitter on the signal 'board'.
// Four puzzle tiles (images) have a Snappable and a Grabbable.
let zone;
let solved = false;
function startup() {
zone = findEntity('Board').getComponent(SnapZoneComponent);
on('board', (event) => {
if (event.eventType !== EventType.SnapZone.Snap) return;
// Keep the tile on the board: it can no longer be dragged out.
const tile = findEntity(event.data.entityId);
const grab = tile && tile.getComponent(GrabbableComponent);
if (grab) grab.setEnabled(false);
if (!solved && zone.contents.length >= zone.props.capacity) {
solved = true;
zone.setProps({ appearance: 'frame', color: '#00ffa3' });
emit('door/open');
}
});
}

How it works:

  • SnapZone::snap arrives when an item is dropped in, with data.entityId.
  • Disabling the tile’s Grabbable with setEnabled(false) stops the player from dragging it out again.
  • When contents reaches capacity, the script switches the zone to an always-visible green frame with setProps and emits door/open for the door’s script, or for an Enable on Signal component, to react.

A screen slot that plays the video dropped in

Section titled “A screen slot that plays the video dropped in”

Start a video when its card snaps into a screen slot, and stop it when it is taken out.

ScreenSlot.jx
#pragma lifecycle(startup)
// Scene: a Snap Zone "Screen Slot" (capacity 1, groups ['videos']).
// Video entities have a Snappable (groups ['videos']), a Grabbable,
// and a Signal Emitter on the signal 'video/snap'.
let slotId;
function startup() {
slotId = findEntity('Screen Slot').entityId;
on('video/snap', (event) => {
const snapped = event.eventType === EventType.Snappable.Snap;
const unsnapped = event.eventType === EventType.Snappable.Unsnap;
if ((!snapped && !unsnapped) || event.data.zoneId !== slotId) return;
const video = findEntity(event.entityId).getComponent(VideoComponent);
if (snapped) video.play();
else video.stop();
});
}

How it works:

  • The events come from the items this time: a Snappable sends Snappable::snap and Snappable::unsnap on its own entity’s Signal Emitter, with data.zoneId, the entity id of the zone.
  • Matching groups (videos on both sides) means the slot takes only video cards, and capacity: 1 makes it hold one at a time.
  • The video’s own events (Video::play and the rest) arrive on the same signal, so the script first keeps only the two Snappable events, then only those for this slot.
  • event.entityId is the video entity whose emitter sent the event, so the script plays exactly the card that was dropped in.

Draw the zone with your own mesh, sized to the bounds as items arrive.

BoardBackdrop.jx
#pragma lifecycle(startup)
// Scene: a Snap Zone "Board" (appearance: invisible) with a Signal Emitter on 'board'.
// As its child, a 1 × 1 plane mesh named "Backdrop" (no Snappable).
function startup() {
const zone = findEntity('Board').getComponent(SnapZoneComponent);
const backdrop = findEntity('Backdrop');
const fit = (size) => backdrop.scale.set(size.width, size.height, 1);
fit(zone.size);
on('board', (event) => {
if (event.eventType === EventType.SnapZone.Resize) fit(event.data);
});
}

How it works:

  • With appearance: 'invisible' the zone draws nothing of its own in play, but still snaps, lays out and sends its events.
  • zone.size gives the current bounds; SnapZone::resize is sent whenever they change, its first layout included, with { width, height, depth } as data.
  • The backdrop is a 1 × 1 plane, so scaling it by the width and height makes it cover the bounds exactly. It is a child without a Snappable, so the zone leaves it alone.
  • Lowering capacity never removes items already in the zone; it only stops new ones from coming in.
  • Overlapping zones never share an item: the first zone to claim it wins. Space zones apart, or use groups to keep their items separate.
  • The layout runs only when an item comes, goes or changes size, never every frame. An invisible zone costs nothing to draw.

What a new Snap Zone is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Snap Zone comp-snap-zone Required One A box that snaps media into a neat grid.

You can also add 19 optional components, such as Animation Graph, Animation Timeline, Billboard, Box Collider, Capsule Collider, Character Controller.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
appearance "hint"
capacity 6 1 ≤ x ≤ 256
groups []
columns 3 1 ≤ x ≤ 256
gap 0.05 0 ≤ x ≤ 10000
margin 0.05 0 ≤ x ≤ 10000
depth 0.1 0 ≤ x ≤ 10000
width 1 0 ≤ x ≤ 10000
height 1 0 ≤ x ≤ 10000
snap 0.25 0 ≤ x ≤ 10000
color "#7a8cff"
highlight "#00ffa3"
animation "sweep"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • SnapZone::snap
  • SnapZone::unsnap
  • SnapZone::change
  • SnapZone::resize

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Snap Zone.

SnapZoneEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Snap Zone');
if (!(entity instanceof SnapZoneEntity)) return;
// Its Snap Zone component holds the settings listed under Props.
const component = entity.getComponent(SnapZoneComponent);
log('capacity is', component.props.capacity);
component.setProps({ capacity: 9 });
}