Skip to content

NightAmbiant

Install

Please install the @canvasengine/presets package first.

bash
npm install @canvasengine/presets

Then, you can use the presets in your project.

Overview

NightAmbiant 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.

Basic Usage

html
<Canvas backgroundColor="#1a472a">
    <Viewport worldWidth={2048} worldHeight={2048} screen>
        <NightAmbiant
            lightSpots={lightSpots}
            darkness={0.8}
            darkColor="#0a1020"
            fogColor="#141a2a"
        />
    </Viewport>
</Canvas>

<script>
    import { NightAmbiant } 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>
        <NightAmbiant lightSpots={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 | Signal<number> | () => numberNo0.75Darkness intensity outside light spots (0 = no darkening, 1 = full black)
darkColorColor | Signal<Color> | () => ColorNo#000000Tint color applied in dark zones (colored night effect)
fogColorColor | Signal<Color> | () => ColorNo#141424Fog color that fades in around light spots
fogRadiusnumber | Signal<number> | () => numberNo0.5Distance from light center where fog starts (0..1)
fogSoftnessnumber | Signal<number> | () => numberNo0.35Width of the fog transition (higher = softer edge)

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]