deck.gl TextLayer Example

Demo

Source

<html>
  <head>
    <title>deck.gl TextLayer Example</title>

    <script src="https://unpkg.com/deck.gl@^9.0.0/dist.min.js"></script>
    <script src='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.js'></script>

    <link href='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.css' rel='stylesheet' />
    <style type="text/css">
      body {
        width: 100vw;
        height: 100vh;
        margin: 0;
        overflow: hidden;
      }
      .deck-tooltip {
        font-family: Helvetica, Arial, sans-serif;
        padding: 6px !important;
        margin: 8px;
        max-width: 300px;
        font-size: 10px;
      }
      #control-panel {
        position: absolute;
        top: 0;
        left: 0;
        margin: 12px;
        padding: 10px;
        font-size: 11px;
        line-height: 1.5;
        z-index: 1;
        font-family: Helvetica, Arial, sans-serif;
        background: #fff;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.15);
      }
      label {
        display: inline-block;
        width: 50px;
      }
    </style>
  </head>

  <body>
      <div id="tooltip"></div>
      <div id="control-panel">
        <div>
          <label>Font Size</label>
          <input id="getSize" type="range" min="0" max="100" step="1" value="16"></input>
          <span id="getSize-value"></span>
        </div>
        <div>
          <label>Opacity</label>
          <input id="opacity" type="range" min="0" max="1" step="0.1" value="1"></input>
          <span id="opacity-value"></span>
        </div>
      </div>
  </body>

  <script type="text/javascript">

    const {DeckGL,TextLayer} = deck;

    const deckgl = new DeckGL({
      mapStyle: 'https://basemaps.cartocdn.com/gl/dark-matter-nolabels-gl-style/style.json',
      initialViewState: {
        longitude: -122.4,
        latitude: 37.74,
        zoom: 11,
        maxZoom: 15,
        pitch: 30,
        bearing: 0
      },
      controller: true,
      getTooltip: ({object}) => object && `${object.name}\n${object.address}`
    });
    
    const OPTIONS = ['getSize', 'opacity'];
    OPTIONS.forEach(key => {
      document.getElementById(key).oninput = renderLayer;
    });

    renderLayer();

    function renderLayer() {
      const options = {};
      OPTIONS.forEach(key => {
        const value = document.getElementById(key).value;
        document.getElementById(key + '-value').innerHTML = value;
        options[key] = Number(value);
      });

      const textLayer = new TextLayer({
        id: 'text-layer',
        data: 'https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/bart-stations.json',
        pickable: true,
        getPosition: d => d.coordinates,
        getText: d => d.name,
        getSize: 16,
        getColor: [247,248,243],
        getTextAnchor: 'middle',
        getAlignmentBaseline: 'center',
        ...options
      });
      
      deckgl.setProps({ layers: [textLayer] });
    }
    
  </script>
</html>