Merge pull request #11698 from ahocevar/draw-pointer

Draw pointer improvements
This commit is contained in:
Andreas Hocevar
2020-11-02 18:40:12 +01:00
committed by GitHub
3 changed files with 59 additions and 31 deletions

View File

@@ -6,17 +6,18 @@ docs: >
Example of using the Draw interaction. Select a geometry type from the Example of using the Draw interaction. Select a geometry type from the
dropdown above to start drawing. To finish drawing, click the last dropdown above to start drawing. To finish drawing, click the last
point. To activate freehand drawing for lines, polygons, and circles, hold point. To activate freehand drawing for lines, polygons, and circles, hold
the `Shift` key. the `Shift` key. To remove the last point of a line or polygon, press "Undo".
tags: "draw, edit, freehand, vector" tags: "draw, edit, freehand, vector"
--- ---
<div id="map" class="map"></div> <div id="map" class="map"></div>
<form class="form-inline"> <form class="form-inline">
<label for="type">Geometry type &nbsp;</label> <label for="type">Geometry type: &nbsp;</label>
<select id="type"> <select class="form-control mr-2 mb-2 mt-2" id="type">
<option value="Point">Point</option> <option value="Point">Point</option>
<option value="LineString">LineString</option> <option value="LineString">LineString</option>
<option value="Polygon">Polygon</option> <option value="Polygon">Polygon</option>
<option value="Circle">Circle</option> <option value="Circle">Circle</option>
<option value="None">None</option> <option value="None">None</option>
</select> </select>
<input class="form-control mr-2 mb-2 mt-2" type="button" value="Undo" id="undo">
</form> </form>

View File

@@ -45,4 +45,8 @@ typeSelect.onchange = function () {
addInteraction(); addInteraction();
}; };
document.getElementById('undo').addEventListener('click', function () {
draw.removeLastPoint();
});
addInteraction(); addInteraction();

View File

@@ -215,6 +215,13 @@ class Draw extends PointerInteraction {
*/ */
this.lastDragTime_; this.lastDragTime_;
/**
* Pointer type of the last pointermove event
* @type {string}
* @private
*/
this.pointerType_;
/** /**
* @type {boolean} * @type {boolean}
* @private * @private
@@ -535,7 +542,7 @@ class Draw extends PointerInteraction {
event.type === MapBrowserEventType.POINTERDOWN event.type === MapBrowserEventType.POINTERDOWN
) { ) {
pass = false; pass = false;
} else if (move) { } else if (move && this.getPointerCount() < 2) {
pass = event.type === MapBrowserEventType.POINTERMOVE; pass = event.type === MapBrowserEventType.POINTERMOVE;
if (pass && this.freehand_) { if (pass && this.freehand_) {
this.handlePointerMove_(event); this.handlePointerMove_(event);
@@ -603,34 +610,37 @@ class Draw extends PointerInteraction {
handleUpEvent(event) { handleUpEvent(event) {
let pass = true; let pass = true;
if (this.downTimeout_) { if (this.getPointerCount() === 0) {
clearTimeout(this.downTimeout_); if (this.downTimeout_) {
this.downTimeout_ = undefined; clearTimeout(this.downTimeout_);
} this.downTimeout_ = undefined;
}
this.handlePointerMove_(event);
this.handlePointerMove_(event);
const circleMode = this.mode_ === Mode.CIRCLE;
const circleMode = this.mode_ === Mode.CIRCLE;
if (this.shouldHandle_) {
if (!this.finishCoordinate_) { if (this.shouldHandle_) {
this.startDrawing_(event); if (!this.finishCoordinate_) {
if (this.mode_ === Mode.POINT) { this.startDrawing_(event);
this.finishDrawing(); if (this.mode_ === Mode.POINT) {
} this.finishDrawing();
} else if (this.freehand_ || circleMode) { }
this.finishDrawing(); } else if (this.freehand_ || circleMode) {
} else if (this.atFinish_(event)) { this.finishDrawing();
if (this.finishCondition_(event)) { } else if (this.atFinish_(event)) {
this.finishDrawing(); if (this.finishCondition_(event)) {
} this.finishDrawing();
} else { }
this.addToDrawing_(event.coordinate); } else {
this.addToDrawing_(event.coordinate);
}
pass = false;
} else if (this.freehand_) {
this.abortDrawing();
} }
pass = false;
} else if (this.freehand_) {
this.abortDrawing();
} }
if (!pass && this.stopClick_) { if (!pass && this.stopClick_) {
event.stopPropagation(); event.stopPropagation();
} }
@@ -643,6 +653,7 @@ class Draw extends PointerInteraction {
* @private * @private
*/ */
handlePointerMove_(event) { handlePointerMove_(event) {
this.pointerType_ = event.originalEvent.pointerType;
if ( if (
this.downPx_ && this.downPx_ &&
((!this.freehand_ && this.shouldHandle_) || ((!this.freehand_ && this.shouldHandle_) ||
@@ -884,14 +895,26 @@ class Draw extends PointerInteraction {
if (this.mode_ === Mode.LINE_STRING) { if (this.mode_ === Mode.LINE_STRING) {
coordinates = /** @type {LineCoordType} */ (this.sketchCoords_); coordinates = /** @type {LineCoordType} */ (this.sketchCoords_);
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
this.geometryFunction_(coordinates, geometry, projection);
if (coordinates.length >= 2) { if (coordinates.length >= 2) {
this.finishCoordinate_ = coordinates[coordinates.length - 2].slice(); this.finishCoordinate_ = coordinates[coordinates.length - 2].slice();
if (this.pointerType_ !== 'mouse') {
const finishCoordinate = this.finishCoordinate_.slice();
coordinates.pop();
coordinates.push(finishCoordinate);
this.sketchPoint_.setGeometry(new Point(finishCoordinate));
}
} }
this.geometryFunction_(coordinates, geometry, projection);
} else if (this.mode_ === Mode.POLYGON) { } else if (this.mode_ === Mode.POLYGON) {
coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0]; coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0];
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
sketchLineGeom = this.sketchLine_.getGeometry(); sketchLineGeom = this.sketchLine_.getGeometry();
if (this.pointerType_ !== 'mouse') {
const finishCoordinate = coordinates[coordinates.length - 2].slice();
coordinates.pop();
coordinates.push(finishCoordinate);
this.sketchPoint_.setGeometry(new Point(finishCoordinate));
}
sketchLineGeom.setCoordinates(coordinates); sketchLineGeom.setCoordinates(coordinates);
this.geometryFunction_(this.sketchCoords_, geometry, projection); this.geometryFunction_(this.sketchCoords_, geometry, projection);
} }