Bathymetry
A code-generated underwater surface, rendered without external imagery or tokens.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:用 canvas 画出水深分层贴图并铺到球体
说明:按深度档位配色绘制全球海域,再叠上粗略大陆,交给 SingleTileImageryProvider 当作底图
核心逻辑:canvas 画色带与大陆 → toDataURL → SingleTileImageryProvider + Rectangle.MAX_VALUE
依赖: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:260px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.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">从近岸青绿到深海群青,共 6 档</div>
<div class="row"><label><input id="land" type="checkbox" checked> 显示大陆</label></div>
<div class="row" id="info">贴图 2048 × 1024,页面内生成</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.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 CW = 2048;
var CH = 1024;
var canvas = document.createElement('canvas');
canvas.width = CW;
canvas.height = CH;
var g = canvas.getContext('2d');
function px(lon) { return (lon + 180) / 360 * CW; }
function py(lat) { return (90 - lat) / 180 * CH; }
/* ---------- 深海到浅海的六档色 ---------- */
var bands = ['#123a5c', '#17496f', '#1c5b84', '#226e99', '#2a83ad', '#3aa0bd'];
g.fillStyle = bands[0];
g.fillRect(0, 0, CW, CH);
/* 用固定公式撒若干深色斑块,模拟洋盆与海沟 */
for (var k = 1; k < bands.length; k++) {
g.fillStyle = bands[k];
for (var n = 0; n < 26; n++) {
var seed = (k * 71 + n * 37) % 211;
var cx = (seed * 47) % CW;
var cy = (seed * 89) % CH;
var r = 90 + (seed % 200) - k * 20;
if (r <= 0) continue;
g.beginPath();
g.ellipse(cx, cy, r * 1.6, r, 0, 0, Math.PI * 2);
g.fill();
}
/* 每画完一档就缩一圈,形成由深到浅的层次 */
}
/* ---------- 粗略大陆:经纬度顶点直接连成多边形 ---------- */
var lands = [
[[-168, 65], [-140, 70], [-95, 73], [-62, 61], [-55, 45], [-66, 25],
[-82, 25], [-97, 18], [-105, 23], [-118, 32], [-125, 48], [-140, 60]],
[[-81, 10], [-60, 12], [-35, -5], [-38, -22], [-48, -33], [-70, -52],
[-75, -45], [-73, -18], [-81, -5]],
[[-17, 15], [10, 36], [32, 32], [43, 12], [51, 11], [40, -5],
[38, -20], [20, -35], [12, -18], [9, 4], [-8, 5]],
[[-10, 36], [0, 44], [5, 52], [30, 70], [60, 72], [140, 72], [160, 68],
[140, 50], [122, 40], [120, 22], [105, 10], [100, 5], [92, 21],
[80, 8], [72, 20], [60, 25], [45, 40], [28, 36], [18, 40], [10, 44]],
[[114, -22], [122, -18], [130, -12], [142, -10], [150, -25], [153, -38],
[140, -38], [128, -32], [115, -34]],
[[-45, 83], [-20, 80], [-22, 68], [-42, 60], [-55, 68], [-55, 78]],
[[-180, -66], [-120, -72], [-60, -68], [0, -70], [60, -67], [120, -72],
[180, -66], [180, -90], [-180, -90]]
];
function drawLand(alpha) {
g.globalAlpha = alpha;
g.fillStyle = '#c8b48a';
g.strokeStyle = '#8f7c58';
g.lineWidth = 2;
lands.forEach(function (ring) {
g.beginPath();
ring.forEach(function (p, i) {
var x = px(p[0]);
var y = py(p[1]);
i === 0 ? g.moveTo(x, y) : g.lineTo(x, y);
});
g.closePath();
g.fill();
g.stroke();
});
g.globalAlpha = 1;
}
drawLand(1);
var url = canvas.toDataURL('image/png');
var provider = new Cesium.SingleTileImageryProvider({
url: url,
rectangle: Cesium.Rectangle.MAX_VALUE,
tileWidth: CW,
tileHeight: CH
});
var layer = viewer.imageryLayers.addImageryProvider(provider);
document.getElementById('land').addEventListener('change', function () {
if (this.checked) {
layer.show = true;
document.getElementById('info').textContent = '贴图 2048 × 1024,页面内生成';
} else {
layer.show = false;
scene.globe.baseColor = Cesium.Color.fromCssColorString('#123a5c');
document.getElementById('info').textContent = '已隐藏贴图,只看球体底色';
}
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(105, 12, 21000000),
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.