Skip to content

Commit 9a59aea

Browse files
committed
feat: 框选删除
1 parent a4e0b1f commit 9a59aea

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- 3dtiles 裁剪
4646
- 屏幕色调调整
4747
- 清除星空/默认图层
48+
- 框选删除
4849

4950
## 交流
5051
qq群 334522247

src/pages/demos/boxSelect.tsx

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { Modal } from "antd";
2+
import React from "react";
3+
import { CesiumMap, Debug } from "../../lib";
4+
5+
export default class BoxSelect extends React.Component<{}, { visible: boolean }> {
6+
line: Cesium.Entity;
7+
points: { point: Cesium.Cartesian3, ins: Cesium.Entity }[] = [];
8+
screenStartPoint = new Cesium.Cartesian2();
9+
screenEndPoint = new Cesium.Cartesian2();
10+
beActive: boolean;
11+
viewer: Cesium.Viewer;
12+
selectDiv: HTMLDivElement;
13+
14+
constructor(props) {
15+
super(props);
16+
this.state = {
17+
visible: false,
18+
}
19+
}
20+
21+
render() {
22+
return <React.Fragment>
23+
<CesiumMap id={this.constructor.name} onViewerLoaded={this.handleViewerLoaded.bind(this)} />
24+
<Modal
25+
visible={this.state.visible}
26+
title="确认删除"
27+
onOk={() => this.deletePoints()}
28+
onCancel={() => this.cancel()}
29+
/>
30+
</React.Fragment>
31+
}
32+
33+
34+
private deletePoints() {
35+
this.points = this.points.filter(item => {
36+
let screenPos = Cesium.SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, item.point);
37+
if (screenPos == null || screenPos.x < this.screenStartPoint.x || screenPos.x > this.screenEndPoint.x || screenPos.y < this.screenStartPoint.y || screenPos.y > this.screenEndPoint.y) {
38+
return true;
39+
} else {
40+
this.viewer.entities.remove(item.ins);
41+
return false
42+
}
43+
})
44+
this.beActive = false;
45+
this.viewer.scene.screenSpaceCameraController.enableZoom = true;
46+
this.viewer.scene.screenSpaceCameraController.enableTranslate = true;
47+
48+
this.setState({ visible: false });
49+
this.selectDiv.style.width = `0px`;
50+
this.selectDiv.style.height = `0px`;
51+
}
52+
53+
private cancel() {
54+
this.beActive = false;
55+
this.viewer.scene.screenSpaceCameraController.enableZoom = true;
56+
this.viewer.scene.screenSpaceCameraController.enableTranslate = true;
57+
58+
59+
this.setState({ visible: false });
60+
this.selectDiv.style.width = `0px`;
61+
this.selectDiv.style.height = `0px`;
62+
}
63+
64+
65+
private init(viewer: Cesium.Viewer) {
66+
//准备线资源
67+
let startPoint = Cesium.Cartesian3.fromDegrees(-75.59689628236974, 40.04021362709179, 110.662327232677132);
68+
let endPoint = Cesium.Cartesian3.fromDegrees(-75.59754639282833, 40.03759666603451, 2.0127028956620197);
69+
70+
let points: Cesium.Cartesian3[] = [];
71+
for (let i = 0; i < 1000; i++) {
72+
let point = Cesium.Cartesian3.lerp(startPoint, endPoint, i / 1000, new Cesium.Cartesian3());
73+
points.push(point);
74+
// let ins = null;
75+
let ins = viewer.entities.add({
76+
position: point,
77+
point: {
78+
pixelSize: 5,
79+
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
80+
disableDepthTestDistance: Number.POSITIVE_INFINITY,
81+
}
82+
});
83+
84+
this.points.push({ point, ins });
85+
}
86+
87+
document.oncontextmenu = function () { return false; }
88+
let selectDiv = document.createElement("div");
89+
selectDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;";
90+
document.body.appendChild(selectDiv);
91+
this.selectDiv = selectDiv;
92+
93+
let handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
94+
this.beActive = false;
95+
96+
handler.setInputAction((event) => {
97+
this.screenStartPoint.x = event.position.x;
98+
this.screenStartPoint.y = event.position.y;
99+
selectDiv.style.left = event.position.x + "px";
100+
selectDiv.style.top = event.position.y + "px";
101+
102+
this.beActive = true;
103+
viewer.scene.screenSpaceCameraController.enableZoom = false;
104+
105+
}, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
106+
//鼠标抬起事件,获取div坐上和右下的x,y 转为经纬度坐标
107+
handler.setInputAction((event) => {
108+
this.screenEndPoint.x = event.position.x;
109+
this.screenEndPoint.y = event.position.y;
110+
111+
this.setState({ visible: true })
112+
113+
}, Cesium.ScreenSpaceEventType.RIGHT_UP);
114+
115+
handler.setInputAction((ev) => {
116+
if (this.beActive) {
117+
selectDiv.style.width = ev.endPosition.x - this.screenStartPoint.x + "px";
118+
selectDiv.style.height = ev.endPosition.y - this.screenStartPoint.y + "px";
119+
}
120+
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
121+
}
122+
123+
private handleViewerLoaded(viewer: Cesium.Viewer) {
124+
this.viewer = viewer;
125+
let modelPath = Cesium.IonResource.fromAssetId(17732);
126+
let tileset = viewer.scene.primitives.add(
127+
new Cesium.Cesium3DTileset({
128+
url: modelPath,
129+
maximumScreenSpaceError: 0.8,
130+
maximumNumberOfLoadedTiles: 100,
131+
})
132+
) as Cesium.Cesium3DTileset;
133+
134+
viewer.zoomTo(tileset);
135+
this.init(viewer);
136+
}
137+
}

src/router/routes.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ export const demosInfo = [
203203
path: "/cleatBg",
204204
asyncComponent: () => import("../pages/demos/clearSky")
205205
},
206+
{
207+
title: "框选删除",
208+
path: "/boxSelectToDelete",
209+
asyncComponent: () => import("../pages/demos/boxSelect")
210+
}
206211
// {
207212
// title: "测试",
208213
// path: "/testDemo",

0 commit comments

Comments
 (0)