Imagery Adjust
Runnable Cesium example: Imagery Adjust. 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
<!--
示例:影像图层的亮度、对比度、饱和度与伽马调整
要点:ImageryLayer 上的 brightness/contrast/saturation/gamma 是逐图层生效的显示参数
依赖:CesiumJS 1.121 · jsDelivr CDN,两层底图均由 canvas 生成
-->
<!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:236px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
input[type=range]{width:100%}
.hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
border:1px solid #343b46;background:#1d232b;color:#cfd6e2;margin-top:8px}
.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="bVal">1.00</span></div>
<div class="row"><input id="b" type="range" min="0.2" max="2" step="0.05" value="1"></div>
<div class="row">对比度 <span id="cVal">1.00</span></div>
<div class="row"><input id="c" type="range" min="0.2" max="2" step="0.05" value="1"></div>
<div class="row">饱和度 <span id="sVal">1.00</span></div>
<div class="row"><input id="s" type="range" min="0" max="2" step="0.05" value="1"></div>
<div class="row">伽马 <span id="gVal">1.00</span></div>
<div class="row"><input id="g" type="range" min="0.3" max="3" step="0.05" value="1"></div>
<div class="row"><button id="reset">恢复默认</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('#16202c');
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;
var RECT = Cesium.Rectangle.fromDegrees(115.8, 39.5, 117.0, 40.3);
/* 底图:深色地形感 */
function drawBase() {
var c = document.createElement('canvas');
c.width = 1024; c.height = 640;
var ctx = c.getContext('2d');
var g = ctx.createLinearGradient(0, 0, 0, 640);
g.addColorStop(0, '#16222f');
g.addColorStop(1, '#0d1620');
ctx.fillStyle = g;
ctx.fillRect(0, 0, 1024, 640);
ctx.strokeStyle = 'rgba(70,96,124,0.5)';
ctx.lineWidth = 1;
for (var x = 0; x < 1024; x += 80) {
ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, 640); ctx.stroke();
}
for (var y = 0; y < 640; y += 80) {
ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(1024, y); ctx.stroke();
}
return c.toDataURL('image/png');
}
/* 叠加层:彩色色块,方便观察调色效果 */
function drawOverlay() {
var c = document.createElement('canvas');
c.width = 1024; c.height = 640;
var ctx = c.getContext('2d');
ctx.clearRect(0, 0, 1024, 640);
var blocks = [
['#4d9bff', 80, 80], ['#5fd0a8', 380, 90], ['#ffb13d', 680, 80],
['#e35d6a', 100, 360], ['#9b7bff', 400, 350], ['#3f86e0', 700, 360]
];
blocks.forEach(function (b) {
ctx.fillStyle = b[0];
ctx.fillRect(b[1], b[2], 220, 170);
});
ctx.fillStyle = 'rgba(255,255,255,0.9)';
ctx.font = '600 34px "Microsoft YaHei", sans-serif';
ctx.fillText('调色测试层', 80, 300);
return c.toDataURL('image/png');
}
viewer.imageryLayers.addImageryProvider(new Cesium.SingleTileImageryProvider({
url: drawBase(), rectangle: RECT
}));
var overlay = viewer.imageryLayers.addImageryProvider(new Cesium.SingleTileImageryProvider({
url: drawOverlay(), rectangle: RECT
}));
overlay.alpha = 0.9;
var sliderIds = ['b', 'c', 's', 'g'];
var valueIds = ['bVal', 'cVal', 'sVal', 'gVal'];
function apply() {
overlay.brightness = parseFloat(document.getElementById('b').value);
overlay.contrast = parseFloat(document.getElementById('c').value);
overlay.saturation = parseFloat(document.getElementById('s').value);
overlay.gamma = parseFloat(document.getElementById('g').value);
sliderIds.forEach(function (id, i) {
document.getElementById(valueIds[i]).textContent =
parseFloat(document.getElementById(id).value).toFixed(2);
});
}
sliderIds.forEach(function (id) {
document.getElementById(id).addEventListener('input', apply);
});
document.getElementById('reset').addEventListener('click', function () {
sliderIds.forEach(function (id) {
document.getElementById(id).value = 1;
});
apply();
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.4, 39.2, 420000),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-78),
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.