Skip to content

Commit

Permalink
fix: improve behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
oterral committed Oct 14, 2024
1 parent 6f6c554 commit a60a080
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@
cad.setProperties({
showSnapPoints: false,
showSnapLines: true,
showOrtholines: false,
showVerticalAndHorizontalLines: false,
});
cad.activate();
});
Expand Down
30 changes: 24 additions & 6 deletions src/helper/getShiftedMultiPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LineString, MultiPoint } from "ol/geom";

let prevCoordinates;
let prevFeature;
let prevIndex = -1;

/**
* Removes the last coordinate of a given geometry (Line or Polygon).
Expand All @@ -19,20 +20,31 @@ const getShiftedMultipoint = (
) => {
// Include all but the last vertex to the coordinate (e.g. at mouse position)
// to prevent snapping on mouse cursor node
let lineGeometry = geometry;

const isPolygon = geometry.getType() === "Polygon";
const lineGeometry = isPolygon
? new LineString(geometry.getCoordinates()[0])
: geometry;
if (isPolygon) {
const coordinates = geometry.getCoordinates()[0];

// If the poylgon is properly closed we remove the last coordinate to avoid duplicated snapping nodes and lines.
if (
coordinates[0].toString() ===
coordinates[coordinates.length - 1].toString()
) {
coordinates.pop();
}
lineGeometry = new LineString(coordinates);
}

let coordinates = [];
// console.log(editFeature, drawFeature);

if (
!editFeature ||
(prevFeature && getUid(editFeature) !== getUid(prevFeature))
) {
prevFeature = editFeature;
prevCoordinates = null;
prevIndex = -1;
}

// When the user is drawing a line or polygon, we just want to remove the last coordinate drawn.
Expand All @@ -51,13 +63,19 @@ const getShiftedMultipoint = (
return coord.toString() !== prevCoordinates[index].toString();
})
: -1;

// The use of prevIndex avoid the flickering of the snapping node on eache pointermove event.
prevIndex = index != -1 ? index : prevIndex;
prevCoordinates = lineGeometry.getCoordinates();

if (index > -1) {
if (prevIndex > -1) {
console.log(index, prevIndex);
// Exclude the node being modified
const coords = lineGeometry.getCoordinates();
coords.splice(index, 1);
coords.splice(prevIndex, 1);
coordinates = coords;
} else {
console.log("index not found", prevIndex, index);
}
}
return new MultiPoint(coordinates);
Expand Down

0 comments on commit a60a080

Please sign in to comment.