Callback Property
Runnable Cesium example: Callback Property. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:用 CallbackProperty 每帧算位置与半径
说明:物体的位置和圆半径都由函数实时给出,不用预先采样
核心逻辑:new Cesium.CallbackProperty(fn, false) 挂在 position 与 ellipse.semiMajorAxis 上
依赖: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:260px}
.hud b{color:#e6e8ec;font-weight:600}
.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>位置在绕圈,圆的半径按正弦变化</div>
<div>两个属性都由 <code>isConstant = false</code> 的回调给出</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.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
var start = Cesium.JulianDate.now().clone();
/* 半径按正弦变化,半轴在 6 km 到 16 km 之间摆动 */
var radius = new Cesium.CallbackProperty(function (time) {
var s = Cesium.JulianDate.secondsDifference(time, start);
return 8000 + 6000 * Math.sin(s * 1.2);
}, false);
/* 位置绕中心点转圈,高度固定 */
var position = new Cesium.CallbackProperty(function (time, result) {
var s = Cesium.JulianDate.secondsDifference(time, start);
var lon = 116.392 + 0.045 * Math.cos(s * 0.5);
var lat = 39.900 + 0.028 * Math.sin(s * 0.5);
return Cesium.Cartesian3.fromDegrees(lon, lat, 2600, Cesium.Ellipsoid.WGS84, result);
}, false);
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.392, 39.900, 0),
ellipse: {
semiMajorAxis: radius,
semiMinorAxis: radius,
height: 0,
material: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.18),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#8fc2ff').withAlpha(0.9)
}
});
viewer.entities.add({
position: position,
point: {
pixelSize: 12,
color: Cesium.Color.fromCssColorString('#ffd166'),
outlineColor: Cesium.Color.WHITE,
outlineWidth: 2,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
label: {
text: '回调算出的位置',
font: '500 12px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.WHITE,
pixelOffset: new Cesium.Cartesian2(0, -22),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
/* 轨迹线的位置也用回调给出,实时把走过的点连成圆 */
viewer.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(function (time, result) {
var s = Cesium.JulianDate.secondsDifference(time, start);
var n = 64;
var arr = new Array((n + 1) * 3);
for (var i = 0; i <= n; i++) {
var a = s * 0.5 + (i / n) * Math.PI * 2;
arr[i * 3] = 116.392 + 0.045 * Math.cos(a);
arr[i * 3 + 1] = 39.900 + 0.028 * Math.sin(a);
arr[i * 3 + 2] = 2600;
}
return Cesium.Cartesian3.fromDegreesArrayHeights(arr);
}, false),
width: 1.5,
material: Cesium.Color.fromCssColorString('#ffd166').withAlpha(0.5)
}
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.392, 39.884, 95000),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-52),
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.