Atlaslog
Atlaslog / Examples / Cesium / Animation Timeline
Cesium · Runnable Example

Animation Timeline

Runnable Cesium example: Animation Timeline. 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 Cesium examples
/demos/cesium-animation-timeline.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-animation-timeline.html
<!--
  示例:启用时间轴播放轨迹动画
  要点:Viewer 打开 animation 与 timeline,SampledPositionProperty 提供采样位置,clock 推进时间
  依赖:CesiumJS 1.121 · 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>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;max-width:250px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  .hud button{font:inherit;font-size:12px;padding:3px 9px;margin-right:4px;border-radius:4px;
       cursor:pointer;border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
  .hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
  .cesium-viewer-animationContainer{bottom:26px!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>时间轴动画</b></div>
  <div class="row">四段航线,共 240 秒模拟时间</div>
  <div class="row">
    <button id="play" class="on">播放</button>
    <button id="pause">暂停</button>
  </div>
  <div class="row">
    <button data-speed="1">1×</button>
    <button data-speed="8" class="on">8×</button>
    <button data-speed="30">30×</button>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: true, timeline: true, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  function icon() {
    var c = document.createElement('canvas');
    c.width = c.height = 44;
    var ctx = c.getContext('2d');
    ctx.translate(22, 22);
    ctx.beginPath();
    ctx.moveTo(0, -16);
    ctx.lineTo(14, 12);
    ctx.lineTo(0, 4);
    ctx.lineTo(-14, 12);
    ctx.closePath();
    ctx.fillStyle = '#4d9bff';
    ctx.fill();
    ctx.strokeStyle = '#dbeaff';
    ctx.lineWidth = 2;
    ctx.stroke();
    return c;
  }

  var START = Cesium.JulianDate.fromIso8601('2026-10-27T00:00:00Z');
  var STOP = Cesium.JulianDate.addSeconds(START, 240, new Cesium.JulianDate());

  /* 航点:经度、纬度、高度 */
  var WAYPOINTS = [
    { lng: 116.391263, lat: 39.907359, h: 1200 },
    { lng: 117.120000, lat: 36.650000, h: 4200 },
    { lng: 118.796000, lat: 32.060000, h: 6800 },
    { lng: 120.580000, lat: 31.300000, h: 3200 },
    { lng: 121.473701, lat: 31.230416, h: 900 }
  ];

  var position = new Cesium.SampledPositionProperty();
  var LEGS = WAYPOINTS.length - 1;
  var SAMPLES_PER_LEG = 12;
  var total = LEGS * SAMPLES_PER_LEG;

  for (var i = 0; i <= total; i++) {
    var f = i / total;
    var leg = Math.min(LEGS - 1, Math.floor(f * LEGS));
    var k = f * LEGS - leg;
    var a = WAYPOINTS[leg];
    var b = WAYPOINTS[leg + 1];

    var lng = a.lng + (b.lng - a.lng) * k;
    var lat = a.lat + (b.lat - a.lat) * k;
    /* 高度用平滑曲线过渡,避免航段接缝处跳变 */
    var h = a.h + (b.h - a.h) * (0.5 - 0.5 * Math.cos(k * Math.PI));

    position.addSample(
      Cesium.JulianDate.addSeconds(START, f * 240, new Cesium.JulianDate()),
      Cesium.Cartesian3.fromDegrees(lng, lat, h)
    );
  }

  position.setInterpolationOptions({
    interpolationDegree: 2,
    interpolationAlgorithm: Cesium.LagrangePolynomialApproximation
  });

  viewer.entities.add({
    position: position,
    billboard: {
      image: icon(),
      scale: 0.85,
      alignedAxis: new Cesium.VelocityVectorProperty(position, true),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    },
    path: {
      resolution: 1,
      width: 2,
      leadTime: 0,
      trailTime: 240,
      material: new Cesium.PolylineGlowMaterialProperty({
        glowPower: 0.2,
        color: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.8)
      })
    },
    label: {
      text: '航班 CA1831',
      font: '500 12px "Microsoft YaHei", sans-serif',
      fillColor: Cesium.Color.WHITE,
      pixelOffset: new Cesium.Cartesian2(0, -24),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    }
  });

  viewer.clock.startTime = START;
  viewer.clock.stopTime = STOP;
  viewer.clock.currentTime = START;
  viewer.clock.multiplier = 8;
  viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
  viewer.clock.shouldAnimate = true;

  viewer.timeline.zoomTo(START, STOP);

  document.getElementById('play').addEventListener('click', function () {
    viewer.clock.shouldAnimate = true;
    this.className = 'on';
    document.getElementById('pause').className = '';
  });

  document.getElementById('pause').addEventListener('click', function () {
    viewer.clock.shouldAnimate = false;
    this.className = 'on';
    document.getElementById('play').className = '';
  });

  var speedButtons = document.querySelectorAll('[data-speed]');
  for (var b = 0; b < speedButtons.length; b++) {
    speedButtons[b].addEventListener('click', function () {
      for (var k = 0; k < speedButtons.length; k++) speedButtons[k].className = '';
      this.className = 'on';
      viewer.clock.multiplier = parseFloat(this.getAttribute('data-speed'));
    });
  }

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(118.6, 33.5, 1900000),
    orientation: {
      heading: Cesium.Math.toRadians(4),
      pitch: Cesium.Math.toRadians(-73),
      roll: 0
    },
    duration: 0
  });
})();
</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 Cesium examples

Cesium · EXAMPLEAppearance Translucent Cesium · EXAMPLEArcgis Elevation Terrain Cesium · EXAMPLEArcgis Mapserver