Skip to content

Images

Updated

An image surface puts a picture in the world: a poster on a wall, a company logo, a 360 panorama around the player, an animated sticker. MIRA has one entity and one component for each image format, and they all share the same idea: a flat surface that shows a texture, sized in metres, with a src that names the file. This page explains each format, the props they share, how they size themselves, how animated formats play, and how a Behavior script swaps, fades and controls them.

This page relies on a few ideas explained elsewhere:

  • Entities and components. An image is an entity whose component holds its settings as props. Scripts read component.props and change them with component.setProps({ ... }). See Components.
  • Lifecycle hooks. Scripts declare startup and update with #pragma lifecycle(...). See Scripting.
  • Signals. A component’s events reach scripts only through a Signal Emitter on the same entity. The script listens with on(signal, callback) and compares event.eventType with an EventType constant. See Events and signals.
  • Media basics. The overview of Media and text introduces surfaces, sources and sizing.

Each format has its own entity and component. An entity draws only its own component’s picture.

Entity Component Script class Shows Use it for
ent-image comp-image ImageComponent A still image: PNG, JPEG, WebP, AVIF where the browser supports it, BMP, a static GIF. Most pictures.
ent-image-ktx2 comp-ktx2 KTX2Component A GPU-compressed .ktx2 texture (Basis UASTC or ETC1S). Large or numerous images, especially on phones and headsets.
ent-image-svg comp-svg SVGComponent A vector SVG drawn as flat shapes. Logos, icons and signs that must stay sharp up close.
ent-image-gif comp-gif GIFComponent An animated GIF. Short looping animations.
ent-lottie comp-lottie LottieComponent A Lottie (Bodymovin) JSON vector animation. Crisp animated stickers, icons and celebrations.

Why several formats? Each one trades something. A plain image is the simplest. A KTX2 file stays compressed in GPU memory, so a gallery of a hundred KTX2 posters uses a fraction of the memory the same posters take as JPEGs. An SVG is made of shapes rather than pixels, so it never blurs. GIF and Lottie move.

Every image entity also accepts a Frame and an Attribution, plus Grabbable, Selectable, Billboard, Snappable and the outline components. Image and KTX2 entities also accept a Cache Mesh (see Batching copies).

Image, KTX2, GIF and Lottie share one kind of surface:

  • Planar. A flat plane in the entity’s local XY plane, width by height metres.
  • Equirectangular. A sphere around the entity, for 360 images. width becomes the sphere’s radius.

The texture is cover-fitted: it fills the plane without stretching, and the side that overflows is cropped. A wide photo on a square plane shows its middle.

These props are the same on comp-image, comp-ktx2, comp-gif and comp-lottie.

Prop Type Default Description
src string none The image: a URL or an asset from the project.
width number 1 Plane width in metres. For equirectangular, the sphere’s radius.
height number 1 Plane height in metres. Ignored for equirectangular.
sizing 'manual' | 'auto' none (manual) auto fits the image’s own proportions inside width × height. Not on Lottie.
color string '#ffffff' A tint multiplied onto the texture. White shows the image unchanged.
zoom number 1 Texture scale. Above 1 zooms in.
grayscale number 0 Desaturation, from 0 (full colour) to 1 (grey).
opacity number 1 Overall opacity, from 0 (invisible) to 1. Fully transparent pixels never hide what is behind them.
projection 'planar' | 'equirectangular' 'planar' A flat plane, or a sphere for a 360 image.
side 'double' | 'front' | 'back' 'double' Which faces are drawn. Use back to see a 360 image from inside its sphere.

Changing src loads the new picture. The other props update the surface that is already showing, without reloading.

Intrinsic sizing means letting the picture decide its own shape. With sizing: 'auto', width and height become bounds: the plane takes the image’s aspect ratio and fits inside them.

  • A 16:9 image in 1 × 1 bounds becomes 1 × 0.5625. A portrait image in the same bounds fits by height.
  • Without sizing, or with 'manual', the plane is exactly width × height and the image is cropped to fill it.
  • The fitted size comes from the loaded file. It is never written back into the props or the transform, and the entity’s scale still applies on top.
  • Until the file has loaded, the plane uses the bounds.
  • A 360 surface ignores sizing. Lottie and SVG do not support it.

Use auto when a script swaps pictures of different shapes onto one frame, so none of them is cropped.

A 360 panorama (an equirectangular image) is a photo that covers every direction. To surround the player with one:

  1. Set projection: 'equirectangular'.
  2. Set side: 'back', so the inside of the sphere is drawn.
  3. Set width (the radius) large enough to surround the player, for example 50.

comp-image decodes through the browser, so it shows whatever the browser’s image decoder supports. It takes only the shared props. Its props can be shared with other players as a network trait, NetworkTraitType.Image (see Multiplayer).

comp-ktx2 shows a GPU-compressed .ktx2 texture. Every surface that shows the same file shares one copy of the texture. Prefer KTX2 for large or numerous images on mobile and XR: it loads fast and stays compressed in GPU memory. It takes only the shared props.

comp-svg turns the SVG’s filled paths into flat shapes, so it stays sharp at any distance. It has one prop:

Prop Type Default Description
src string none The SVG file: a URL or an asset from the project.

The drawing is normalized to a 1 × 1 box: size it with the entity’s scale, not with width and height. Copies of the same file share one parsed drawing.

comp-gif plays an animated GIF and comp-lottie plays a Lottie JSON animation. Both decode their frames off the main thread, so the render loop only uploads finished frames. They add these props to the shared ones:

Prop Type Default Description
autoplay boolean true Start playing once the frames are decoded.
repeat 'once' | 'repeat' 'repeat' once stops on the last frame; repeat starts again.
speed number 1 Playback speed multiplier, above 0 and at most 16.
maxFps number 30 The most frames per second the animation updates, 1 to 60. The scene still renders at full rate.
controls boolean true on GIF, false on Lottie A play/pause and repeat pill under a planar surface.
quality number 1 Lottie only. Raster resolution scale. Higher is sharper and costs more per frame.

On a Lottie, changing src or quality reloads the animation; the other props apply to the running one.

Method On What it does
play() GIFComponent, LottieComponent Ask the animation to play.
pause() GIFComponent, LottieComponent Ask it to hold its current frame.
setProps(patch) every image component Change props. A new src reloads.

A GIF or Lottie advances only while it can be seen:

  • In play, while its entity is active and shown. A hidden entity, or one under a hidden parent, decodes no frames.
  • In the editor, only while its entity is selected, so a scene full of animations costs nothing while you author it.

play() and pause() set what you want. The visibility rule still applies on top: a playing GIF on a hidden entity is paused until it is shown again, and sends GIF::pause and GIF::play as that happens.

A Cache Mesh component on an Image or KTX2 entity draws it together with every identical copy in a single draw call: same source, size, tint, zoom, grayscale, opacity and side. Use it for repeated static images, such as a row of identical posters or a tiled sign. See LOD and caching.

  • Planar surfaces only: a 360 sphere draws on its own.
  • Do not cache a surface under a mask.
  • Picking, frames and selection still work on each copy.

Each component sends these through the Signal Emitters on its entity.

Event EventType Data When
Image::ready EventType.Image.Ready { width, height }, the texture’s pixels The texture is shown.
Image::error EventType.Image.Error { message } The image cannot load.
KTX2::ready EventType.KTX2.Ready { width, height } The texture is shown.
KTX2::error EventType.KTX2.Error { message } The file cannot load.
SVG::ready EventType.SVG.Ready {} The drawing is shown.
SVG::error EventType.SVG.Error { message } The SVG cannot load.
GIF::ready, Lottie::ready EventType.GIF.Ready, EventType.Lottie.Ready { width, height, animated } The animation is decoded. animated is false for a single frame.
GIF::error, Lottie::error EventType.GIF.Error, EventType.Lottie.Error { message } The file cannot load or decode.
GIF::play, Lottie::play EventType.GIF.Play, EventType.Lottie.Play {} Playback actually starts.
GIF::pause, Lottie::pause EventType.GIF.Pause, EventType.Lottie.Pause {} Playback actually stops: paused, its entity hidden or disabled, or its scene left.
GIF::end, Lottie::end EventType.GIF.End, EventType.Lottie.End {} An animation set to play once reaches its last frame.

Every example below assumes the entity names given in its introduction. Where a script listens for events, the entity carries a Signal Emitter whose signal is the one the script names.

A poster that shows the next picture each time it is clicked. The scene has an Image entity named Poster with a Selectable (so it can be clicked) and a Signal Emitter with the signal poster. The three paths are the pictures of the live example below, served with these docs under /example-assets/; use your own files or project assets in their place.

PosterCycle.jx
#pragma lifecycle(startup)
const POSTERS = [
'/example-assets/lemon-decal.png',
'/example-assets/bang.png',
'/example-assets/alpha.png',
];
let poster;
let index = 0;
function startup() {
poster = findEntity('Poster').getComponent(ImageComponent);
poster.setProps({ src: POSTERS[0], sizing: 'auto', width: 1.6, height: 1.6 });
on('poster', (event) => {
if (event.eventType === EventType.Selectable.Select) {
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
index = (index + 1) % POSTERS.length;
poster.setProps({ src: POSTERS[index] });
}
if (event.eventType === EventType.Image.Ready) {
log(`Poster ${index + 1}: ${event.data.width} x ${event.data.height} px`);
}
if (event.eventType === EventType.Image.Error) {
console.warn(`Poster ${index + 1} failed: ${event.data.message}`);
}
});
}
  • findEntity('Poster') runs once, in startup, and the component is kept in a variable. Looking it up every frame would walk the scene each time.
  • sizing: 'auto' with 1.6 × 1.6 bounds lets a landscape and a portrait poster both fit without cropping.
  • One on('poster', ...) listener handles the click (Selectable::select) and the image’s own ready and error events, because the Signal Emitter forwards every event of the entity.
  • event.data holds the event’s details: the pixel size for ready, a message for error.

2. A slideshow that fades between pictures

Section titled “2. A slideshow that fades between pictures”

A picture frame that holds each slide for four seconds, fades it out, swaps the file, waits for it to load and fades back in. The Behavior entity is a child of an Image entity that has a Signal Emitter with the signal slides. The slides are the live example’s own pictures, under /example-assets/.

Slideshow.jx
#pragma lifecycle(startup)
const SLIDES = [
'/example-assets/lemon-decal.png',
'/example-assets/bang.png',
'/example-assets/alpha.png',
];
const HOLD_MS = 4000;
const FADE_SECONDS = 0.6;
let image;
function wait(ms) {
return new Promise((done) => delay(done, ms));
}
// Resolves on the next ready or error event of this image.
function nextLoad() {
return new Promise((done) => {
const stop = on('slides', (event) => {
if (event.eventType === EventType.Image.Ready || event.eventType === EventType.Image.Error) {
stop();
done();
}
});
});
}
function fade(from, to) {
return Tween.value(from, to, (value) => image.setProps({ opacity: value }), {
duration: FADE_SECONDS,
ease: Ease.InOutSine,
});
}
async function startup() {
image = self.parent.getComponent(ImageComponent);
let index = 0;
for (;;) {
await wait(HOLD_MS);
await fade(1, 0);
index = (index + 1) % SLIDES.length;
const loaded = nextLoad();
image.setProps({ src: SLIDES[index] });
await loaded;
await fade(0, 1);
}
}
  • Scripts have no setTimeout. wait wraps delay in a promise so an async hook can await it.
  • nextLoad starts listening before src changes, so it cannot miss a fast Image::ready. The function on returns stops the listener once it has fired.
  • Tween.value animates a plain number and hands each value to setProps({ opacity }). Opacity updates the live surface without reloading, so this is cheap.
  • The loop never ends on its own. When the script is released, its tweens and delays stop and the awaits simply never resume.

A Lottie celebration that appears, plays once and hides itself. The scene has a Lottie entity named Confetti, saved disabled, with a Signal Emitter whose signal is confetti. Another script calls emit('goal/reached').

Confetti.jx
#pragma lifecycle(startup)
let entity;
let lottie;
function startup() {
entity = findEntity('Confetti');
lottie = entity.getComponent(LottieComponent);
lottie.setProps({ repeat: 'once', autoplay: false, speed: 1.5 });
on('goal/reached', () => {
entity.setEnabled(true);
lottie.play();
});
on('confetti', (event) => {
if (event.eventType === EventType.Lottie.End) entity.setEnabled(false);
if (event.eventType === EventType.Lottie.Error) log('Confetti failed:', event.data.message);
});
}

In the live example, a button sends the goal. Its entity has a Selectable and a Signal Emitter whose signal is goal/button:

GoalButton.jx
#pragma lifecycle(startup)
function startup() {
on('goal/button', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
emit('goal/reached');
});
}
  • repeat: 'once' makes the animation stop on its last frame and send Lottie::end.
  • autoplay: false keeps it from starting by itself; the script calls play() when the goal is reached.
  • A disabled entity decodes nothing, so a hidden celebration costs nothing until it is needed.
  • setEnabled(false) on Lottie::end hides it again, ready for the next goal.

A GIF that the player can pause and resume by clicking it, with a lower frame rate on phones and headsets. The Behavior entity is a child of a GIF entity with a Selectable and a Signal Emitter whose signal is sticker.

StickerToggle.jx
#pragma lifecycle(startup)
#if IS_MOBILE || IS_HEADSET
const FPS = 12;
#else
const FPS = 24;
#endif
let gif;
let playing;
function startup() {
gif = self.parent.getComponent(GIFComponent);
gif.setProps({ maxFps: FPS, controls: false });
playing = gif.props.autoplay; // an autoplaying GIF may start before this script does
on('sticker', (event) => {
switch (event.eventType) {
case EventType.GIF.Ready:
if (!event.data.animated) log('This GIF has a single frame');
break;
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;
}
});
}
  • #if IS_MOBILE || IS_HEADSET picks the frame rate when the script is compiled, so the other branch costs nothing at run time.
  • playing starts from autoplay: a GIF that is decoded while the scene loads starts playing before the script’s startup runs, so the script never sees that first GIF::play.
  • After that, playing follows the GIF::play and GIF::pause events rather than the script’s own calls. The events report what actually happened, including a pause caused by the entity being hidden.
  • controls: false removes the built-in pill, since a click on the sticker now does the same job.

An SVG logo that breathes gently. The Behavior entity is a child of an SVG entity. Because an SVG is sized by its entity’s scale, the script animates the scale.

LogoPulse.jx
#pragma lifecycle(startup)
function startup() {
const logo = self.parent;
logo.scale.set(1.2, 1.2, 1.2); // the 1 x 1 drawing becomes 1.2 m wide
Tween.to(logo.scale, { x: 1.3, y: 1.3, z: 1.3 }, {
duration: 1.4,
ease: Ease.InOutSine,
repeat: Infinity,
yoyo: true,
});
}
  • An SVG drawing always fits a 1 × 1 box, so scale sets its size in metres.
  • repeat: Infinity with yoyo: true plays the tween forwards and backwards for ever.

A viewer that surrounds the player with a 360 photo and switches to the next one when the player presses the right arrow key. The scene has an Image entity named Panorama.

PanoramaViewer.jx
#pragma lifecycle(startup, update)
const PLACES = [
{ name: 'Lighthouse', src: '/assets/360/lighthouse.jpg' },
{ name: 'Canyon', src: '/assets/360/canyon.jpg' },
{ name: 'Old library', src: '/assets/360/library.jpg' },
];
let panorama;
let index = 0;
function startup() {
panorama = findEntity('Panorama').getComponent(ImageComponent);
panorama.setProps({
src: PLACES[0].src,
projection: 'equirectangular',
side: 'back',
width: 50, // the sphere's radius, in metres
});
log(`Now in: ${PLACES[0].name}`);
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.ArrowRight)) return;
index = (index + 1) % PLACES.length;
panorama.setProps({ src: PLACES[index].src });
log(`Now in: ${PLACES[index].name}`);
}
  • projection, side and width together turn the plane into a sphere the player stands inside.
  • isKeyReleased is true for one frame after the key goes up, so one press moves one panorama. isKeyPressed would be true on every frame the key is held.
  • Only src changes afterwards, so the sphere keeps its shape.
  • Prefer KTX2 for many or large images on phones and headsets. It loads faster and uses far less GPU memory.
  • Lower maxFps on animations that do not need it: a sticker at 15 frames a second costs half the uploads of one at 30.
  • Keep Lottie quality at 1 unless the surface fills much of the view.
  • Hide animations that are out of sight. A disabled or hidden GIF or Lottie decodes nothing.
  • Batch identical posters with a Cache Mesh.
  • Images are unlit: they show their own colours and are not affected by scene lights, so lighting changes cost nothing for them.
  • Changing src every frame. Each new src loads the file again. Change it only when the picture really changes.
  • Sizing an SVG with width and height. An SVG has only src; size it with the entity’s scale.
  • Expecting events without a Signal Emitter. Image::ready, GIF::end and the rest reach scripts only through a Signal Emitter on the image’s entity.
  • Listening for ready after changing src. A cached file can be ready almost at once. Start listening first, as the slideshow example does.
  • A 360 image that looks inside out or invisible. Set side: 'back' so the inside of the sphere is drawn, and make width (the radius) large enough to surround the player.
  • Calling play() on a hidden GIF and expecting it to move. Visibility wins: the animation plays only once its entity is enabled and shown.
  • Using sizing on Lottie or SVG. Neither supports it.