Skip to content

NightAmbient

Install

Please install the @canvasengine/presets package first.

bash
npm install @canvasengine/presets

Then, you can use the presets in your project.

Overview

NightAmbient adds a night overlay with dynamic light spots.

  • If a Viewport is present in context, the filter is attached to it automatically.
  • Otherwise, it attaches to the parent container.
  • All props are reactive and support signal/function values.
  • haze is an ambient night veil, not a physical fog effect.
  • NightAmbiant remains available as a backward-compatible alias.

Basic Usage

html
<Canvas backgroundColor="#1a472a">
    <Viewport worldWidth={2048} worldHeight={2048} screen>
        <NightAmbient
            spots={lightSpots}
            darkness={{
                opacity: 0.8,
                color: "#0a1020",
            }}
            haze={{
                color: "#141a2a",
                radius: 0.5,
                softness: 0.35,
                opacity: 0.35,
            }}
        />
    </Viewport>
</Canvas>

<script>
    import { NightAmbient } from '@canvasengine/presets'
    import { signal } from 'canvasengine'

    const playerX = signal(400)
    const playerY = signal(300)

    const lightSpots = () => [
        {
            x: playerX,
            y: playerY,
            radius: 170,
            intensity: 1.0,
            flicker: true,
            flickerSpeed: 15,
        },
        { x: 280, y: 260, radius: 180, intensity: 0.95, flicker: true, flickerSpeed: 13 },
    ]
</script>

Without Viewport

html
<Canvas>
    <Container>
        <NightAmbient spots={spots} />
        <Sprite image="back.png" />
    </Container>
</Canvas>

Reactive CRUD Example

html
<script>
    import { signal } from 'canvasengine'

    const spots = signal([
        { x: 280, y: 260, radius: 180, intensity: 0.95, flicker: true, flickerSpeed: 13 },
    ])

    function addSpot() {
        spots.update((list) => [
            ...list,
            { x: 720, y: 540, radius: 130, intensity: 0.8, pulse: true, pulseSpeed: 1.5 },
        ])
    }

    function removeFirstSpot() {
        spots.update((list) => list.slice(1))
    }
</script>

Props

PropTypeRequiredDefaultDescription
lightSpotsArray<SpotInput> | Signal | () => ArrayNo[]Main reactive list of light spots
spotsSame as lightSpotsNo-Alias for compatibility
darknessnumber | { opacity, color } | Signal | () => valueNo{ opacity: 0.75, color: "#000000" }Darkness overlay outside light spots. A number is treated as opacity
darkColorColor | Signal<Color> | () => ColorNo#000000Legacy alias for darkness.color
haze{ color, radius, softness, opacity } | Signal | () => valueNosee belowHaze configuration

Darkness fields

FieldTypeDefaultDescription
opacitynumber | Signal<number> | () => number0.75Overlay opacity outside light spots (0 = no darkening, 1 = full color overlay)
colorColor | Signal<Color> | () => Color#000000Overlay color outside light spots

Haze fields

FieldTypeDefaultDescription
colorColor | Signal<Color> | () => Color#141424Haze color
radiusnumber | Signal<number> | () => number0.5Distance from light center where haze starts (0..1)
softnessnumber | Signal<number> | () => number0.35Width of the haze transition
opacitynumber | Signal<number> | () => number0.35Maximum haze overlay opacity outside light spots

SpotInput fields

FieldTypeRequiredDefaultDescription
xnumber | Signal<number> | () => numberYes-X position in world coordinates
ynumber | Signal<number> | () => numberYes-Y position in world coordinates
radiusnumberNo180Light radius in pixels
intensitynumberNo1.0Light intensity (0..2)
flickerbooleanNofalseEnable flickering effect (like a candle)
flickerSpeednumberNo12Speed of the flicker animation
pulsebooleanNofalseEnable pulsing effect (slow breathing)
pulseSpeednumberNo2Speed of the pulse animation
phasenumberNoautoPhase offset for animations (to desync multiple lights)

Color format

Color supports multiple formats:

  • Hex string: "#0a1020", "#abc"
  • Number: 0x0a1020
  • RGB tuple (0-255): [10, 16, 32]
  • Normalized RGB (0-1): [0.04, 0.06, 0.12]