Skip to content

Mostra un'immagine dai dati ​

Un'immagine in data (la foto di una fotocamera, una mappa, un disegno) arriva come un data URI pronto che va così com'è nel src di un <img>. Finché non arriva la prima immagine la vista mostra una breve nota. La galleria dell'esempio mostra un'immagine nuova ogni due secondi.

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

Come funziona ​

  • world.data.picture è una stringa come data:image/png;base64,… e manca finché non arriva la prima immagine.
  • La webapp invia alla tua vista un'immagine grande solo quando l'immagine cambia e non a ogni cambiamento del mondo.
  • La vista in JavaScript semplice scrive src solo per un'immagine nuova perché anche i cambiamenti che non toccano l'immagine chiamano il listener di subscribe. React salta da solo l'aggiornamento quando il valore non è cambiato.
  • Dai all'<img> un alt che dica che cosa mostra. Una width e una height impediscono alla pagina di saltare quando arriva l'immagine.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.