Back to all examples

Traffic flow with bloom

This example on GitHub

Light up live traffic over a mono dark map with the map-effects plugin's bloom.

Traffic flow with bloom
import { TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { setKnob, TomTomMap, TrafficFlowModule, TrafficIncidentsModule } from '@tomtom-org/maps-sdk/map';
import { geocodeOne } from '@tomtom-org/maps-sdk/services';
import { effectKnobCatalogue, MapEffects } from '@tomtom-org/maps-sdk-plugin-map-effects';
import './style.css';
import { BLOOM_LOOK, FLOW_WIDTH_FACTOR } from './bloomLook';
import { API_KEY } from './config';
import { initScopeSelector } from './scopeSelector';

// (Set your own API key when working in your own environment)
TomTomConfig.instance.put({ apiKey: API_KEY, language: 'en-GB' });

(async () => {
    const manhattan = await geocodeOne('Manhattan, New York');
    const [longitude, latitude] = manhattan.geometry.coordinates;

    const map = new TomTomMap({
        style: 'monoDark',
        mapLibre: {
            container: 'sdk-map',
            center: [longitude, latitude],
            zoom: 13,
            // Bloom reads the map canvas back; MapLibre keeps it readable only when asked.
            canvasContextAttributes: { preserveDrawingBuffer: true },
        },
    });

    // Flow tubes and incident jams: between them, everything this example lights. Wider tubes give
    // the glow something to spread.
    await TrafficFlowModule.get(map, { visible: true, widthFactor: FLOW_WIDTH_FACTOR });
    await TrafficIncidentsModule.get(map, { visible: true });

    const effects = new MapEffects(map, BLOOM_LOOK);

    // The same glow, pointed at less of the map.
    initScopeSelector(BLOOM_LOOK.bloom?.only ?? [], (scope) =>
        setKnob(effects, effectKnobCatalogue, 'bloom.only', scope),
    );
})();

Manhattan at night stays grey while its live traffic glows, as far as the panel lets it.

  • TrafficFlowModule.get and TrafficIncidentsModule.get draw live traffic on the roads the style already ships, with no source or layer wiring.
  • The flow module’s widthFactor widens the flow tubes, which gives the glow something to spread. A light can only bleed what the map drew.
  • bloom.only keeps that light on the colours the loaded style paints traffic in, so the white labels stay crisp however hard the glow burns.

Related examples