# Pixel signs

> drawText writes into a picture painted in code: a motel sign in the engine’s own font, another in a .ttf, and sheep made of fillEllipse.

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

`drawText(pixels, text, x, y)` writes into a picture painted in code, laid out exactly as `createText`. Once it is in the pixels it is part of the art: the motel sign swings, and its letters swing with it.

**With no font** it uses the one the engine carries, which needs no loading, so the motel sign is painted straight in the scene body. The VACANCY light blinks: only its strip is painted again, and only that strip is sent with `updatePixelTexture`.

**A `.ttf` has to arrive first.** The yellow sign is painted inside a `useWatch` on its fonts, which runs at the start (when there is nothing to write with yet) and again when they land. `smooth: false` draws the pixel font with hard edges, every pixel in or out.

Each sheep is two `fillEllipse` calls: a circle stretched to its own width and height, with the same stepped edge as `fillCircle`.

## Code

`index.ts`:

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

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

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

`art/paint.ts`:

```ts
import { getColor } from 'nacatamalon';
import { createPixels, fillEllipse, fillRect } from 'nacatamalon/pixels';
import type { TColor } from 'nacatamalon';

// A board with a border: every sign here starts as one.
export const board = (width: number, height: number, fill: TColor, edge: TColor) => {
    const pixels = createPixels(width, height, edge);

    fillRect(pixels, 2, 2, width - 4, height - 4, fill);

    return pixels;
};

// A sheep out of two ellipses and two legs. fillEllipse is a circle stretched to its own width and
// height, with the same stepped edge as fillCircle.
export const paintSheep = () => {
    const wool = getColor('#f4f1e8');
    const face = getColor('#2b2b33');
    const sheep = createPixels(28, 20);

    fillRect(sheep, 7, 13, 2, 6, face);
    fillRect(sheep, 18, 13, 2, 6, face);
    fillEllipse(sheep, 13, 9, 10, 6, wool);
    fillEllipse(sheep, 23, 7, 4, 3, face);

    return sheep;
};
```

`components/drivesign.ts`:

```ts
import { createSprite, getColor, useLoadFont, useWatch } from 'nacatamalon';
import { createPixelTexture, drawText, updatePixelTexture } from 'nacatamalon/pixels';
import * as Settings from '../settings';
import { board } from '../art/paint';

export const DriveSign = () => {
    // 2. A .ttf, which arrives a moment after the scene starts. useWatch runs now, when drawText has
    // nothing to draw with yet, and again when the font lands; updatePixelTexture sends it on.
    const comic = useLoadFont({
        src: '/example-assets/fonts/vector/Bangers-Regular.ttf'
    });

    const arcade = useLoadFont({
        src: '/example-assets/fonts/vector/PressStart2P-Regular.ttf'
    });

    const drive = board(150, 80, getColor('#ffcf5a'), getColor('#5b3a24'));
    const driveTexture = createPixelTexture(drive);

    createSprite({
        texture: driveTexture,
        width: 150,
        height: 80,
        transform: {
            x: 370,
            y: 90
        }
    });
    useWatch(() => {
        drawText(drive, 'SUNSET\nDRIVE', 75, 34, {
            font: comic,
            fontSize: 26,
            color: '#7a2e10',
            align: 'center',
            anchor: Settings.MIDDLE
        });
        // A pixel font drawn from its outlines with smooth off: every pixel in or out.
        drawText(drive, 'OPEN 24H', 75, 68, {
            font: arcade,
            fontSize: 8,
            color: '#7a2e10',
            anchor: Settings.MIDDLE,
            smooth: false
        });
        updatePixelTexture(driveTexture);
    }, [comic, arcade]);
};
```

`components/flock.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';
import { createPixelTexture } from 'nacatamalon/pixels';
import * as Settings from '../settings';
import { paintSheep } from '../art/paint';

export const Flock = () => {
    // 3. A flock painted with fillEllipse, wandering about.
    const sheepTexture = createPixelTexture(paintSheep());

    const flock = [60, 220, 390].map((x, i) => ({
        sprite: createSprite({
            texture: sheepTexture,
            width: 56,
            height: 40,
            transform: {
                x,
                y: Settings.HEIGHT - 40
            }
        }),
        speed: 18 + i * 9,
    }));

    useUpdate((delta) => {
        for (const sheep of flock) {
            sheep.sprite.transform.x += sheep.speed * delta;

            if (sheep.sprite.transform.x > Settings.WIDTH + 30) {
                sheep.sprite.transform.x = -30;
            }
        }
    });
};
```

`components/motel.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';
import { createPixelTexture, drawText, fillRect, updatePixelTexture } from 'nacatamalon/pixels';
import * as Settings from '../settings';
import { board } from '../art/paint';

export const Motel = () => {
    // 1. The font the engine carries: nothing to load, so it can be drawn right here in the body.
    const motel = board(120, 52, Settings.PLUM, Settings.PINK);

    drawText(motel, 'MOTEL', 60, 18, {
        fontSize: 16,
        color: Settings.PINK,
        anchor: Settings.MIDDLE
    });

    const motelTexture = createPixelTexture(motel);

    const motelSign = createSprite({
        texture: motelTexture,
        width: 240,
        height: 104,
        transform: {
            x: 140,
            y: 100
        }
    });

    // The VACANCY light blinks: only its strip is painted again and only that strip is sent.
    let lit = true;
    let clock = 0;

    const blink = () => {
        fillRect(motel, 4, 32, 112, 14, Settings.PLUM);
        drawText(motel, 'VACANCY', 60, 39, {
            color: lit ? Settings.CYAN : Settings.UNLIT,
            anchor: Settings.MIDDLE
        });
        updatePixelTexture(motelTexture, {
            x: 4,
            y: 32,
            width: 112,
            height: 14
        });
    };

    blink();

    useUpdate((delta, time) => {
        clock += delta;

        if (clock > 0.6) {
            clock = 0;
            lit = !lit;
            blink();
        }

        motelSign.transform.rotation = Math.sin(time * 1.3) * 0.06;
    });
};
```

`scenes/demo.ts`:

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

import * as Settings from '../settings';

import { Motel } from '../components/motel';
import { DriveSign } from '../components/drivesign';
import { Flock } from '../components/flock';

export const Street = () => {
    createSprite({
        width: Settings.WIDTH,
        height: 70,
        tint: '#203a22',
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 0,
            y: Settings.HEIGHT - 70
        }
    });
    useSpawn(Motel)();
    useSpawn(DriveSign)();
    useSpawn(Flock)();

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 480;
export const HEIGHT = 300;
export const MIDDLE = {
    x: 0.5,
    y: 0.5
};

export const PINK = getColor('#ff4fd8');
export const CYAN = getColor('#4fe3ff');
export const UNLIT = getColor('#3a2448');
export const PLUM = getColor('#2a1a3a');
```
