# Vector fonts

> useLoadFont reads a .ttf or .woff, and the text stays sharp at any size, scale, turn or camera zoom.

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

`useLoadFont` reads a plain `.ttf` or `.woff`, with its kerning, accents and ñ, and keeps each letter as a distance field the first time it is shown. So the same font is sharp at any `fontSize`, scaled, turned, and through a camera zooming from 1x to 3x.

On a vector font `fontSize` is the size of the font as in CSS: one em, 16 when left out. A pixel font drawn as outlines keeps its square corners, and a material on a vector text works as on a sprite.

Fonts: Bangers and Press Start 2P, under the SIL Open Font License.

## Code

`index.ts`:

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

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

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

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

`components/samples.ts`:

```ts
import { createMaterial, createText, useLoadFont, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';
import { SHIMMER_WGSL, SHIMMER_GLSL } from '../shaders';

export const Samples = () => {
    const centred = (x: number, y: number) => ({
        x,
        y
    });

    // Two ordinary .ttf files: no picture of the letters, no list of characters.
    const comic = useLoadFont({
        src: '/example-assets/fonts/vector/Bangers-Regular.ttf'
    });

    // A pixel font drawn as outlines: its square corners stay square at any size.
    const arcade = useLoadFont({
        src: '/example-assets/fonts/vector/PressStart2P-Regular.ttf',
        chars: 'PRESS START 0123456789'
    });

    const title = createText({
        text: '¡Hola, ñandú!',
        font: comic,
        style: {
            fontSize: 48
        },
        tint: Settings.YELLOW,
        anchor: Settings.MIDDLE,
        transform: centred(Settings.WIDTH / 2, 56),
    });

    // The same font at four sizes, every one as sharp as the others.
    [10, 16, 24, 36].forEach((fontSize, i) => {
        createText({
            text: 'AVATAR Wolf',
            font: comic,
            style: {
                fontSize
            },
            tint: Settings.WHITE,
            transform: centred(24, 104 + [0, 16, 38, 68][i]),
        });
    });

    createText({
        text: 'PRESS START',
        font: arcade,
        style: {
            fontSize: 8
        },
        tint: Settings.PINK,
        transform: centred(270, 112)
    });
    createText({
        text: 'PRESS START',
        font: arcade,
        style: {
            fontSize: 16
        },
        tint: Settings.PINK,
        transform: centred(270, 130)
    });
    createText({
        text: 'SCORE',
        font: arcade,
        style: {
            fontSize: 16
        },
        tint: Settings.PINK,
        transform: centred(270, 164)
    });

    const score = createText({
        text: '0000',
        font: arcade,
        style: {
            fontSize: 24
        },
        tint: Settings.WHITE,
        transform: centred(270, 186)
    });

    createText({
        text: 'shimmer',
        font: comic,
        style: {
            fontSize: 40
        },
        tint: '#7ad7ff',
        material: createMaterial({
            name: 'shimmer',
            fragment: SHIMMER_WGSL,
            fragmentGlsl: SHIMMER_GLSL
        }),
        anchor: Settings.MIDDLE,
        transform: centred(Settings.WIDTH / 2, 262),
    });

    let points = 0;

    useUpdate((delta, time) => {
        // The title breathes and sways: scaled and turned, it is worked out again at its new size.
        const breath = 1 + 0.12 * Math.sin(time * 2);

        title.transform.scaleX = breath;
        title.transform.scaleY = breath;
        title.transform.rotation = 0.08 * Math.sin(time * 1.3);

        points += Math.round(delta * 600);
        score.text = String(points % 10000).padStart(4, '0');
    });
};
```

`scenes/demo.ts`:

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

import * as Settings from '../settings';
import { Samples } from '../components/samples';

export const Showcase = () => {
    const camera = useCamera2d();

    useSpawn(Samples)();
    useUpdate((_delta, time) => {
        // After a few seconds as it is, the camera zooms in to 3x around the middle and back out,
        // pausing at 1x between swings.
        const zoom = 1 + 2 * Math.max(0, Math.sin((time - 3) * 0.5));

        camera.zoom = zoom;
        camera.transform.x = Settings.WIDTH / 2 - Settings.WIDTH / (2 * zoom);
        camera.transform.y = Settings.HEIGHT / 2 - Settings.HEIGHT / (2 * zoom);
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 300;
import { getColor } from 'nacatamalon';

export const WHITE = getColor('#ffffff');
export const YELLOW = getColor('#f5c518');
export const PINK = getColor('#ff5d8f');
export const MIDDLE = {
    x: 0.5,
    y: 0.5
};
```

`shaders.ts`:

```ts
export const SHIMMER_WGSL = `
fn effect(color: vec4f, uv: vec2f) -> vec4f {
    let wave = 0.5 + 0.5 * sin(uv.x * 40.0 - mu.time * 4.0);
    return vec4f(mix(color.rgb, vec3f(1.0, 0.9, 0.4), wave * 0.8), color.a);
}
`;

export const SHIMMER_GLSL = `
vec4 effect(vec4 color, vec2 uv) {
    float wave = 0.5 + 0.5 * sin(uv.x * 40.0 - mu.time * 4.0);
    return vec4(mix(color.rgb, vec3(1.0, 0.9, 0.4), wave * 0.8), color.a);
}
`;
```
