Billboards
Screen-space icons that always face the camera, with scale, origin and distance rules.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:用 canvas 画图标并作为 billboard 标注城市
说明:图标在内存中绘制成 dataURL,挂在 billboard 上,演示 scale、verticalOrigin 与贴地
核心逻辑:canvas 画水滴标记 → dataURL → 每个城市一个 Entity(billboard + label)
依赖: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:7px}
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="sVal">1.0</span></div>
<div class="row"><input id="s" type="range" min="0.4" max="2.4" step="0.1" value="1"></div>
<div class="row"><label><input id="clamp" type="checkbox" checked> 底部贴地</label></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;
/* ---------- 画一个水滴形标记 ---------- */
function makeIcon(fill) {
var w = 64, h = 84;
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var g = canvas.getContext('2d');
g.beginPath();
g.arc(w / 2, 32, 22, Math.PI * 0.85, Math.PI * 2.15);
g.lineTo(w / 2, h - 8);
g.closePath();
g.fillStyle = fill;
g.fill();
g.lineWidth = 4;
g.strokeStyle = 'rgba(255,255,255,.92)';
g.stroke();
g.beginPath();
g.arc(w / 2, 32, 9, 0, Math.PI * 2);
g.fillStyle = 'rgba(255,255,255,.85)';
g.fill();
return canvas.toDataURL('image/png');
}
var ICONS = ['#4d9bff', '#39c0a0', '#f2a03d', '#e45c7a', '#8e7cf0', '#3fb6e8'];
var cities = [
[116.391263, 39.907359, '北京'],
[121.473701, 31.230416, '上海'],
[113.264385, 23.129112, '广州'],
[104.066541, 30.572269, '成都'],
[87.616848, 43.825592, '乌鲁木齐'],
[126.642464, 45.756967, '哈尔滨']
];
var billboards = [];
cities.forEach(function (c, i) {
var entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(c[0], c[1], 0),
billboard: {
image: makeIcon(ICONS[i % ICONS.length]),
scale: 1,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
label: {
text: c[2],
font: '500 13px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.WHITE,
outlineColor: Cesium.Color.fromCssColorString('#0e1116'),
outlineWidth: 3,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
verticalOrigin: Cesium.VerticalOrigin.TOP,
pixelOffset: new Cesium.Cartesian2(0, 4),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
billboards.push(entity);
});
document.getElementById('s').addEventListener('input', function () {
var v = parseFloat(this.value);
document.getElementById('sVal').textContent = v.toFixed(1);
billboards.forEach(function (e) { e.billboard.scale = v; });
});
document.getElementById('clamp').addEventListener('change', function () {
var ref = this.checked
? Cesium.HeightReference.CLAMP_TO_GROUND
: Cesium.HeightReference.NONE;
billboards.forEach(function (e) { e.billboard.heightReference = ref; });
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(110, 33, 6200000),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-90),
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.