# Full screen

> The game asks for full screen with a click or a key, and scales to whole multiples with bars in its own colour.

Example `fullscreen`, running at https://nacatamalon.com/en/examples/fullscreen. The whole catalogue: https://nacatamalon.com/llms/examples.md.

The game itself asks for full screen: **click** or press **F**. `enterFullscreen()`, on the game handle, puts it on the whole monitor scaled by `fullscreenScaling`: `'integer'` by default, whole multiples, every game pixel a square and bars round the edge in the background colour. **Esc** leaves, and so does `exitFullscreen()`. Leaving puts it back exactly as it was in the page.

Two rules come from the browser, not the engine. It is only granted in answer to the player, a click or a key: from a timer or at start-up it is refused, and from `usePointer().onDown` or a `justPressed` check, as here, it works. And some places have none: on an iPhone only a video may go full screen. There `enterFullscreen()` answers `false` instead of throwing, and the game says so and carries on.

`isFullscreen()` is read every frame rather than remembered, because Esc leaves without asking the game. Every example also has a full screen button in the playground bar, which calls the same thing.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Room } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#14101f',
    fullscreenScaling: 'integer',
});

// Declare Scenes
game({
    Room,
});
```

`components/screen.ts`:

```ts
import { createSprite, createText, useGame, useKeyboard, useLoadBitmapFont, useLoadTexture, usePointer, useUpdate } from 'nacatamalon';

export const Screen = () => {
    const game = useGame();
    const keys = useKeyboard();

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    const status = createText({
        text: '',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#ffb347',
        anchor: {
            x: 0.5,
            y: 0,
        },
        transform: {
            x: 160,
            y: 16,
        },
    });

    createText({
        text: 'CLICK OR F',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#8c82a6',
        anchor: {
            x: 0.5,
            y: 0,
        },
        transform: {
            x: 160,
            y: 200,
        },
    });

    // Something small in pixel art, so it is plain that full screen keeps the pixels square.
    const gammix = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix',
    });

    const hero = createSprite({
        texture: gammix,
        width: 32,
        height: 32,
        transform: {
            x: 160,
            y: 112,
        },
    });

    let refused = false;

    const toggle = async () => {
        if (game.isFullscreen()) {
            await game.exitFullscreen();

            return;
        }

        refused = !(await game.enterFullscreen());
    };

    usePointer().onDown(() => {
        void toggle();
    });

    useUpdate((_, time) => {
        if (keys.justPressed('f')) {
            void toggle();
        }

        hero.transform.y = 112 + Math.sin(time * 3) * 6;

        // Read every frame rather than remembered: Esc leaves without asking the game, and this
        // still says the truth the frame after.
        status.text = game.isFullscreen() ? 'FULL SCREEN  ESC TO LEAVE' : refused ? 'NOT AVAILABLE HERE' : 'IN THE PAGE';
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';

import { Screen } from '../components/screen';

export const Room = () => {
    useSpawn(Screen)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 224;
```
