Atlaslog
Atlaslog / Examples / deck.gl / Animation Frame
deck.gl · Runnable Example

Animation Frame

Runnable deck.gl example: Animation Frame. 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-animation-frame.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/deckgl-animation-frame.html
<!--
  示例:逐帧动画
  核心逻辑:requestAnimationFrame 里只改 radiusScale 与透明度,不重建数据也不重建图层
  数据:页面内生成的 240 个点位,每个点带一个相位偏移
  依赖: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 input[type=range]{width:120px}
  .bar button{background:#21262d;color:#c9d1d9;border:1px solid #30363d;border-radius:4px;padding:3px 9px;
       font:12px/1.4 inherit;cursor:pointer}
  .bar button:hover{border-color:#58a6ff}
  #cv{flex:1 1 auto;min-height:0;width:100%}
</style>
</head>
<body>
<div class="bar">
  <span>点位 <b id="n">—</b></span>
  <span>帧 <b id="f">0</b></span>
  <label>速度 <input id="s" type="range" min="0" max="3" step="0.1" value="1"> <b id="sv">1.0</b></label>
  <button id="pause">暂停</button>
</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 centers = [
    [116.39, 39.90], [121.47, 31.23], [113.26, 23.12],
    [104.06, 30.57], [114.30, 30.59], [108.94, 34.34]
  ];

  var data = [];
  for (var i = 0; i < 240; i++) {
    var c = centers[i % centers.length];
    data.push({
      lng: c[0] + (Math.random() - 0.5) * 3.4,
      lat: c[1] + (Math.random() - 0.5) * 2.6,
      phase: Math.random() * Math.PI * 2,
      size: 12 + Math.random() * 26
    });
  }
  document.getElementById('n').textContent = data.length;

  /* 两个同心圆做呼吸效果:外圈淡、内圈实。
     deck.gl 9 的图层实例不可变,改参数要新建实例再交给 Deck,不能对旧实例 setProps。 */
  function makeRipple(scale) {
    return new deck.ScatterplotLayer({
      id: 'ripple',
      data: data,
      getPosition: function (d) { return [d.lng, d.lat]; },
      getRadius: function (d) { return d.size; },
      radiusUnits: 'pixels',
      radiusScale: scale,
      getFillColor: [56, 189, 248, 28],
      stroked: true,
      getLineColor: [56, 189, 248, 90],
      lineWidthMinPixels: 1
    });
  }

  var ripple = makeRipple(1);

  var core = new deck.ScatterplotLayer({
    id: 'core',
    data: data,
    getPosition: function (d) { return [d.lng, d.lat]; },
    getRadius: 3,
    radiusUnits: 'pixels',
    radiusMinPixels: 2,
    getFillColor: [186, 230, 253, 230]
  });

  var deckgl = new deck.Deck({
    canvas: document.getElementById('cv'),
    width: '100%',
    height: '100%',
    initialViewState: {
      longitude: 109,
      latitude: 33,
      zoom: 3.8,
      pitch: 0,
      bearing: 0
    },
    controller: true,
    layers: [ripple, core]
  });

  var speed = 1;
  var running = true;
  var frame = 0;
  var t = 0;
  var frames = document.getElementById('f');

  function tick() {
    if (running) {
      t += 0.02 * speed * data.length / 240;
      var s = Math.sin(t);
      ripple = makeRipple(0.7 + 0.9 * s * s);
      deckgl.setProps({ layers: [ripple, core] });
      frame++;
      if (frame % 10 === 0) frames.textContent = frame;
    }
    requestAnimationFrame(tick);
  }
  requestAnimationFrame(tick);

  document.getElementById('s').addEventListener('input', function () {
    speed = parseFloat(this.value);
    document.getElementById('sv').textContent = speed.toFixed(1);
  });

  document.getElementById('pause').addEventListener('click', function () {
    running = !running;
    this.textContent = running ? '暂停' : '继续';
  });

  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 · EXAMPLEArc deck.gl · EXAMPLEArc Curve deck.gl · EXAMPLEBasemap Esri