Cesium@1.138中实现交互式多边形绘制与编辑工具类
/** * Cesium 交互式多边形绘制与编辑工具类草绿色系版 */ class PolygonDrawer { constructor(Cesium, viewer, options {}) { this.Cesium Cesium; this.viewer viewer; this.onFinish options.onFinish || (() {}); this.onEdit options.onEdit || (() {}); // 核心修改将默认填充色改为草绿色系带透明度 this.fillColor options.fillColor || this.Cesium.Color.fromCssColorString(#7CFC00).withAlpha(0.2); this.outlineColor options.fillColor || this.Cesium.Color.fromCssColorString(#7CFC00).withAlpha(1); this.pointColor options.pointColor || this.Cesium.Color.RED; // 使用自定义方法计算高饱和度颜色 const outlineColor this._getHighSaturationColor(this.outlineColor); // 创建高饱和度的虚线材质用于 Polyline 边框 this.outlineMaterial new this.Cesium.PolylineDashMaterialProperty({ color: outlineColor, dashLength: 16.0 }); // 内部状态 this.positions []; this.floatingPoint null; this.pointEntities []; this.polygonEntity null; this.outlinePolylineEntity null; // 专门用于显示边框的 Polyline Entity this.handler null; this.isDrawing false; this.isEditing false; // 拖拽状态变量 this.draggedPointIndex null; this.editHandler null; } /** * 将 Cesium.Color 转换为 HSL 空间提取色相并将饱和度设为 100% * private */ _getHighSaturationColor(cesiumColor) { const r cesiumColor.red; const g cesiumColor.green; const b cesiumColor.blue; const a cesiumColor.alpha; const max Math.max(r, g, b); const min Math.min(r, g, b); let h 0, s 0; const l (max min) / 2; if (max ! min) { const d max - min; s l 0.5 ? d / (2 - max - min) : d / (max min); switch (max) { case r: h ((g - b) / d (g b ? 6 : 0)) / 6; break; case g: h ((b - r) / d 2) / 6; break; case b: h ((r - g) / d 4) / 6; break; } } s 1.0; // 强制饱和度为 100% let r2, g2, b2; if (s 0) { r2 g2 b2 l; } else { const hue2rgb (p, q, t) { if (t 0) t 1; if (t 1) t - 1; if (t 1/6) return p (q - p) * 6 * t; if (t 1/2) return q; if (t 2/3) return p (q - p) * (2/3 - t) * 6; return p; }; const q l 0.5 ? l * (1 s) : l s - l * s; const p 2 * l - q; r2 hue2rgb(p, q, h 1/3); g2 hue2rgb(p, q, h); b2 hue2rgb(p, q, h - 1/3); } return new this.Cesium.Color(r2, g2, b2, a); } /** * 开始绘制 */ start() { if (this.isDrawing) return; this.positions []; this.pointEntities []; this.floatingPoint null; this.isDrawing true; this.isEditing false; this.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction( this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK ); // 1. 创建动态填充多边形 Entity this.polygonEntity this.viewer.entities.add({ polygon: { hierarchy: new this.Cesium.CallbackProperty(() { if (this.positions.length 2) return new this.Cesium.PolygonHierarchy([]); const pts this.floatingPoint ? this.positions.concat([this.floatingPoint]) : this.positions; return new this.Cesium.PolygonHierarchy(pts); }, false), material: this.fillColor, outline: false, // 关闭默认边框交由独立的 Polyline 处理 clampToGround: true } }); // 2. 创建动态边框 Polyline Entity this.outlinePolylineEntity this.viewer.entities.add({ polyline: { positions: new this.Cesium.CallbackProperty(() { if (this.positions.length 2) return []; const pts this.floatingPoint ? this.positions.concat([this.floatingPoint]) : this.positions; // 闭合多边形边框将第一个点追加到末尾 return [...pts, pts[0]]; }, false), width: 1, material: this.outlineMaterial, clampToGround: true } }); this.handler new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this._bindDrawEvents(); } /** * 绑定绘制阶段的交互事件 * private */ _bindDrawEvents() { this.handler.setInputAction((click) { const cartesian this.viewer.scene.pickPosition(click.position); if (!cartesian) return; this.positions.push(cartesian); const point this.viewer.entities.add({ position: cartesian, point: { pixelSize: 6, color: this.pointColor, outlineColor: this.Cesium.Color.WHITE, outlineWidth: 2, disableDepthTestDistance: Number.POSITIVE_INFINITY } }); this.pointEntities.push(point); }, this.Cesium.ScreenSpaceEventType.LEFT_CLICK); this.handler.setInputAction((movement) { if (this.positions.length 0) return; const p this.viewer.scene.pickPosition(movement.endPosition); if (p) this.floatingPoint p; }, this.Cesium.ScreenSpaceEventType.MOUSE_MOVE); const finish () this.finish(); this.handler.setInputAction(finish, this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK); this.handler.setInputAction(finish, this.Cesium.ScreenSpaceEventType.RIGHT_CLICK); } /** * 结束绘制并自动进入编辑模式 */ finish() { if (!this.isDrawing) return; this.floatingPoint null; this.isDrawing false; if (this.handler) { this.handler.destroy(); this.handler null; } console.log(✅ 多边形绘制完成最终顶点数量:, this.positions.length); this.onFinish(this.positions); this._startEditing(); } /** * 开启拖拽编辑模式 * private */ _startEditing() { if (this.isEditing || this.pointEntities.length 0) return; this.isEditing true; this.editHandler new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this.editHandler.setInputAction((click) { const picked this.viewer.scene.pick(click.position); if (this.Cesium.defined(picked) picked.id) { const index this.pointEntities.indexOf(picked.id); if (index ! -1) { this.draggedPointIndex index; this.viewer.scene.canvas.style.cursor move; this.viewer.scene.screenSpaceCameraController.enableInputs false; } } }, this.Cesium.ScreenSpaceEventType.LEFT_DOWN); this.editHandler.setInputAction((movement) { if (this.draggedPointIndex null) return; const ray this.viewer.camera.getPickRay(movement.endPosition); const cartesian this.viewer.scene.globe.pick(ray, this.viewer.scene); if (cartesian) { this.positions[this.draggedPointIndex] cartesian; this.pointEntities[this.draggedPointIndex].position new this.Cesium.CallbackProperty( () cartesian, false ); } }, this.Cesium.ScreenSpaceEventType.MOUSE_MOVE); this.editHandler.setInputAction(() { if (this.draggedPointIndex ! null) { this.draggedPointIndex null; this.viewer.scene.canvas.style.cursor ; this.viewer.scene.screenSpaceCameraController.enableInputs true; console.log(✏️ 多边形顶点编辑完成最新坐标:, this.positions); this.onEdit(this.positions); } }, this.Cesium.ScreenSpaceEventType.LEFT_UP); } /** * 清除当前绘制的所有实体 */ clear() { this.pointEntities.forEach((e) this.viewer.entities.remove(e)); this.pointEntities []; this.positions []; this.floatingPoint null; } /** * 完全销毁工具实例 */ destroy() { this.isEditing false; if (this.editHandler) { this.editHandler.destroy(); this.editHandler null; } this.viewer.scene.canvas.style.cursor ; this.viewer.scene.screenSpaceCameraController.enableInputs true; if (this.handler) { this.handler.destroy(); this.handler null; } this.clear(); if (this.polygonEntity) { this.viewer.entities.remove(this.polygonEntity); this.polygonEntity null; } // 销毁边框 Polyline Entity if (this.outlinePolylineEntity) { this.viewer.entities.remove(this.outlinePolylineEntity); this.outlinePolylineEntity null; } } } export default PolygonDrawer;const d new PolygonDrawer(Cesium, viewer); d.start();

相关新闻

WuWa-Mod终极指南:彻底解放鸣潮游戏潜能的15种神奇模组

WuWa-Mod终极指南:彻底解放鸣潮游戏潜能的15种神奇模组

WuWa-Mod终极指南:彻底解放鸣潮游戏潜能的15种神奇模组 【免费下载链接】wuwa-mod Wuthering Waves pak mods 项目地址: https://gitcode.com/GitHub_Trending/wu/wuwa-mod 还在为鸣潮游戏中的各种限制感到束手束脚吗?WuWa-Mod作为目前最受欢迎的…

2026/7/8 5:10:37 阅读更多 →
机械加工企业必看!ISO9001认证办理全流程拆解,一步到位不踩坑

机械加工企业必看!ISO9001认证办理全流程拆解,一步到位不踩坑

不少机械加工企业想办理ISO9001认证,却因不熟悉流程,要么盲目准备导致反复返工,要么被不专业机构误导走弯路。其实,ISO9001认证办理有清晰的标准化流程,只要把握关键节点,就能高效推进。整个流程分为三个核…

2026/7/8 5:10:37 阅读更多 →
计算机毕业设计之基于vue的零食销售系统

计算机毕业设计之基于vue的零食销售系统

随着互联网技术与电商行业的深度融合,传统零食销售模式面临渠道单一、运营效率低下等挑战,数字化转型成为行业发展的必然趋势。本研究针对零食销售场景的痛点,设计并实现了一套基于B/S架构的现代化电商平台,采用Java语言与Spring …

2026/7/8 5:10:37 阅读更多 →

最新新闻

Openspec + Superpowers:契约即代码的API开发新范式

Openspec + Superpowers:契约即代码的API开发新范式

1. 项目概述:这不是又一个“AI写代码”噱头,而是开发者工作流的底层重构 我第一次在终端里敲下 openspec init 并看到它自动生成了带完整 OpenAPI 3.1 规范、类型安全客户端、Mock 服务和 Postman 集合的整个工程骨架时,手是停住的。不是因…

2026/7/8 6:00:48 阅读更多 →
Go 1.26 新特性回顾:语言增强、工具升级与 Green Tea GC 默认启用

Go 1.26 新特性回顾:语言增强、工具升级与 Green Tea GC 默认启用

作者:陈明勇,一名热爱技术、乐于分享的开发者,同时也是开源爱好者。 我专注于分享 Go 语言相关的技术知识,同时也会深入探讨 AI 领域的前沿技术。 成功的路上并不拥挤,有没有兴趣结个伴? Go 开源库代表作&a…

2026/7/8 5:58:47 阅读更多 →
2026年哪家商标异议代办翻盘率更高?圈内曝光5个筛选关键细节

2026年哪家商标异议代办翻盘率更高?圈内曝光5个筛选关键细节

在日常商业经营中,不少商家都遭遇过商标异议的困扰。辛苦经营的品牌,突然面临被异议的风险,究竟该找哪家代办机构来提高翻盘率呢?这是很多商家心中的疑问。下面就为大家介绍筛选商标异议代办机构的关键细节。专业能力与经验专业能…

2026/7/8 5:56:47 阅读更多 →
D2DX现代化补丁:暗黑破坏神2终极优化指南,解锁高帧率与宽屏体验

D2DX现代化补丁:暗黑破坏神2终极优化指南,解锁高帧率与宽屏体验

D2DX现代化补丁:暗黑破坏神2终极优化指南,解锁高帧率与宽屏体验 【免费下载链接】d2dx D2DX is a complete solution to make Diablo II run well on modern PCs, with high fps and better resolutions. 项目地址: https://gitcode.com/gh_mirrors/d2…

2026/7/8 5:54:46 阅读更多 →
2026年直线滑台选购指南:这三家厂商口碑公认最好

2026年直线滑台选购指南:这三家厂商口碑公认最好

你有没有遇到过这样的场景——设备调试阶段一切正常,一上产线连续跑三天,定位偏差从0.01mm漂到了0.05mm,良率直接跳水两个点?这不是设备不行,是直线滑台选错了。直线滑台是自动化设备中最基础的直线运动执行部件&#…

2026/7/8 5:52:46 阅读更多 →
2026 指挥中心控制台选型排名:4 家主流源头厂商客观横向对比(政企控制室操作台采购指南)

2026 指挥中心控制台选型排名:4 家主流源头厂商客观横向对比(政企控制室操作台采购指南)

2026 政企指挥中心控制台选型客观测评,对比科思诺、铁力山、飞马拓新、照彰实业生产产能、交付周期、场景适配能力,提供控制室操作台采购标准、避坑要点与场景推荐,适配公安、能源、金融、水利调度项目。综合自有全流程产线、南北直营服务、全…

2026/7/8 5:50:46 阅读更多 →

日新闻

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手,全日常一键长草!| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: …

2026/7/8 0:00:48 阅读更多 →
MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N1 到批处理的全路径 一、从"功能正确"到"性能可接受"——MyBatis 批量操作的三段式进化 MyBatis 在日常增删改查场景中几乎是无感的——实体映射直观、SQL 控制灵活。但当数据量从千级上升到十万级、百万级,许…

2026/7/8 0:00:48 阅读更多 →
工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

1. 工业负载控制方案概述在工业自动化、电机驱动和照明控制等高需求场景中,可靠地控制电感和电阻负载是核心挑战之一。TPD2015FN作为东芝的8通道高端智能功率开关IC,配合PIC18F45K22微控制器,能够构建一套稳定、高效的负载控制系统。这套组合…

2026/7/8 0:02:48 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/7 14:24:45 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/7 12:34:47 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/7 15:59:06 阅读更多 →

月新闻