# Nine-slice

> One frame picture at any size without deforming its corners: a stretched sprite beside createNineSlice stretching, tiling and fitting.

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

A UI frame at any size without deforming its corners: the panel, the button and the dialogue box of every game. The top row stretches it as a sprite and the corners smear.

The other three use `createNineSlice`: `slice: 8` cuts it into nine parts, the corners stay as drawn, and `mode` says how the edges and the middle fill their space (`'stretch'`, `'tile'` or `'tile-fit'`). Resizing is changing `width`; `scaleX` would stretch the corners too.

## Code

`index.ts`:

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

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

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

`components/panel_row.ts`:

```ts
import { createNineSlice, createSprite, createText, useUpdate } from 'nacatamalon';
import type { TTexture, TBitmapFont, TNineSliceMode } from 'nacatamalon';
import { RESIZE, SLICE } from '../settings';

type TPanelRowProps = {
    label: string;
    y: number;
    mode: 'sprite' | TNineSliceMode;
    texture: TTexture;
    font: TBitmapFont;
};

export const PanelRow = ({ label, y, mode, texture, font }: TPanelRowProps) => {
    const anchor = {
        x: 0,
        y: 0.5
    };

    createText({
        text: label,
        font,
        anchor,
        transform: {
            x: 16,
            y
        }
    });

    const options = {
        texture,
        width: 200,
        height: 48,
        anchor,
        transform: {
            x: 100,
            y
        }
    };

    const panel = mode === 'sprite'
        ? createSprite(options)
        : createNineSlice({
            ...options,
            slice: SLICE,
            mode
        });

    // All rows read the same scene clock, so their dimensions stay in step.
    // Change width/height rather than scale, to preserve the nine-slice corners.
    useUpdate((_delta, time) => {
        panel.width = Math.round(RESIZE.width + Math.sin(time * RESIZE.widthSpeed) * RESIZE.widthRange);
        panel.height = Math.round(RESIZE.height + Math.sin(time * RESIZE.heightSpeed) * RESIZE.heightRange);
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useLoadBitmapFont, useLoadTexture, useSpawn } from 'nacatamalon';
import { PanelRow } from '../components/panel_row';
import { ROWS } from '../settings';

export const Comparison = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/ui/panel.png'
    });

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

    const spawnRow = useSpawn(PanelRow);

    for (const row of ROWS) {
        spawnRow({
            ...row,
            texture,
            font
        });
    }

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const SLICE = 8;

export const RESIZE = {
    width: 240,
    widthRange: 120,
    widthSpeed: 1.5,
    height: 44,
    heightRange: 20,
    heightSpeed: 1.1
};

export const ROWS = [
    {
        label: 'SPRITE',
        y: 44,
        mode: 'sprite'
    },
    {
        label: 'STRETCH',
        y: 120,
        mode: 'stretch'
    },
    {
        label: 'TILE',
        y: 196,
        mode: 'tile'
    },
    {
        label: 'TILE FIT',
        y: 272,
        mode: 'tile-fit'
    }
] as const;
```
