Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/maptalks/src/geometry/editor/GeometryEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ function coordinatesToContainerPoint(map, coordinate) {
}


export function fixHandlePointCoordinates(geometry: Geometry, vertex: Coordinate, dragContainerPoint: Point) {
export function fixHandlePointCoordinates(geometry: Geometry, vertex: Coordinate, dragContainerPoint: Point, dragOnAxis?: string) {
const map = geometry.getMap();
if (!vertex || vertex.z === 0) {
return map.containerPointToCoord(dragContainerPoint)
const c = map.containerPointToCoord(dragContainerPoint);
return calCoordOffsetByDragOnAxis(c, vertex, dragOnAxis);
}
const altitude = vertex.z;
const glRes = map.getGLRes();
Expand All @@ -76,7 +77,7 @@ export function fixHandlePointCoordinates(geometry: Geometry, vertex: Coordinate
if (isPoint) {
coordinates.z = altitude;
}
return coordinates;
return calCoordOffsetByDragOnAxis(coordinates, vertex, dragOnAxis);


}
Expand Down Expand Up @@ -143,6 +144,32 @@ function onHandleDragEnd(ev: Record<string, any>): boolean {
return false;
}

function calCoordOffsetByDragOnAxis(currentCoord: Coordinate, preCoord?: Coordinate, dragOnAxis?: string): Coordinate {
if (!dragOnAxis) {
return currentCoord;
}
//currentCoord is offset
if (!preCoord && currentCoord) {
if (dragOnAxis === 'x') {
currentCoord.y = 0;
}
if (dragOnAxis === 'y') {
currentCoord.x = 0;
}
return currentCoord;
}
//need cal offset by currentCoordinates and preCoordinates
const offset = currentCoord.sub(preCoord);
if (dragOnAxis === 'x') {
preCoord.x += offset.x;
}
if (dragOnAxis === 'y') {
preCoord.y += offset.y;
}
return preCoord.copy();

}

const options: GeometryEditOptionsType = {
//fix outline's aspect ratio when resizing
'fixAspectRatio': false,
Expand Down Expand Up @@ -180,7 +207,9 @@ class GeometryEditor extends Eventable(Class) {
//@internal
_shadow?: Geometry;
//@internal
_geometryDraggble: boolean
_geometryDraggble: boolean;
//@internal
_geometryDragOnAxis: string;
//@internal
_history: any
//@internal
Expand Down Expand Up @@ -274,6 +303,8 @@ class GeometryEditor extends Eventable(Class) {
const layer = this._geometry.getLayer();
const needShadow = layer.options['renderer'] === 'canvas';
this._geometryDraggble = geometry.options['draggable'];
this._geometryDragOnAxis = geometry.options['dragOnAxis'];
const dragOnAxis = this.options.dragOnAxis;
if (needShadow) {
geometry.config('draggable', false);
//edits are applied to a shadow of geometry to improve performance.
Expand All @@ -292,10 +323,12 @@ class GeometryEditor extends Eventable(Class) {
});

this._shadow = shadow;
shadow.config({ dragOnAxis });

geometry.hide();
} else if (geometry instanceof Marker) {
geometry.config('draggable', true);
geometry.config({ dragOnAxis });
}

this._switchGeometryEvents('on');
Expand Down Expand Up @@ -348,6 +381,7 @@ class GeometryEditor extends Eventable(Class) {
}
map.off('drawtopstart', this._refresh, this);
this._geometry.config('draggable', this._geometryDraggble);
this._geometry.config('dragOnAxis', this._geometryDragOnAxis);
if (this._shadow) {
this._shadow.off(SHADOW_DRAG_EVENTS, this._shadowDragEvent, this);
delete this._shadow;
Expand Down Expand Up @@ -493,6 +527,7 @@ class GeometryEditor extends Eventable(Class) {
},
onMove: (param): void => {
const offset = param['coordOffset'];
calCoordOffsetByDragOnAxis(offset, null, this.options.dragOnAxis);
if (shadow) {
shadow.translate(offset);
} else {
Expand All @@ -504,6 +539,7 @@ class GeometryEditor extends Eventable(Class) {
const shadowFirst = shadow.getFirstCoordinate();
const first = this._geometry.getFirstCoordinate();
const offset = shadowFirst.sub(first);
calCoordOffsetByDragOnAxis(offset, null, this.options.dragOnAxis);
this._update('translate', offset);
shadow.remove();
}
Expand Down Expand Up @@ -1187,7 +1223,7 @@ class GeometryEditor extends Eventable(Class) {

const coordinates = getVertexCoordinates(ringIndex);
const containerPoint = handleConatainerPoint.sub(getDxDy());
const coordinate = fixHandlePointCoordinates(me._geometry, coordinates[index], containerPoint);
const coordinate = fixHandlePointCoordinates(me._geometry, coordinates[index], containerPoint, me.options.dragOnAxis);
const vertex = coordinates[index];
vertex.x = coordinate.x;
vertex.y = coordinate.y;
Expand Down
4 changes: 4 additions & 0 deletions packages/maptalks/src/geometry/ext/Geometry.Drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,12 @@ class GeometryDragHandler extends Handler {
if (!dragOnScreenAxis) {
if (axis === 'x') {
pointOffset.y = coordOffset.y = 0;
//https://github.com/maptalks/maptalks.js/issues/2817
coord.y = this._lastCoord.y;
} else if (axis === 'y') {
pointOffset.x = coordOffset.x = 0;
//https://github.com/maptalks/maptalks.js/issues/2817
coord.x = this._lastCoord.x;
}
}
this._lastPoint = point;
Expand Down
1 change: 1 addition & 0 deletions packages/maptalks/src/geometry/ext/Geometry.Edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type GeometryEditOptionsType = {
vertexZIndex?: number;
newVertexZIndex?: number;
shadowDraggable?: boolean;
dragOnAxis?: 'x' | 'y'
}

declare module "../Geometry" {
Expand Down