Atlaslog
Atlaslog / Examples / deck.gl / Trip Trail
deck.gl · Runnable Example

Trip Trail

Runnable deck.gl example: Trip Trail. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All deck.gl examples
/demos/deckgl-trip-trail.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/deckgl-trip-trail.html
<!--
  示例:拖尾长度
  要点:两个 TripsLayer 共用时钟,trailLength 分别 30 秒与 120 秒
  依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>拖尾长度</title>
<style>
  html,body{height:100%;margin:0;background:#0d1117}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
  .bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600}
  .bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
  .bar input[type=checkbox]{accent-color:#4f9ef8}
  .bar input[type=range]{width:150px;accent-color:#4f9ef8}
  .bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
  .bar button:hover{background:#26303d}
  #cv{flex:1 1 auto;min-height:0;width:100%;display:block}

</style>
</head>
<body>
<div class="bar"><span>短尾迹 30 s · 长尾迹 120 s</span><span>当前时刻 <b id="t">—</b></span></div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
  'use strict';

  if (typeof deck === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }
  var trips = [];
  for (var r = 0; r < 4; r++) {
    var path = [], ts = [];
    var lng = 116.26 + Math.random() * 0.26;
    var lat = 39.83 + Math.random() * 0.12;
    var head = Math.random() * Math.PI * 2;
    for (var s = 0; s < 120; s++) {
      head += (Math.random() - 0.5) * 0.35;
      lng += Math.cos(head) * 0.0028;
      lat += Math.sin(head) * 0.0019;
      path.push([lng, lat]);
      ts.push(s * 2);
    }
    trips.push({ path: path, timestamps: ts });
  }

  var current = 0;
  var total = 240;

  function layers() {
    return [
      new deck.TripsLayer({
        id: 'short',
        data: trips,
        getPath: function (d) { return d.path; },
        getTimestamps: function (d) { return d.timestamps; },
        getColor: [92, 186, 255],
        currentTime: current,
        trailLength: 30,
        fadeTrail: true,
        widthMinPixels: 2,
        capRounded: true
      }),
      new deck.TripsLayer({
        id: 'long',
        data: trips,
        getPath: function (d) { return d.path; },
        getTimestamps: function (d) { return d.timestamps; },
        getColor: [255, 176, 96],
        currentTime: current,
        trailLength: 120,
        fadeTrail: true,
        widthMinPixels: 1,
        opacity: 0.55,
        capRounded: true
      })
    ];
  }

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: { longitude: 116.39, latitude: 39.89, zoom: 10.8, pitch: 0, bearing: 0 },
    controller: true,
    layers: layers()
  });

  var last = performance.now();
  function tick(now) {
    current = (current + (now - last) / 1000 * 5) % total;
    last = now;
    document.getElementById('t').textContent = current.toFixed(0) + ' s';
    deckgl.setProps({ layers: layers() });
    requestAnimationFrame(tick);
  }
  requestAnimationFrame(tick);
  window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More deck.gl examples

deck.gl · EXAMPLEView Controller deck.gl · EXAMPLEView Sync deck.gl · EXAMPLEView Transition