前言
缺少前置学习使用资料,请自行查阅:[https://blog.csdn.net/weixin_44402694/article/details/123110136](https://blog.csdn.net/weixin_44402694/article/details/123110136)
以下示例使用到的公共静态资料,不建议下载,建议官网自行下载超图Build资源,示例所涉及图片会在示例使用到时提供出来。如有需要可下载:[https://download.csdn.net/download/weixin_44402694/82180350](https://download.csdn.net/download/weixin_44402694/82180350)。
特效-云层设置。
使用
-
效果
-
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>特效 - 云层设置</title>
<link href="./public/Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
<script
type="text/javascript"
src="./public/Build/Cesium/Cesium.js"
></script>
<style>
* {
margin: 0;
padding: 0;
}
html,
body,
#cesium-container {
width: 100%;
height: 100%;
}
.panel {
position: fixed;
left: 10px;
top: 10px;
z-index: 1;
background-color: #fff;
padding: 10px;
}
.panel .btn {
cursor: pointer;
display: none;
}
.panel .check {
cursor: pointer;
}
</style>
</head>
<body>
<div id="cesium-container" />
<div class="panel">
<p class="check">开启云层 <input type="checkbox" /></p>
<p class="btn">云层纹理1</p>
<p class="btn">云层纹理2</p>
</div>
<script>
let viewer, cloudBox
window.onload = function () {
viewer = new Cesium.Viewer('cesium-container', {
sceneModePicker: true,
navigation: false,
})
initCreateCloudBoxHandler()
initBindDrawEventHandler()
}
// 初始化绑定按钮的绘制事件
function initBindDrawEventHandler() {
const check = document.querySelector('.check input')
const btns = document.querySelectorAll('.panel .btn')
check.addEventListener('change', () => {
Array.from(btns).forEach((btn) => {
btn.style.display = check.checked ? 'block' : 'none'
})
if (check.checked) {
// 开启云层
viewer.scene.cloudBox = cloudBox
// 默认高亮云层纹理1
viewer.scene.cloudBox.url =
'http://support.supermap.com.cn:8090/webgl/examples/webgl/images/clouds-supermap-sm.png'
activeCurrentClickBtnHandler(0)
} else {
viewer.scene.cloudBox = null
}
})
btns[0].addEventListener('click', () => {
activeCurrentClickBtnHandler(0)
viewer.scene.cloudBox.url =
'http://support.supermap.com.cn:8090/webgl/examples/webgl/images/clouds-supermap-sm.png'
})
btns[1].addEventListener('click', () => {
activeCurrentClickBtnHandler(1)
viewer.scene.cloudBox.url =
'http://support.supermap.com.cn:8090/webgl/examples/webgl/images/clouds.png'
})
}
// 初始化创建云层box
function initCreateCloudBoxHandler() {
cloudBox = new Cesium.CloudBox({
url: 'http://support.supermap.com.cn:8090/webgl/examples/webgl/images/clouds-supermap-sm.png',
})
}
// 高亮当前点击的按钮
function activeCurrentClickBtnHandler(idx) {
const btns = document.querySelectorAll('.panel .btn')
Array.from(btns).forEach((btn, index) => {
btn.style.color = index === idx ? 'red' : '#000'
})
}
</script>
</body>
</html>