Skip to content

Image GIF

Updated

An animated GIF surface.

Type
ent-image-gif
Script class
GIFEntity
Family
Media

An Image GIF entity plays an animated GIF on a plane: a sticker, a looping sign, a small animated decoration. Frames are decoded off the main thread, so the animation does not slow the rest of the world down.

An animation plays only while it can be seen. In play, it runs while its entity is enabled and shown; a hidden entity decodes nothing. In the editor, it plays only while the entity is selected, so a scene full of GIFs costs nothing while you build it. By default a small play/pause pill sits under the plane.

Pick a sibling when it fits better: a Lottie for crisp vector animation that scales well, a Video for long or high-resolution footage with sound, or an Image for a still picture.

The props that matter most, on its GIF component (GIFComponent in scripts):

  • src: the GIF file, as a URL or an asset from your project.
  • autoplay: start playing once the frames are decoded.
  • repeat: repeat loops; once stops on the last frame and sends GIF::end.
  • speed: the playback rate, above 0 and up to 16; 2 plays twice as fast.
  • maxFps: the most frames per second the animation updates, 1 to 60. The scene still renders at full rate.
  • controls: the play/pause and repeat pill under the plane.

It also has the surface props of an Image: width, height, sizing, opacity, color, grayscale, zoom, projection and side. Scripts control playback with play() and pause().

  1. Add an Image GIF entity to the scene and name it, such as Sticker.
  2. Set its src to an animated GIF, and its width and height in metres, or set sizing to auto to keep the GIF’s proportions.
  3. Choose repeat, and turn controls off if visitors should not pause it.
  4. Lower maxFps for decorations that do not need smooth motion.
  5. Add a Signal Emitter to hear its events in scripts, and a Selectable to make it clickable.

Let the player stop and restart a GIF by clicking it.

GifClickToggle.jx
#pragma lifecycle(startup)
// Scene: an Image GIF entity "Sticker" with a Selectable and a
// Signal Emitter on the signal 'sticker'.
let gif;
let playing = false;
function startup() {
gif = findEntity('Sticker').getComponent(GIFComponent);
on('sticker', (event) => {
switch (event.eventType) {
case EventType.GIF.Play:
playing = true;
break;
case EventType.GIF.Pause:
playing = false;
break;
case EventType.Selectable.Select:
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
if (playing) gif.pause();
else gif.play();
break;
}
});
}

How it works:

  • GIF::play and GIF::pause report what really happens, whoever caused it: autoplay, the pill, a script, or the entity being hidden. The script keeps playing in step with them.
  • A click (Selectable::select) calls pause() or play() from that state.

Play a celebration GIF once when a goal is reached, then switch it off.

GifCelebrate.jx
#pragma lifecycle(startup)
// Scene: an Image GIF entity "Celebrate", saved disabled, with
// repeat: once and a Signal Emitter on the signal 'celebrate'.
let entity;
function startup() {
entity = findEntity('Celebrate');
on('goal/reached', () => {
entity.setEnabled(true);
entity.getComponent(GIFComponent).play();
});
on('celebrate', (event) => {
if (event.eventType === EventType.GIF.End) entity.setEnabled(false);
if (event.eventType === EventType.GIF.Error) log('GIF failed:', event.data.message);
});
}

How it works:

  • The entity is saved disabled so nothing shows until the goal; setEnabled(true) brings it in.
  • With repeat: 'once', GIF::end arrives at the last frame, and the script switches the entity off again.

Use a lower update rate for every GIF in the scene on phones and headsets.

GifBudget.jx
#pragma lifecycle(startup)
#if IS_MOBILE || IS_HEADSET
const FPS = 15;
#else
const FPS = 30;
#endif
function startup() {
let count = 0;
scene.entities.forEach((entity) => {
const gif = entity.getComponent(GIFComponent);
if (!gif) return;
gif.setProps({ maxFps: FPS });
count += 1;
});
log('Set maxFps to', FPS, 'on', count, 'GIFs');
}

How it works:

  • #if IS_MOBILE || IS_HEADSET picks the frame rate when the script is compiled: 15 on phones and headsets, 30 elsewhere.
  • scene.entities holds every entity of the scene; the script changes only those with a GIF component.
  • maxFps applies to the running animation without reloading it.
  • play() and pause() say what you want; the visibility rule still applies on top, so a hidden GIF stays still after play() and starts when shown.
  • Changing src reloads the GIF; the other props apply to the running animation.
  • GIF::ready carries animated: false means the file has a single frame, and an Image would do the same job.

What a new Image GIF 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.
GIF comp-gif Required One An animated GIF surface.

You can also add 18 optional components, such as Animation Graph, Animation Timeline, Attribution, Billboard, Enable On Signal, Follow Target.

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
repeat "repeat"
width 1 0 ≤ x ≤ 10000
height 1 0 ≤ x ≤ 10000
color "#ffffff"
zoom 1 0.001 ≤ x ≤ 1000
grayscale 0 0 ≤ x ≤ 1
opacity 1 0 ≤ x ≤ 1
projection "planar"
side "double"
maxFps 30 1 ≤ x ≤ 60
autoplay true
speed 1 0 < x ≤ 16
controls true

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

  • GIF::ready
  • GIF::error
  • GIF::play
  • GIF::pause
  • GIF::end

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 Image GIF.

GIFEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Image GIF');
if (!(entity instanceof GIFEntity)) return;
// Its GIF component holds the settings listed under Props.
const component = entity.getComponent(GIFComponent);
log('width is', component.props.width);
component.setProps({ width: 1.5 });
}