Cesium 学习教程

编程入门 行业动态 更新时间:2024-10-28 20:29:37

Cesium 学习<a href=https://www.elefans.com/category/jswz/34/1771193.html style=教程"/>

Cesium 学习教程

Cesium 学习教程 - 地球以及渲染数据导出(打印)图片

    • 地球导出核心代码
    • 完整代码:
    • 在线示例

本文包括地球导出核心代码、完整代码以及在线示例。


地球导出核心代码

这里放上核心代码:


/*** @todo canvas 导出图片* @param {string} dataurl - 地址* @return {Blob}*/
function dataURLtoBlob(dataurl) {const arr = dataurl.split(',');const mime = arr[0].match(/:(.*?);/)[1];const bstr = atob(arr[1]);let n = bstr.length;const u8arr = new Uint8Array(n);while (n--) {u8arr[n] = bstr.charCodeAt(n);}return new Blob([u8arr], {type: mime});
}/*** @todo 场景导出* @description 场景导出* @return {*}*/
function exportFunc() {// 不写会导出为黑图viewer.render();// 地球 canvasconst canvas = viewer.scene.canvas;// canvas 转图片const image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');// 利用 a 标签下载导出const link = document.createElement('a');const blob = dataURLtoBlob(image);// 创建链接地址const objurl = URL.createObjectURL(blob);link.download = 'scene.png';link.href = objurl;link.click();
}

完整代码:


<!DOCTYPE html>
<html lang="en">
<head><!-- Use correct character set. --><meta charset="utf-8"/><!-- Tell IE to use the latest, best version. --><meta http-equiv="X-UA-Compatible" content="IE=edge"/><!-- Make the application on mobile take up the full browser screen and disable user scaling. --><metaname="viewport"content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/><title>Cesium export</title><script src="/examples/csdn/Cesium.js"></script><script src="./cesium_init.js"></script><script src="/examples/resources/jquery-3.5.1.min.js"></script><style>@import url(./Widgets/widgets.css);html,body,#cesiumContainer {width: 100%;height: 100%;margin: 0;padding: 0;overflow: hidden;}</style><script>var _hmt = _hmt || [];(function () {var hm = document.createElement("script");hm.src = ".js?f80a36f14f8a73bb0f82e0fdbcee3058";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();</script>
</head>
<body>
<button id="exportFunc" onClick="exportFunc()">打印地球</button>
<div id="cesiumContainer"></div>
<script>// 创建三维球const viewer = init();// 条纹材质const stripeMaterial = new Cesium.StripeMaterialProperty({evenColor: Cesium.Color.WHITE.withAlpha(0.5),oddColor: Cesium.Color.BLUE.withAlpha(0.5),repeat: 5.0,});// 多边形坐标const cartesian3s = Cesium.Cartesian3.fromDegreesArray([-107.0,27.0,-107.0,22.0,-102.0,23.0,-97.0,21.0,-97.0,25.0,]);// 定位至多边形viewer.flyTo(viewer.entities.add({// 添加多边形polygon: {hierarchy: new Cesium.PolygonHierarchy(cartesian3s),outline: true,outlineColor: Cesium.Color.WHITE,outlineWidth: 4,material: stripeMaterial,},}));/*** @todo canvas 导出图片* @param {string} dataurl - 地址* @return {Blob}*/function dataURLtoBlob(dataurl) {const arr = dataurl.split(',');const mime = arr[0].match(/:(.*?);/)[1];const bstr = atob(arr[1]);let n = bstr.length;const u8arr = new Uint8Array(n);while (n--) {u8arr[n] = bstr.charCodeAt(n);}return new Blob([u8arr], {type: mime});}/*** @todo 场景导出* @description 场景导出* @return {*}*/function exportFunc() {// 不写会导出为黑图viewer.render();// 地球 canvasconst canvas = viewer.scene.canvas;// canvas 转图片const image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');// 利用 a 标签下载导出const link = document.createElement('a');const blob = dataURLtoBlob(image);// 创建链接地址const objurl = URL.createObjectURL(blob);link.download = 'scene.png';link.href = objurl;link.click();}</script>
</body>
</html>


在线示例

Cesium 在线示例:地球以及渲染数据导出

更多推荐

Cesium 学习教程

本文发布于:2023-12-06 22:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1669197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:教程   Cesium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!