Edit

Snap Interaction with Custom Segmenter

draw16 modify9 vector82 snap3 midpoint1

Example of using the snap interaction with a custom LineString segmenter.

The Snap interaction can be configured with a custom segmenter for each geometry type. In this example, a custom LineString segmenter adds snapping to the midpoint of each segment.

main.js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import Draw from 'ol/interaction/Draw.js';
import Modify from 'ol/interaction/Modify.js';
import Snap from 'ol/interaction/Snap.js';
import VectorLayer from 'ol/layer/Vector.js';
import VectorSource from 'ol/source/Vector.js';

const vector = new VectorLayer({
  background: '#333',
  source: new VectorSource(),
  style: {
    'stroke-color': '#ffcc33',
  },
});

const map = new Map({
  layers: [vector],
  target: 'map',
  view: new View({
    center: [-11000000, 4600000],
    zoom: 4,
  }),
});

const modify = new Modify({
  source: vector.getSource(),
});
map.addInteraction(modify);

const draw = new Draw({
  source: vector.getSource(),
  type: 'LineString',
});
map.addInteraction(draw);

const snap = new Snap({
  source: vector.getSource(),
  segmenters: {
    LineString: (geometry) => {
      const segments = [];
      geometry.forEachSegment((c1, c2) => {
        segments.push([c1, c2], [[(c1[0] + c2[0]) / 2, (c1[1] + c2[1]) / 2]]);
      });
      return segments;
    },
  },
});
map.addInteraction(snap);
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Snap Interaction with Custom Segmenter</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "snap-custom-segmenter",
  "dependencies": {
    "ol": "10.5.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}