diff --git a/examples/index.ts b/examples/index.ts index bcc0dc1..6d0b3c0 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -66,6 +66,9 @@ const buttonGroup = document.getElementById('button-group') as HTMLElement; buttonGroup.onclick = (evt) => { const targetElement = evt.target as HTMLElement; switch (targetElement.id) { + case 'drawPolygon': + geometry = new CesiumPlot.Polygon(Cesium, viewer); + break; case 'drawReactangle': geometry = new CesiumPlot.Reactangle(Cesium, viewer); break; diff --git a/index.html b/index.html index 661f93a..f46d67d 100644 --- a/index.html +++ b/index.html @@ -45,6 +45,7 @@
+ diff --git a/src/index.ts b/src/index.ts index eb90edc..212781d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import Ellipse from './polygon/ellipse'; import Lune from './polygon/lune'; import Reactangle from './polygon/rectangle'; import Triangle from './polygon/triangle'; +import Polygon from './polygon/polygon'; const CesiumPlot = { FineArrow, @@ -31,7 +32,8 @@ const CesiumPlot = { Ellipse, Lune, Reactangle, - Triangle + Triangle, + Polygon, }; export default CesiumPlot; diff --git a/src/polygon/polygon.ts b/src/polygon/polygon.ts new file mode 100644 index 0000000..1a9dd54 --- /dev/null +++ b/src/polygon/polygon.ts @@ -0,0 +1,56 @@ +import Base from '../base'; +// @ts-ignore +import { Cartesian3 } from '@examples/cesium'; + +import { PolygonStyle } from '../interface'; + +export default class Polygon extends Base { + points: Cartesian3[] = []; + + constructor(cesium: any, viewer: any, style?: PolygonStyle) { + super(cesium, viewer, style); + this.cesium = cesium; + this.setState('drawing'); + this.onDoubleClick(); + } + + getType(): 'polygon' | 'line' { + return 'polygon'; + } + + /** + * Add points only on click events + */ + addPoint(cartesian: Cartesian3) { + this.points.push(cartesian); + if (this.points.length === 1) { + this.onMouseMove(); + } + } + + /** + * Draw a shape based on mouse movement points during the initial drawing. + */ + updateMovingPoint(cartesian: Cartesian3) { + const tempPoints = [...this.points, cartesian]; + this.setGeometryPoints(tempPoints); + if (tempPoints.length === 2) { + this.addFirstLineOfTheArrow(); + } else { + this.drawPolygon(); + } + } + + /** + * In edit mode, drag key points to update corresponding key point data. + */ + updateDraggingPoint(cartesian: Cartesian3, index: number) { + this.points[index] = cartesian; + this.setGeometryPoints(this.points); + this.drawPolygon(); + } + + getPoints() { + return this.points; + } +}