Atlaslog
Atlaslog / Examples / Cesium / Camera Follow
Cesium · Runnable Example

Camera Follow

Runnable Cesium example: Camera Follow. 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-camera-follow.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-camera-follow.html
<!--
  示例:相机跟随运动目标
  要点:SampledPositionProperty 提供时间化轨迹,trackedEntity 让相机挂在实体上,viewFrom 控制机位
  依赖: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:252px}
  .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-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>相机跟随</b></div>
  <div class="row" id="state">跟随中 · 无人机</div>
  <div class="row">
    <button id="follow" class="on">跟随</button>
    <button id="free">自由视角</button>
  </div>
  <div class="row">
    <button data-speed="1">1×</button>
    <button data-speed="4" class="on">4×</button>
    <button data-speed="12">12×</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: false, timeline: false, 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;

  /* 方向箭头图标:canvas 画一个朝上的三角 */
  function icon() {
    var c = document.createElement('canvas');
    c.width = c.height = 48;
    var ctx = c.getContext('2d');
    ctx.translate(24, 24);
    ctx.beginPath();
    ctx.moveTo(0, -18);
    ctx.lineTo(13, 14);
    ctx.lineTo(0, 7);
    ctx.lineTo(-13, 14);
    ctx.closePath();
    ctx.fillStyle = '#ffb13d';
    ctx.fill();
    ctx.lineWidth = 2;
    ctx.strokeStyle = '#fff3e0';
    ctx.stroke();
    return c;
  }

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

  var position = new Cesium.SampledPositionProperty();
  var STEPS = 30;
  for (var i = 0; i <= STEPS; i++) {
    var t = Cesium.JulianDate.addSeconds(START, i * 5, new Cesium.JulianDate());
    var a = (i / STEPS) * Math.PI * 2;
    var lng = 116.391263 + Math.cos(a) * 0.045;
    var lat = 39.907359 + Math.sin(a) * 0.032;
    var h = 1100 + 420 * Math.sin(a * 2);
    position.addSample(t, Cesium.Cartesian3.fromDegrees(lng, lat, h));
  }
  position.setInterpolationOptions({
    interpolationDegree: 2,
    interpolationAlgorithm: Cesium.HermitePolynomialApproximation
  });

  var drone = viewer.entities.add({
    id: 'drone',
    name: '无人机',
    position: position,
    viewFrom: new Cesium.Cartesian3(0, -620, 380),
    billboard: {
      image: icon(),
      scale: 0.9,
      alignedAxis: new Cesium.VelocityVectorProperty(position, true),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    },
    path: {
      resolution: 1,
      width: 2,
      material: new Cesium.PolylineGlowMaterialProperty({
        glowPower: 0.2,
        color: Cesium.Color.fromCssColorString('#ffb13d').withAlpha(0.85)
      })
    },
    label: {
      text: '无人机 01',
      font: '500 12px "Microsoft YaHei", sans-serif',
      fillColor: Cesium.Color.fromCssColorString('#ffb13d'),
      pixelOffset: new Cesium.Cartesian2(0, -26),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    }
  });

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

  var stateEl = document.getElementById('state');
  var followBtn = document.getElementById('follow');
  var freeBtn = document.getElementById('free');

  viewer.trackedEntity = drone;

  followBtn.addEventListener('click', function () {
    viewer.trackedEntity = drone;
    followBtn.className = 'on';
    freeBtn.className = '';
    stateEl.textContent = '跟随中 · 无人机';
  });

  freeBtn.addEventListener('click', function () {
    viewer.trackedEntity = undefined;
    followBtn.className = '';
    freeBtn.className = 'on';
    stateEl.textContent = '自由视角 · 可拖动地球';
  });

  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'));
    });
  }
})();
</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 · EXAMPLECamera Orbit Cesium · EXAMPLECamera Set View Cesium · EXAMPLECamera Tour