# Glowing sprites

> blend: 'additive' adds a sprite’s colour to what is behind it: lamp halos, a saucer’s beam and fireflies, compared live with ordinary blending.

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

`createSprite({ blend: 'additive' })` adds the sprite's colour to what is behind it instead of covering it, so it can only brighten: the halos round the lamps, the saucer's beam, the fireflies.

Click or press **Space** to switch to ordinary `'alpha'`. Over the black walls the two look alike, but under `'alpha'` the windows inside each halo and the sky behind the beam go dull, and where two halos cross it is no brighter than one.

`blend` can change any frame, and works the same on WebGPU and WebGL2, with a material of its own too.

## Code

`index.ts`:

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

import * as Settings from './settings';
import { Night } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#000000'
});

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

`art/paint.ts`:

```ts
import { getColor } from 'nacatamalon';
import { createPixels, fillGradient, fillRect, mapPixels } from 'nacatamalon/pixels';

const W = 480;
const H = 300;

// A dusk sky and a row of buildings with a few windows lit.
export const paintStreet = () => {
    const street = createPixels(W, H);

    fillGradient(street, {
        colors: ['#1b1440', '#5a2a6a', '#c0566a'],
        bands: 8,
        dither: true
    });

    const wall = getColor('#140f22');
    const window = getColor('#ffd27a');

    [[0, 150, 90], [90, 120, 70], [160, 170, 110], [270, 135, 80], [350, 160, 130]].forEach(([x, top, width]) => {
        fillRect(street, x, top, width, H - top, wall);

        for (let wy = top + 10;wy < H - 30;wy += 18) {
            for (let wx = x + 8;wx < x + width - 8;wx += 16) {
                if ((wx * 7 + wy * 3) % 5 === 0) {
                    fillRect(street, wx, wy, 6, 8, window);
                }
            }
        }
    });

    return street;
};

// A round glow, strongest in the middle and gone at the edge.
export const paintHalo = () => mapPixels(createPixels(64, 64), (_, x, y) => {
    const d = Math.hypot(x - 31.5, y - 31.5) / 32;
    const a = Math.max(0, 1 - d);

    return {
        r: 1,
        g: 0.8,
        b: 0.45,
        a: a * a
    };
});

// A cone of light, wider at the bottom and fading as it goes down.
export const paintBeam = () => mapPixels(createPixels(80, 160), (_, x, y) => {
    const half = 8 + y * 0.22;
    const across = Math.abs(x - 39.5) / half;
    const a = across >= 1 ? 0 : (1 - across) * (1 - y / 160) * 0.8;

    return {
        r: 0.5,
        g: 1,
        b: 0.7,
        a
    };
});
```

`components/glows.ts`:

```ts
import { createSprite, createText, getColor, useKeyboard, usePointer, useUpdate } from 'nacatamalon';
import { createPixelTexture } from 'nacatamalon/pixels';
import type { TSprite } from 'nacatamalon';
import * as Settings from '../settings';
import { paintBeam, paintHalo } from '../art/paint';

export const Glows = () => {
    const glows: TSprite[] = [];
    const halo = createPixelTexture(paintHalo());
    const lamp = getColor('#ffe9b0');

    // Street lamps in front of the lit windows: a post, a bulb, and a halo wide enough to reach the
    // next one. Where two halos cross, adding makes it brighter; covering does not.
    [60, 150, 240, 330, 420].forEach((x) => {
        createSprite({
            width: 3,
            height: 90,
            tint: '#0b0814',
            transform: {
                x,
                y: Settings.HEIGHT - 45
            }
        });
        createSprite({
            width: 6,
            height: 4,
            tint: lamp,
            transform: {
                x,
                y: Settings.HEIGHT - 90
            }
        });
        glows.push(createSprite({
            texture: halo,
            width: 150,
            height: 150,
            transform: {
                x,
                y: Settings.HEIGHT - 90
            },
            blend: 'additive'
        }));
    });

    // A saucer with its beam.
    const beam = createSprite({
        texture: createPixelTexture(paintBeam()),
        width: 80,
        height: 160,
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: 240,
            y: 70
        },
        blend: 'additive'
    });

    const saucer = createSprite({
        width: 44,
        height: 10,
        tint: '#9aa4b8',
        transform: {
            x: 240,
            y: 66
        }
    });

    glows.push(beam);

    // Fireflies: tiny halos drifting about, brighter where two cross.
    const flies = Array.from({
        length: 14
    }, (_, i) => ({
        sprite: createSprite({
            texture: halo,
            width: 16,
            height: 16,
            tint: '#b8ff6a',
            blend: 'additive'
        }),
        phase: i * 1.7,
    }));

    flies.forEach((fly) => glows.push(fly.sprite));

    const label = createText({
        text: '',
        transform: {
            x: 8,
            y: 8
        }
    });

    createText({
        text: 'CLICK OR SPACE: SWITCH',
        tint: '#ffd0f0',
        transform: {
            x: 8,
            y: 20
        }
    });

    let additive = true;

    const show = () => {
        for (const glow of glows) {
            glow.blend = additive ? 'additive' : 'alpha';
        }

        label.text = additive ? "BLEND 'ADDITIVE'" : "BLEND 'ALPHA'";
    };

    show();

    const keys = useKeyboard();

    usePointer().onDown(() => {
        additive = !additive;
        show();
    });

    useUpdate((_delta, time) => {
        if (keys.justPressed(' ')) {
            additive = !additive;
            show();
        }

        const x = 240 + Math.sin(time * 0.7) * 120;

        saucer.transform.x = x;
        beam.transform.x = x;
        beam.tint = {
            r: 1,
            g: 1,
            b: 1,
            a: 0.75 + Math.sin(time * 9) * 0.25
        };
        flies.forEach((fly, i) => {
            fly.sprite.transform.x = 240 + Math.sin(time * 0.5 + fly.phase) * 220;
            fly.sprite.transform.y = 200 + Math.cos(time * 0.8 + fly.phase * 1.3) * 40 + (i % 3) * 10;
        });
    });
};
```

`scenes/demo.ts`:

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

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

import { paintStreet } from '../art/paint';
import { Glows } from '../components/glows';

export const Night = () => {
    createSprite({
        texture: createPixelTexture(paintStreet()),
        width: Settings.WIDTH,
        height: Settings.HEIGHT,
        anchor: {
            x: 0,
            y: 0
        }
    });

    useSpawn(Glows)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 300;
```
