Atlaslog
Atlaslog / Examples / Cesium / Ellipse Fan
Cesium · Runnable Example

Ellipse Fan

Runnable Cesium example: Ellipse Fan. 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-ellipse-fan.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-ellipse-fan.html
<!--
  示例:椭圆与扇形两种面状图元
  要点:EllipseGraphics 用长短半轴画椭圆,PolygonGraphics 用弧线坐标拼出扇形
  依赖: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:6px}
  input[type=range]{width:100%}
  .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">扇形圆心角 <span id="arcVal">120</span>°</div>
  <div class="row"><input id="arc" type="range" min="20" max="330" step="10" value="120"></div>
  <div class="row">椭圆用长短半轴,扇形按弧长采样</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 LON = 116.388;
  var LAT = 39.907;

  /* 椭圆:东西 1400 米、南北 800 米,拉伸 180 米 */
  viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(LON, LAT),
    ellipse: {
      semiMajorAxis: 1400,
      semiMinorAxis: 800,
      material: Cesium.Color.fromCssColorString('#3f86e0').withAlpha(0.7),
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString('#cfe4ff'),
      extrudedHeight: 180
    },
    label: {
      text: '椭圆 extrudedHeight 180',
      font: '500 12px "Microsoft YaHei", sans-serif',
      fillColor: Cesium.Color.WHITE,
      pixelOffset: new Cesium.Cartesian2(0, -8),
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    }
  });

  /* 扇形:以圆心为顶点,按圆心角采样弧线上的点 */
  var RADIUS = 1600;
  var fan = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(LON + 0.02, LAT - 0.012),
    polygon: {
      hierarchy: new Cesium.CallbackProperty(function (time, result) {
        return computeFan(parseFloat(document.getElementById('arc').value), result);
      }, false),
      material: Cesium.Color.fromCssColorString('#ffb13d').withAlpha(0.45),
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString('#ffd18a'),
      height: 2
    }
  });

  var CENTER = { lng: LON + 0.02, lat: LAT - 0.012 };

  function computeFan(degrees, result) {
    var positions = [];
    positions.push(Cesium.Cartesian3.fromDegrees(CENTER.lng, CENTER.lat, 2));

    var start = -degrees / 2;
    var steps = 48;
    for (var i = 0; i <= steps; i++) {
      var bearing = start + (degrees / steps) * i;
      positions.push(pointAt(CENTER.lng, CENTER.lat, RADIUS, bearing));
    }
    return new Cesium.PolygonHierarchy(positions);
  }

  /* 由圆心沿方位角推算出半径上的点,近似平面投影 */
  function pointAt(lng, lat, distance, bearingDeg) {
    var b = Cesium.Math.toRadians(bearingDeg);
    var mPerDegLat = 111320;
    var mPerDegLng = 111320 * Math.cos(Cesium.Math.toRadians(lat));
    return Cesium.Cartesian3.fromDegrees(
      lng + Math.sin(b) * distance / mPerDegLng,
      lat + Math.cos(b) * distance / mPerDegLat,
      2
    );
  }

  document.getElementById('arc').addEventListener('input', function () {
    document.getElementById('arcVal').textContent = this.value;
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.398, 39.888, 4200),
    orientation: {
      heading: Cesium.Math.toRadians(20),
      pitch: Cesium.Math.toRadians(-38),
      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 · EXAMPLEEntity Drag Cesium · EXAMPLEExport Kml Cesium · EXAMPLEFlyline