Skip to content

Show a picture from data ​

A picture in data (a photo from a camera, a map, a drawing) arrives as a ready data URI that goes into the src of an <img> as it is. Until the first picture arrives, the view shows a short note. The gallery in the example shows a new picture every two seconds.

jsx
import { useWorld } from './useWorld.js';

// Three pictures, drawn as SVG and given as image sources (data URIs).
const COLORS = ['#f2cf3a', '#3d7ab8', '#d35400'];
const PICTURES = COLORS.map((color) => `data:image/svg+xml,${encodeURIComponent(
	`<svg xmlns="http://www.w3.org/2000/svg" width="240" height="160"><rect width="240" height="160" fill="#e3f1d9"/><circle cx="120" cy="80" r="50" fill="${color}"/></svg>`,
)}`);

// The fake world to build with shows a new picture every 2 seconds.
const gallery = {
	name: 'Gallery',
	data: { picture: { every: 2000, value: (time) => PICTURES[Math.floor(time / 2000) % PICTURES.length] } },
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: gallery } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// A picture in `data` is an image source, ready to go into an <img> as it is.
	const picture = world.data.picture;
	return (
		<>
			<h2>{world.name}</h2>
			{picture ? <img src={picture} alt="The gallery's picture" width="240" height="160" /> : <p>No picture yet.</p>}
			<p>The picture changes every 2 seconds.</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// Three pictures, drawn as SVG and given as image sources (data URIs).
const COLORS = ['#f2cf3a', '#3d7ab8', '#d35400'];
const PICTURES = COLORS.map((color) => `data:image/svg+xml,${encodeURIComponent(
	`<svg xmlns="http://www.w3.org/2000/svg" width="240" height="160"><rect width="240" height="160" fill="#e3f1d9"/><circle cx="120" cy="80" r="50" fill="${color}"/></svg>`,
)}`);

// The fake world to build with shows a new picture every 2 seconds.
const gallery = {
	name: 'Gallery',
	data: { picture: { every: 2000, value: (time) => PICTURES[Math.floor(time / 2000) % PICTURES.length] } },
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: gallery } });

const title = document.createElement('h2');
const image = document.createElement('img');
image.alt = "The gallery's picture";
image.width = 240;
image.height = 160;
const empty = document.createElement('p');
empty.textContent = 'No picture yet.';
const caption = document.createElement('p');
caption.textContent = 'The picture changes every 2 seconds.';
document.body.append(title, image, empty, caption);

world.subscribe((node) => {
	title.textContent = node.name;
	// A picture in `data` is an image source, ready to go into an <img> as it is.
	const picture = node.data.picture;
	image.hidden = !picture;
	empty.hidden = Boolean(picture);
	// subscribe also runs for other changes. Touch the <img> only for a new picture.
	if (picture && image.src !== picture) image.src = picture;
});
Live, on a fake world
Open

How it works ​

  • world.data.picture is a string such as data:image/png;base64,… and is missing until the first picture comes.
  • The webapp sends a large picture to your view only when the picture changes and not at every change of the world.
  • The plain JavaScript view writes src only for a new picture because subscribe also runs for changes that do not touch the picture. React skips the update on its own when the value has not changed.
  • Give the <img> an alt that says what it shows. A width and a height keep the page from jumping when the picture arrives.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.