Atlaslog
Atlaslog / Examples / Cesium / Particle Trail
Cesium · Runnable Example

Particle Trail

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

Source

demos/cesium-particle-trail.html
<!--
  示例:给运动目标加一条粒子拖尾
  要点:发射器固定在原点,粒子出生时写入目标当前局部坐标,于是留下轨迹
  依赖: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,.92);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;width:252px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  .hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
       border:1px solid #343b46;background:#1d232b;color:#cfd6e2;margin-top:8px}
  .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">目标沿椭圆轨道飞行</div>
  <div class="row">粒子生命 2.2 秒,拖尾长度固定</div>
  <div class="row"><button id="toggle">暂停</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;

  var CENTER = { lng: 116.389, lat: 39.906 };
  var ORIGIN = Cesium.Cartesian3.fromDegrees(CENTER.lng, CENTER.lat, 0);
  var MATRIX = Cesium.Transforms.eastNorthUpToFixedFrame(ORIGIN);
  var INV = Cesium.Matrix4.inverse(MATRIX, new Cesium.Matrix4());

  /* 目标位置:按秒数在椭圆上跑一圈,30 秒一圈 */
  function targetAt(seconds) {
    var a = seconds / 30 * Math.PI * 2;
    return Cesium.Cartesian3.fromDegrees(
      CENTER.lng + Math.cos(a) * 0.013,
      CENTER.lat + Math.sin(a) * 0.009,
      320 + Math.sin(a * 2) * 120
    );
  }

  /* 光点贴图 */
  function glowTexture() {
    var c = document.createElement('canvas');
    c.width = c.height = 48;
    var ctx = c.getContext('2d');
    var g = ctx.createRadialGradient(24, 24, 0, 24, 24, 24);
    g.addColorStop(0, 'rgba(255,255,255,1)');
    g.addColorStop(0.4, 'rgba(140,220,255,0.9)');
    g.addColorStop(1, 'rgba(80,160,255,0)');
    ctx.fillStyle = g;
    ctx.fillRect(0, 0, 48, 48);
    return c;
  }

  var localTarget = new Cesium.Cartesian3();
  var birth = false;

  /* 粒子刚出生时搬到目标此刻的位置 */
  function attachToTarget(particle, dt) {
    if (particle.age < 0.06) {
      Cesium.Cartesian3.clone(localTarget, particle.position);
      particle.velocity = new Cesium.Cartesian3(0, 0, 0);
    }
  }

  var trail = new Cesium.ParticleSystem({
    image: glowTexture(),
    startColor: Cesium.Color.fromCssColorString('#9fe4ff').withAlpha(0.95),
    endColor: Cesium.Color.fromCssColorString('#2f6ea8').withAlpha(0.0),
    startScale: 0.9,
    endScale: 0.2,
    minimumParticleLife: 2.0,
    maximumParticleLife: 2.4,
    minimumSpeed: 0,
    maximumSpeed: 0,
    imageSize: new Cesium.Cartesian2(16, 16),
    emissionRate: 320,
    emitter: new Cesium.SphereEmitter(0.6),
    modelMatrix: MATRIX,
    lifetime: 60,
    updateCallback: attachToTarget
  });
  scene.primitives.add(trail);

  var seconds = 0;
  var paused = false;

  var target = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(CENTER.lng, CENTER.lat, 320),
    point: {
      pixelSize: 14,
      color: Cesium.Color.fromCssColorString('#eaf2ff'),
      outlineColor: Cesium.Color.fromCssColorString('#4d9bff'),
      outlineWidth: 2,
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    }
  });

  /* 轨道参考线 */
  var orbit = [];
  for (var i = 0; i <= 72; i++) {
    var a = i / 72 * Math.PI * 2;
    orbit.push(
      CENTER.lng + Math.cos(a) * 0.013,
      CENTER.lat + Math.sin(a) * 0.009
    );
  }
  viewer.entities.add({
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(orbit),
      width: 1.5,
      material: new Cesium.PolylineDashMaterialProperty({
        color: Cesium.Color.fromCssColorString('#41536a'),
        dashLength: 18
      }),
      clampToGround: true
    }
  });

  var last = performance.now();

  scene.preUpdate.addEventListener(function () {
    var now = performance.now();
    var dt = Math.min((now - last) / 1000, 0.1);
    last = now;
    if (!paused) seconds += dt;

    var world = targetAt(seconds);
    target.position = world;

    /* 世界坐标换到粒子系统的局部坐标,喂给出生的粒子 */
    Cesium.Matrix4.multiplyByPoint(INV, world, localTarget);
  });

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

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.389, 39.874, 2600),
    orientation: {
      heading: 0,
      pitch: Cesium.Math.toRadians(-26),
      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 · EXAMPLEParticle Weather Cesium · EXAMPLEPer Feature Post Processing Cesium · EXAMPLEPhysically Based Materials