# Tweens and easing

> useTween carries a value from one place to another through a curve, with delay, repeat and yoyo.

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

`useTween()` hands back a function that starts tweens. Each one moves the fields of its `target` named in `to`, from where they are, over `duration` seconds.

Six rows make the same trip through six curves, looping with `yoyo` and `loop: true`. They fan out because each curve is somewhere else at the same moment.

## Code

`index.ts`:

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

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

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

`components/easing_row.ts`:

```ts
import { createSprite, useLoadTexture, useTween } from 'nacatamalon';
import type { TEaseFn } from 'nacatamalon/math';
import { TEXTURE, TRIP } from '../settings';

export const EasingRow = ({ ease, index }: { ease: TEaseFn; index: number }) => {
    const texture = useLoadTexture(TEXTURE);
    const tween = useTween();

    const dot = createSprite({
        texture,
        width: TRIP.size,
        height: TRIP.size,
        transform: {
            x: TRIP.start,
            y: TRIP.rowY + index * TRIP.rowGap,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // The dot's x, from where it is to 416, and back for ever.
    tween({
        target: dot,
        to: {
            transform: {
                x: TRIP.end
            }
        },
        duration: TRIP.duration,
        ease,
        yoyo: true,
        loop: true,
    }).play();
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { EasingRow } from '../components/easing_row';
import { EASES } from '../settings';

export const Demo = () => {
    const row = useSpawn(EasingRow);

    EASES.forEach((ease, index) => {
        row({
            ease,
            index
        });
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { linear, easeInOutSine, easeInOutCubic, easeOutBack, easeOutElastic, easeOutBounce } from 'nacatamalon/math';

export const WIDTH = 480;
export const HEIGHT = 320;
export const EASES = [linear, easeInOutSine, easeInOutCubic, easeOutBack, easeOutElastic, easeOutBounce];
export const TRIP = {
    start: 56,
    end: 416,
    duration: 1.8,
    rowY: 34,
    rowGap: 46,
    size: 28
};
export const TEXTURE = {
    src: '/example-assets/assets/gammix.png',
    key: 'gammix'
};
```
