mirror of
https://github.com/ethan-zf/cesium-plot-js.git
synced 2025-06-24 03:27:29 +00:00
Unbind 'mousemove' event after drawing is complete, and enhance TypeScript type validation
This commit is contained in:
parent
aa7b0698f0
commit
ed5c21dacb
@ -5,7 +5,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc && vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
|
"lint": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
|
@ -1,36 +1,48 @@
|
|||||||
import Draw from '../draw';
|
import Draw from '../draw';
|
||||||
import * as Utils from '../utils';
|
import * as Utils from '../utils';
|
||||||
|
import { Cartesian3 } from '../../examples/cesium';
|
||||||
|
|
||||||
export default class FineArrow extends Draw {
|
export default class FineArrow extends Draw {
|
||||||
points: any = [];
|
points: Cartesian3[] = [];
|
||||||
constructor(cesium, viewer, style) {
|
|
||||||
|
constructor(cesium: any, viewer: any, style: any) {
|
||||||
super(cesium, viewer);
|
super(cesium, viewer);
|
||||||
this.cesium = cesium;
|
this.cesium = cesium;
|
||||||
// this.setPoints = this.setPoints.bind(this);
|
|
||||||
this.onClick();
|
this.onClick();
|
||||||
}
|
}
|
||||||
|
|
||||||
addPoint(cartesian) {
|
/**
|
||||||
|
* Add points only on click events
|
||||||
|
*/
|
||||||
|
addPoint(cartesian: Cartesian3) {
|
||||||
this.points.push(cartesian);
|
this.points.push(cartesian);
|
||||||
if (this.points.length == 2) {
|
if (this.points.length === 1) {
|
||||||
|
this.onMouseMove();
|
||||||
|
}
|
||||||
|
if (this.points.length === 2) {
|
||||||
const geometryPoints = this.createPolygon(this.points);
|
const geometryPoints = this.createPolygon(this.points);
|
||||||
this.setGeometryPoints(geometryPoints);
|
this.setGeometryPoints(geometryPoints);
|
||||||
this.addToMap();
|
this.addToMap();
|
||||||
this.removeEventListener();
|
this.removeClickListener();
|
||||||
|
this.removeMoveListener();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateMovingPoint(cartesian) {
|
/**
|
||||||
let tempPoints = [].concat(this.points);
|
* Update the last point as the mouse moves.
|
||||||
tempPoints = tempPoints.concat([cartesian]);
|
*/
|
||||||
|
updateMovingPoint(cartesian: Cartesian3) {
|
||||||
|
let tempPoints = [...this.points, cartesian];
|
||||||
const geometryPoints = this.createPolygon(tempPoints);
|
const geometryPoints = this.createPolygon(tempPoints);
|
||||||
this.setGeometryPoints(geometryPoints);
|
this.setGeometryPoints(geometryPoints);
|
||||||
this.addToMap();
|
this.addToMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
createPolygon(positions) {
|
/**
|
||||||
const p1 = this.cartesianToLnglat(positions[0]);
|
* Generate geometric shapes based on key points.
|
||||||
const p2 = this.cartesianToLnglat(positions[1]);
|
*/
|
||||||
|
createPolygon(positions: Cartesian3[]) {
|
||||||
|
const [p1, p2] = positions.map(this.cartesianToLnglat);
|
||||||
const len = Utils.getBaseLength([p1, p2]);
|
const len = Utils.getBaseLength([p1, p2]);
|
||||||
const tailWidth = len * this.tailWidthFactor;
|
const tailWidth = len * this.tailWidthFactor;
|
||||||
const neckWidth = len * this.neckWidthFactor;
|
const neckWidth = len * this.neckWidthFactor;
|
||||||
|
47
src/draw.ts
47
src/draw.ts
@ -1,8 +1,9 @@
|
|||||||
import * as Utils from './utils';
|
import * as Utils from './utils';
|
||||||
|
import * as CesiumTypeOnly from '../examples/cesium';
|
||||||
|
|
||||||
export default class Draw {
|
export default class Draw {
|
||||||
cesium: any;
|
cesium: typeof CesiumTypeOnly;
|
||||||
viewer: any;
|
viewer: CesiumTypeOnly.Viewer;
|
||||||
arrowLengthScale: number = 5;
|
arrowLengthScale: number = 5;
|
||||||
maxArrowLength: number = 2;
|
maxArrowLength: number = 2;
|
||||||
tailWidthFactor: number;
|
tailWidthFactor: number;
|
||||||
@ -10,13 +11,11 @@ export default class Draw {
|
|||||||
headWidthFactor: number;
|
headWidthFactor: number;
|
||||||
headAngle: number;
|
headAngle: number;
|
||||||
neckAngle: number;
|
neckAngle: number;
|
||||||
eventHandler: any;
|
eventHandler: CesiumTypeOnly.ScreenSpaceEventHandler;
|
||||||
clickListener: any;
|
entity: CesiumTypeOnly.Entity;
|
||||||
entity: any;
|
geometryPoints: CesiumTypeOnly.Cartesian3[] | undefined;
|
||||||
renderingPoints: any;
|
|
||||||
geometryPoints: any;
|
|
||||||
|
|
||||||
constructor(cesium, viewer) {
|
constructor(cesium: typeof CesiumTypeOnly, viewer: CesiumTypeOnly.Viewer) {
|
||||||
this.cesium = cesium;
|
this.cesium = cesium;
|
||||||
this.viewer = viewer;
|
this.viewer = viewer;
|
||||||
this.tailWidthFactor = 0.1;
|
this.tailWidthFactor = 0.1;
|
||||||
@ -24,37 +23,41 @@ export default class Draw {
|
|||||||
this.headWidthFactor = 0.25;
|
this.headWidthFactor = 0.25;
|
||||||
this.headAngle = Math.PI / 8.5;
|
this.headAngle = Math.PI / 8.5;
|
||||||
this.neckAngle = Math.PI / 13;
|
this.neckAngle = Math.PI / 13;
|
||||||
|
this.cartesianToLnglat = this.cartesianToLnglat.bind(this);
|
||||||
|
this.pixelToCartesian = this.pixelToCartesian.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
onClick() {
|
onClick() {
|
||||||
// 添加点击事件监听器
|
|
||||||
this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
|
this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
|
||||||
this.clickListener = this.eventHandler.setInputAction((evt) => {
|
this.eventHandler.setInputAction((evt) => {
|
||||||
const cartesian = this.pixelToCartesian(evt.position);
|
const cartesian = this.pixelToCartesian(evt.position);
|
||||||
this.addPoint(cartesian);
|
this.addPoint(cartesian);
|
||||||
this.onMouseMove();
|
|
||||||
}, this.cesium.ScreenSpaceEventType.LEFT_CLICK);
|
}, this.cesium.ScreenSpaceEventType.LEFT_CLICK);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeEventListener() {
|
|
||||||
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_CLICK, this.clickListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
onMouseMove() {
|
onMouseMove() {
|
||||||
this.eventHandler.setInputAction((evt) => {
|
this.eventHandler.setInputAction((evt) => {
|
||||||
const cartesian = this.pixelToCartesian(evt.endPosition);
|
const cartesian = this.pixelToCartesian(evt.endPosition);
|
||||||
const lnglat = this.cartesianToLnglat(cartesian);
|
const lnglat = this.cartesianToLnglat(cartesian);
|
||||||
const lastPoint = this.cartesianToLnglat(this.points[this.points.length - 1]);
|
const lastPoint = this.cartesianToLnglat(this.points[this.points.length - 1]);
|
||||||
const distance = Utils.MathDistance(lnglat, lastPoint);
|
const distance = Utils.MathDistance(lnglat, lastPoint);
|
||||||
|
|
||||||
if (distance < 0.0001) {
|
if (distance < 0.0001) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Synchronize data to subclasses.
|
||||||
this.updateMovingPoint(cartesian);
|
this.updateMovingPoint(cartesian);
|
||||||
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
setGeometryPoints(geometryPoints) {
|
removeClickListener() {
|
||||||
|
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_CLICK);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeMoveListener() {
|
||||||
|
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
setGeometryPoints(geometryPoints: CesiumTypeOnly.Cartesian3[]) {
|
||||||
this.geometryPoints = geometryPoints;
|
this.geometryPoints = geometryPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,14 +77,14 @@ export default class Draw {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cartesianToLnglat(cartesian) {
|
cartesianToLnglat(cartesian: CesiumTypeOnly.Cartesian3): number[] {
|
||||||
var lnglat = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
|
const lnglat = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
|
||||||
var lat = this.cesium.Math.toDegrees(lnglat.latitude);
|
const lat = this.cesium.Math.toDegrees(lnglat.latitude);
|
||||||
var lng = this.cesium.Math.toDegrees(lnglat.longitude);
|
const lng = this.cesium.Math.toDegrees(lnglat.longitude);
|
||||||
return [lng, lat];
|
return [lng, lat];
|
||||||
}
|
}
|
||||||
|
|
||||||
pixelToCartesian(position) {
|
pixelToCartesian(position: CesiumTypeOnly.Cartesian2): CesiumTypeOnly.Cartesian3 | undefined {
|
||||||
const ray = this.viewer.camera.getPickRay(position);
|
const ray = this.viewer.camera.getPickRay(position);
|
||||||
const cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
|
const cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
|
||||||
return cartesian;
|
return cartesian;
|
||||||
|
Loading…
Reference in New Issue
Block a user