CallbackProperty就是属性回调函数,对于实体来说,比如一条线,如果我想改变其线宽,或者改变其颜色,但又不想重新绘制,那CallbackProperty就派上了用场。
请看如下示例:
let viewer = new Cesium.Viewer('cesiumContainer');
let entity = viewer.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(changePosition, false),
width: new Cesium.CallbackProperty(changeWidth, false),
material: new Cesium.ColorMaterialProperty(new Cesium.CallbackProperty(changeColor, false)),
}
});
function changePosition(time, isConstant) {
let lat = 20;
let lon = 100;
return Cesium.Cartesian3.fromDegreesArray(
[lon, lat, lon - Math.floor(Math.random() * 1000), lat - Math.floor(Math.random() * 100)],
Cesium.Ellipsoid.WGS84,
isConstant
);
}
function changeWidth(time, isConstant) {
return Math.floor(Math.random() * 10);
}
function changeColor(time, isConstant) {
return Cesium.Color.fromRandom({
minimumRed: 0.1,
minimumGreen: 0.1,
minimumBlue: 0.1,
alpha: 1.0
});
}