Debug Stats
Runnable Cesium example: Debug Stats. 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
<!--
示例:把帧率、图元数量与相机高度摆在一个面板里
要点:scene.debugShowFramesPerSecond 提供内置帧率,postRender 里读取场景统计
依赖: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,.93);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;width:262px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.kv{display:flex;justify-content:space-between;line-height:1.9}
.kv span:first-child{color:#7f8896}
.kv span:last-child{font-family:ui-monospace,Consolas,monospace;font-size:11px;color:#8fb7e0}
.hud button{font:inherit;font-size:12px;padding:3px 9px;margin-right:4px;border-radius:4px;
cursor:pointer;border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
.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 class="kv"><span>内置帧率</span><span id="fps">已开启</span></div>
<div class="kv"><span>顶层图元</span><span id="prim">—</span></div>
<div class="kv"><span>实体数量</span><span id="ent">—</span></div>
<div class="kv"><span>后处理阶段</span><span id="stage">—</span></div>
<div class="kv"><span>相机高度</span><span id="height">—</span></div>
</div>
<div class="row">
<button id="fpsBtn" class="on">帧率</button>
<button id="fogBtn" class="on">雾</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;
/* 打开内置帧率显示,会出现在画面右上角 */
scene.debugShowFramesPerSecond = true;
scene.fog.enabled = true;
/* 一批箱体与一条贴地线,让统计数字有内容 */
for (var i = 0; i < 8; i++) {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.366 + i * 0.006, 39.904, 90),
box: {
dimensions: new Cesium.Cartesian3(140, 140, 180),
material: Cesium.Color.fromCssColorString(i % 2 ? '#2f4a66' : '#3a5a72'),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#8fb7e0')
}
});
}
viewer.entities.add({
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray([
116.360, 39.896, 116.400, 39.896, 116.400, 39.912, 116.360, 39.912,
116.360, 39.896
]),
width: 2,
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString('#ffb13d'),
dashLength: 18
}),
clampToGround: true
}
});
var primEl = document.getElementById('prim');
var entEl = document.getElementById('ent');
var stageEl = document.getElementById('stage');
var heightEl = document.getElementById('height');
var frame = 0;
var carto = new Cesium.Cartographic();
scene.postRender.addEventListener(function () {
if (++frame % 20 !== 0) return;
primEl.textContent = scene.primitives.length;
entEl.textContent = viewer.entities.values.length;
stageEl.textContent = scene.postProcessStages.length;
Cesium.Cartographic.fromCartesian(viewer.camera.positionWC, undefined, carto);
heightEl.textContent = Math.round(carto.height) + ' 米';
});
document.getElementById('fpsBtn').addEventListener('click', function () {
scene.debugShowFramesPerSecond = !scene.debugShowFramesPerSecond;
this.className = scene.debugShowFramesPerSecond ? 'on' : '';
document.getElementById('fps').textContent =
scene.debugShowFramesPerSecond ? '已开启' : '已关闭';
});
document.getElementById('fogBtn').addEventListener('click', function () {
scene.fog.enabled = !scene.fog.enabled;
this.className = scene.fog.enabled ? 'on' : '';
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.389, 39.876, 4200),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-24),
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.