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;
});Come funziona
world.data.pictureè una stringa comedata: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
srcsolo per un'immagine nuova perché anche i cambiamenti che non toccano l'immagine chiamano il listener disubscribe. React salta da solo l'aggiornamento quando il valore non è cambiato. - Dai all'
<img>unaltche dica che cosa mostra. Unawidthe unaheightimpediscono alla pagina di saltare quando arriva l'immagine.
Vedi anche
- DataImage: un componente pronto che mostra un'immagine dai dati.
- Mostra un'immagine a schermo intero.
- Segui le immagini della fotocamera: immagini dieci volte al secondo.