# Dancing fonts

> A per-letter wave, a rainbow cascade, a CRT glitch and a scrolling ticker: every character animated on its own.

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

Each character is a text of its own, so it can move and change colour independently. All four effects are started when the scene is built, with no `useUpdate`.

The wave staggers yoyo tweens; the rainbow runs one hue tween and sets every colour in `onUpdate`; the glitch chains drop, recover and wait tweens on each character's tint alpha; and the ticker scrolls with `loop: true`.

## 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: '#08070f',
});

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

`components/glitch.ts`:

```ts
import { getColor, useLoadBitmapFont, useTween } from 'nacatamalon';
import { linear, useRandom } from 'nacatamalon/math';
import * as Settings from '../settings';
import { makeChars, centred } from '../helpers/letters';

import type { TColor } from 'nacatamalon';

export const Glitch = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const tween = useTween();
    const random = useRandom();
    // ROW 3: GLITCH
    const GLITCH_TEXT = 'GAME OVER';
    const glitch = makeChars(font, GLITCH_TEXT, centred(GLITCH_TEXT, 24), 210, 24, () => getColor('#22eeff'));

    // How see-through a character is lives in the alpha of its own tint.
    // Drop, recover and wait, one after another; then again, with new numbers each time.
    const triggerGlitch = (tint: TColor, wait: number) => {
        const dropTo = 0.05 + random.rand() * 0.2;

        const glitchOnce = tween.chain(
            tween({
                from: 1,
                to: dropTo,
                ease: linear,
                duration: 0.04 + random.rand() * 0.08,
                delay: wait,
                onUpdate: (a) => {
                    tint.a = a;
                },
            }),
            tween({
                from: dropTo,
                to: 1,
                ease: linear,
                duration: 0.08 + random.rand() * 0.12,
                onUpdate: (a) => {
                    tint.a = a;
                },
            }),
            // A tween that changes nothing is a timer.
            tween({
                from: 0,
                to: 0,
                duration: 0.3 + random.rand() * 2.5,
                onUpdate: () => {}
            }),
        );

        glitchOnce.onEnd(() => triggerGlitch(tint, 0));
        glitchOnce.play();
    };

    glitch.forEach(({ tint }) => triggerGlitch(tint, random.rand() * 1.5));
};
```

`components/rainbow.ts`:

```ts
import { fromHsl, useLoadBitmapFont, useTween } from 'nacatamalon';
import { linear } from 'nacatamalon/math';
import * as Settings from '../settings';
import { makeChars, centred } from '../helpers/letters';

export const Rainbow = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const tween = useTween();
    // ROW 2: RAINBOW
    const RAIN_TEXT = 'READY';

    const rainbow = makeChars(font, RAIN_TEXT, centred(RAIN_TEXT, 40), 108, 40, () => ({
        r: 1,
        g: 1,
        b: 1,
        a: 1
    }));

    tween({
        from: 0,
        to: 360,
        duration: 4,
        ease: linear,
        loop: true,
        onUpdate: (phase) => {
            rainbow.forEach(({ tint }, i) => {
                const c = fromHsl((phase + i * 52) % 360, 1, 0.55);

                tint.r = c.r;
                tint.g = c.g;
                tint.b = c.b;
            });
        },
    }).play();
};
```

`components/ticker.ts`:

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

export const Ticker = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const tween = useTween();
    // TICKER: SCROLL
    const TICKER_TEXT = '  INSERT COIN  PRESS START  HIGH SCORE  NEW RECORD  PLAYER 2 READY  ';
    const tickerWidth = TICKER_TEXT.length * 18;

    const ticker = createText({
        text: TICKER_TEXT,
        font: font,
        style: {
            fontSize: 16
        },
        tint: 'rgba(255, 107, 53, 0.85)',
        transform: {
            x: Settings.WIDTH,
            y: 285,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    tween({
        from: Settings.WIDTH,
        to: -tickerWidth,
        duration: (Settings.WIDTH + tickerWidth) / 100,
        ease: linear,
        loop: true,
        onUpdate: (x) => {
            ticker.transform.x = x;
        },
    }).play();
};
```

`components/wave.ts`:

```ts
import { getColor, useLoadBitmapFont, useTween } from 'nacatamalon';
import { easeInOutSine } from 'nacatamalon/math';
import * as Settings from '../settings';
import { makeChars, centred } from '../helpers/letters';

export const Wave = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const tween = useTween();
    // ROW 1: WAVE
    const WAVE_TEXT = 'INSERT COIN';
    const WAVE_Y = 26;
    const wave = makeChars(font, WAVE_TEXT, centred(WAVE_TEXT, 24), WAVE_Y, 24, () => getColor('#f5c518'));

    wave.forEach(({ ct }, i) => {
        tween({
            from: WAVE_Y + 12,
            to: WAVE_Y - 12,
            duration: 0.45,
            ease: easeInOutSine,
            delay: i * 0.065,
            loop: true,
            yoyo: true,
            onUpdate: (y) => {
                ct.transform.y = y;
            },
        }).play();
    });
};
```

`helpers/letters.ts`:

```ts
import { createText } from 'nacatamalon';

import type { TBitmapFont, TColor } from 'nacatamalon';
import { WIDTH } from '../settings';

export const makeChars = (font: TBitmapFont, text: string, x: number, y: number, size: number, tintFor: (index: number) => TColor) => {
    const advance = 9 * (size / 8);

    return text.split('').map((char, i) => {
        // A colour of its own per character, so changing one does not change its neighbours.
        const tint = tintFor(i);

        const ct = createText({
            text: char,
            font,
            style: {
                fontSize: size
            },
            tint,
            transform: {
                x: x + i * advance,
                y,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            }
        });

        return {
            ct,
            // createText copies its input colour; animate the colour it actually owns.
            tint: ct.tint
        };
    });
};

export const centred = (text: string, size: number) => Math.round((WIDTH - text.length * 9 * (size / 8)) / 2);
```

`scenes/demo.ts`:

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

import { Wave } from '../components/wave';
import { Rainbow } from '../components/rainbow';
import { Glitch } from '../components/glitch';
import { Ticker } from '../components/ticker';

export const Scene = () => {
    useSpawn(Wave)();
    useSpawn(Rainbow)();
    useSpawn(Glitch)();
    useSpawn(Ticker)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
```
