# Font gallery

> Two free bitmap fonts at different sizes and tints, and a scrolling ticker.

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

Two bitmap fonts, each with a `fontSize` that is a whole multiple of its own height (both are 8 px), which is what keeps every pixel square. Nacatamal Arcade is the engine's own font as files, with lowercase, accents and the Spanish ñ, ¿ and ¡; the other is the tutorial's own HUD font. Asking for the same font twice gives back the one already loaded.

The ticker at the bottom is a text whose `transform.x` moves every frame.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';

import { Scene } from './scenes/demo';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0a0a18',
});

// Declare Scenes
game({
    Scene
});
```

`components/samples.ts`:

```ts
import { createText, getColor, useLoadBitmapFont } from 'nacatamalon';
import * as Settings from '../settings';

export const Samples = () => {
    let y = 12;

    for (const { json, atlas, size, tint, label } of Settings.FONTS) {
        const font = useLoadBitmapFont({
            json,
            atlas
        });

        createText({
            text: label,
            font,
            style: {
                fontSize: size
            },
            tint: getColor(tint),
            transform: {
                x: 10,
                y,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            }
        });
        y += size + 10;
    }
};
```

`components/ticker.ts`:

```ts
import { createText, useLoadBitmapFont, useUpdate, useWatch, measureText } from 'nacatamalon';
import * as Settings from '../settings';

export const Ticker = () => {
    // Asking for the font again gives back the one already loaded above.
    const tickerFont = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    const ticker = createText({
        text: '  READY PLAYER ONE  GAME OVER  HIGH SCORE  INSERT COIN  ',
        font: tickerFont,
        style: {
            fontSize: 16
        },
        tint: 'rgba(255, 255, 255, 0.5)',
        transform: {
            x: Settings.WIDTH,
            y: 296,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    let width = 0;

    useWatch(() => {
        if (tickerFont.status !== 'ready') {
            return;
        }

        width = measureText(ticker).width;
    }, [tickerFont]);
    useUpdate((delta) => {
        ticker.transform.x -= Settings.TICKER_SPEED * delta;

        if (ticker.transform.x < -(width)) {
            ticker.transform.x = Settings.WIDTH;
        }
    });
};
```

`scenes/demo.ts`:

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

import { Samples } from '../components/samples';
import { Ticker } from '../components/ticker';

export const Scene = () => {
    useSpawn(Samples)();
    useSpawn(Ticker)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
export const FONTS = [
    {
        ...ARCADE,
        size: 32,
        tint: '#4cff91',
        label: 'NACATAMAL'
    },
    {
        ...ARCADE,
        size: 48,
        tint: '#ff6b35',
        label: 'ARCADE'
    },
    {
        ...ARCADE,
        size: 24,
        tint: '#c084fc',
        label: '¡HOLA, NIÑO!'
    },
    {
        ...ARCADE,
        size: 24,
        tint: '#60a5fa',
        label: 'Áé Íó Úü ¿?'
    },
    {
        ...ARCADE,
        size: 16,
        tint: '#f5c518',
        label: 'abc 0123456789'
    },
];

export const TICKER_SPEED = 80;
```
