Skip to content

Show a picture full screen ​

To show a photo, a map or a board of the world at full size, open it in a layer over your view when the person clicks and request full screen from the browser in the same click handler. The webapp lets every view use full screen and on most browsers the picture then covers the whole screen including the top bar of the webapp. Where the browser does not support it (on some phones) the layer still covers your whole view.

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;
});
Live, on a fake world
Open

How it works ​

  • The layer is a div with position: fixed; inset: 0 that covers the view at any size. Inside it object-fit: contain shows the whole picture with black bars around it.
  • requestFullscreen() runs inside the click handler because browsers allow full screen only right after an action of the person. The catch ignores a rejection and leaves the layer open over the view.
  • Close hides the layer directly and the fullscreenchange listener hides it when the person leaves full screen in another way (Esc or the back gesture of the phone).
  • The picture comes from data like every other picture of a world (Show a picture from data).

See also ​

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