Mostra un'immagine a schermo intero
Per mostrare a grandezza piena una foto, una mappa o una scacchiera del mondo, aprila al clic in un livello sopra la tua vista e nello stesso handler del clic chiedi al browser lo schermo intero. La webapp permette lo schermo intero a ogni vista e sulla maggior parte dei browser l'immagine copre allora tutto lo schermo e anche la barra in alto della webapp. Dove il browser non lo supporta (su alcuni telefoni) il livello copre comunque tutta la tua vista.
jsx
import { useEffect, useRef, useState } from 'react';
import { useWorld } from './useWorld.js';
// The picture is a small drawing as a data URI, the way a world gives pictures in `data`.
const photo = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 90"><rect width="160" height="90" fill="#8ec5ea"/>'
+ '<circle cx="125" cy="25" r="12" fill="#f6d55c"/><path d="M0 90 50 35 85 75 115 50 160 90Z" fill="#3c8d5a"/></svg>',
)}`;
// The fake gallery holds the picture in `data`, with no account and no network.
const gallery = { name: 'Gallery', data: { photo } };
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: gallery } });
const [isOpen, setIsOpen] = useState(false);
const overlay = useRef(null);
// Leaving full screen another way (Esc, the back gesture of the phone) closes the overlay too.
useEffect(() => {
const onChange = () => {
if (!document.fullscreenElement) setIsOpen(false);
};
document.addEventListener('fullscreenchange', onChange);
return () => document.removeEventListener('fullscreenchange', onChange);
}, []);
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
function open() {
setIsOpen(true);
// The webapp allows full screen, and the view asks for it where the browser can (an iPhone cannot).
if (overlay.current.requestFullscreen) overlay.current.requestFullscreen().catch(() => {});
}
function close() {
setIsOpen(false);
if (document.fullscreenElement) document.exitFullscreen();
}
return (
<>
<img src={world.data.photo} alt="The photo" style={{ width: 200 }} />
<br />
<button onClick={open}>Full screen</button>
{/* The overlay shows the picture over the whole view, on black. */}
<div
ref={overlay}
style={{ position: 'fixed', inset: 0, display: isOpen ? 'block' : 'none', background: 'black', textAlign: 'center' }}
>
<img
src={world.data.photo}
alt="The photo, full screen"
style={{ width: '100%', height: 'calc(100% - 3em)', objectFit: 'contain' }}
/>
<button onClick={close}>Close</button>
</div>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The picture is a small drawing as a data URI, the way a world gives pictures in `data`.
const photo = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 90"><rect width="160" height="90" fill="#8ec5ea"/>'
+ '<circle cx="125" cy="25" r="12" fill="#f6d55c"/><path d="M0 90 50 35 85 75 115 50 160 90Z" fill="#3c8d5a"/></svg>',
)}`;
// The fake gallery holds the picture in `data`, with no account and no network.
const gallery = { name: 'Gallery', data: { photo } };
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: gallery } });
// The small picture, and the button that opens it.
const small = document.createElement('img');
small.alt = 'The photo';
small.style.width = '200px';
const open = document.createElement('button');
open.textContent = 'Full screen';
// The overlay shows the picture over the whole view, on black.
const overlay = document.createElement('div');
overlay.style.cssText = 'position: fixed; inset: 0; display: none; background: black; text-align: center';
const big = document.createElement('img');
big.alt = 'The photo, full screen';
big.style.cssText = 'width: 100%; height: calc(100% - 3em); object-fit: contain';
const close = document.createElement('button');
close.textContent = 'Close';
overlay.append(big, close);
document.body.append(small, document.createElement('br'), open, overlay);
open.onclick = () => {
overlay.style.display = 'block';
// The webapp allows full screen, and the view asks for it where the browser can (an iPhone cannot).
if (overlay.requestFullscreen) overlay.requestFullscreen().catch(() => {});
};
close.onclick = () => {
overlay.style.display = 'none';
if (document.fullscreenElement) document.exitFullscreen();
};
// Leaving full screen another way (Esc, the back gesture of the phone) closes the overlay too.
document.addEventListener('fullscreenchange', () => {
if (!document.fullscreenElement) overlay.style.display = 'none';
});
// Called at once, then after every change of the world.
world.subscribe((node) => {
small.src = node.data.photo;
big.src = node.data.photo;
});Come funziona
- Il livello è un
divconposition: fixed; inset: 0che copre la vista a qualsiasi dimensione. Al suo internoobject-fit: containmostra l'immagine intera con bande nere intorno. requestFullscreen()parte dentro l'handler del clic perché i browser permettono lo schermo intero solo subito dopo un'azione della persona. Ilcatchignora un rifiuto e lascia il livello aperto sopra la vista.- Close nasconde direttamente il livello e il listener di
fullscreenchangelo nasconde quando la persona esce dallo schermo intero in un altro modo (Esc o il gesto indietro del telefono). - L'immagine arriva da
datacome ogni altra immagine di un mondo (Mostra un'immagine dai dati).
Vedi anche
FullScreenPicture: un componente pronto con lo stesso comportamento.- La fotocamera e il resto del browser: che cosa può usare una vista senza chiedere.
- Mostra un'immagine dai dati