Render Mode
Runnable Cesium example: Render Mode. 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
<!--
示例:静止场景下不再逐帧重绘
要点:requestRenderMode 打开后,只有相机变化或调用 requestRender 才渲染
依赖: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,.92);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;width:256px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.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}
.mono{font-family:ui-monospace,Consolas,monospace;font-size:11px;color:#8fb7e0}
.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">
<button id="on" class="on">开启</button>
<button id="off">关闭</button>
<button id="once">手动重绘</button>
</div>
<div class="row mono" id="cnt">累计渲染 0 帧</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;
/* 静态内容:一片楼群,不随时间变化 */
for (var i = 0; i < 6; i++) {
for (var j = 0; j < 4; j++) {
var h = 120 + ((i + j) % 3) * 90;
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(
116.372 + i * 0.008, 39.898 + j * 0.008, h / 2
),
box: {
dimensions: new Cesium.Cartesian3(180, 180, h),
material: Cesium.Color.fromCssColorString(
(i + j) % 2 === 0 ? '#2c4a6b' : '#354f63'
),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#7fa6c9')
}
});
}
}
/* 打开按需渲染:maximumRenderTimeChange 设为无穷,静止时彻底不画 */
scene.requestRenderMode = true;
scene.maximumRenderTimeChange = Number.POSITIVE_INFINITY;
var frames = 0;
scene.postRender.addEventListener(function () {
frames++;
});
/* 用定时器更新文字,不触发 Cesium 渲染 */
setInterval(function () {
document.getElementById('cnt').textContent = '累计渲染 ' + frames + ' 帧';
}, 400);
document.getElementById('on').addEventListener('click', function () {
scene.requestRenderMode = true;
scene.maximumRenderTimeChange = Number.POSITIVE_INFINITY;
this.className = 'on';
document.getElementById('off').className = '';
});
document.getElementById('off').addEventListener('click', function () {
scene.requestRenderMode = false;
this.className = 'on';
document.getElementById('on').className = '';
});
document.getElementById('once').addEventListener('click', function () {
scene.requestRender();
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.364, 39.886, 1900),
orientation: {
heading: Cesium.Math.toRadians(26),
pitch: Cesium.Math.toRadians(-30),
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.