cesium 后处理3(发光区域跟随鼠标移动)

124 篇文章 ¥129.90 ¥299.90
### 实现 Cesium 中线条跟随鼠标交互 为了实现Cesium 中绘制一条可以随着鼠标移动而更新的线段,通常会采用监听鼠标事件的方式并实时更新线段的位置。下面是一个具体的实现方法[^4]。 #### 创建基础环境 确保 HTML 页面中有用于承载 Cesium Viewer 的容器,并设置其样式使该容器能够占据整个浏览器窗口[^2]: ```css html, body, #cesiumContainer { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; } ``` #### 初始化 Cesium 场景 初始化 `Viewer` 对象作为 Cesium 应用程序的主要入口点: ```javascript const viewer = new Cesium.Viewer('cesiumContainer', { terrainProvider: Cesium.createWorldTerrain() }); ``` #### 定义绘图工具类 创建一个名为 `DrawTool` 的 JavaScript 类来管理所有的绘图逻辑,包括但不限于添加新顶点、移除最后一个顶点以及清除所有已有的路径数据等操作: ```javascript class DrawTool { constructor(viewer) { this.viewer = viewer; this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas); this.positions = []; this.polyline = undefined; // 添加鼠标点击事件处理函数 this._addClickHandler(); // 添加鼠标移动事件处理函数 this._addMouseMoveHandler(); } _addClickHandler() { const that = this; this.handler.setInputAction(function (movement) { let cartesian = that.viewer.camera.pickEllipsoid(movement.position, that.viewer.scene.globe.ellipsoid); if (cartesian !== undefined && !that.isDrawing()) { that.addPoint(cartesian); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } _addMouseMoveHandler(){ const that = this; this.handler.setInputAction(function (movement){ if(that.isDrawing()){ let lastPosition = that.positions[that.positions.length - 1]; let currentCartesian = that.viewer.camera.pickEllipsoid(movement.endPosition, that.viewer.scene.globe.ellipsoid); if(currentCartesian){ that.updateLastSegment(lastPosition,currentCartesian); } } },Cesium.ScreenSpaceEventType.MOUSE_MOVE); } addPoint(position) { this.positions.push(Cesium.Cartesian3.clone(position)); if (!this.polyline || this.polyline.isDestroyed()) { this.polyline = this.viewer.entities.add({ polyline: { positions: new Cesium.CallbackProperty(() => {return this.positions;}, false), material: new Cesium.PolylineGlowMaterialProperty({color : Cesium.Color.RED}), width: 5, clampToGround: true } }); } else { this.polyline.polyline.material.color = new Cesium.CallbackProperty(()=>{return Cesium.Color.BLUE;},false); } } updateLastSegment(start,end){ this.positions[this.positions.length-1]=end; } isDrawing(){ return this.positions.length>0; } clearAllPoints() { while (this.positions.length > 0) { this.removeLastPoint(); } } removeLastPoint() { if (this.positions.length === 0) return; this.positions.pop(); if (this.positions.length === 0) { this.destroyPolyline(); } } destroyPolyline() { if (this.polyline && !this.polyline.isDestroyed()) { this.polyline.destroy(); this.polyline = null; } } } // 使用实例化后的对象开始绘图 let drawToolInstance = new DrawTool(viewer); ``` 此代码片段展示了如何利用 Cesium 提供的各种 API 来构建一个简单的绘图工具,允许用户通过单击地图上的不同位置来增加新的节点,并且当处于绘画状态时(即已经有一个以上的点被加入),可以通过拖动鼠标来自由调整最后一条线段的方向和长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GIS-CL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值