Skip to content

Modes

LibreDraw uses a mode-based architecture. Only one mode is active at a time, and each mode defines how user interactions are interpreted.

Overview

ModeDescriptionActivated by
idleNo drawing interaction. Map behaves normally.Default / toolbar
drawClick to add vertices, double-click to close polygon.Toolbar draw button / setMode('draw')
selectClick to select, drag to edit vertices or move polygon.Toolbar select button / setMode('select')
splitSplit a polygon with a two-point line.Toolbar split button / setMode('split')
setbackApply inward edge setback with distance input.Toolbar setback button / setMode('setback')

Try it

Use the buttons below to switch between modes. Draw some polygons in draw mode, then switch to select mode to edit them.

Idle Mode

The default mode. No drawing or editing interactions are active. The map behaves normally — pan, zoom, and all standard MapLibre interactions work.

ts
draw.setMode('idle');

Draw Mode

In draw mode, you create new polygons by clicking on the map.

Mouse Interaction

ActionEffect
ClickAdd a vertex
Double-clickClose the polygon (minimum 3 vertices)
Escape keyCancel the current drawing

Touch Interaction

ActionEffect
TapAdd a vertex
Double-tapClose the polygon
Long-pressUndo last vertex

Behavior

  • A preview line follows the cursor while drawing
  • A semi-transparent polygon preview shows the current shape
  • Map panning is disabled during draw mode
  • Double-click zoom is disabled during draw mode
  • Self-intersecting polygons are automatically rejected
ts
draw.setMode('draw');

draw.on('create', (e) => {
  console.log('New polygon:', e.feature);
  // Remains in draw mode for continuous drawing
});

Select Mode

In select mode, you can select existing polygons and edit them.

Selecting

ActionEffect
Click on polygonSelect it (shows vertex handles)
Click outsideDeselect
Delete keyDelete selected polygon

Vertex Editing

When a polygon is selected, vertex handles appear:

ActionEffect
Drag a vertexMove the vertex
Drag a midpointInsert a new vertex and drag it
Long-press a vertexDelete the vertex (minimum 3 maintained)

Polygon Dragging

ActionEffect
Drag inside polygonMove the entire polygon

Behavior

  • Double-click zoom is disabled during select mode
  • Map panning is temporarily disabled during vertex/polygon drag
  • Self-intersection is prevented during editing
  • Undo/redo works for all edit operations
ts
draw.setMode('select');

// Or programmatically select a feature
draw.selectFeature('feature-id');

draw.on('update', (e) => {
  console.log('Polygon edited:', e.feature);
});

draw.on('selectionchange', (e) => {
  console.log('Selection:', e.selectedIds);
});

Split Mode

In split mode, you split one polygon into two polygons.

ActionEffect
Click on polygonSelect split target
Click first pointSet split-line start
Click second pointExecute split
Escape keyCancel current split interaction
ts
draw.setMode('split');
draw.on('split', (e) => console.log(e.originalFeature.id, e.features));
draw.on('splitfailed', (e) => console.warn(e.reason));

Setback Mode

In setback mode, you select an edge and apply inward offset by distance.

ActionEffect
Click on polygonSelect setback target
Click edgeStart preview
Change distanceUpdate preview line
Enter / execute buttonApply setback
Escape keyCancel and reset
ts
draw.setMode('setback');
draw.on('setback', (e) => console.log(e.edgeIndex, e.distance));
draw.on('setbackfailed', (e) => console.warn(e.reason));

Mode Transitions

                   setMode('draw')
           ┌─────────────────────────┐
           │                         ▼
        ┌──────┐               ┌──────────┐
        │ idle │               │   draw   │
        └──────┘               └──────────┘
           ▲                         │
           │     polygon created     │
           └─────────────────────────┘

           │    setMode('select')
           ├─────────────────────────┐
           │                         ▼
           │                   ┌──────────┐
           │                   │  select  │
           │                   └──────────┘
           │                         │
           ├─────────────────────────┘
           │    setMode('split')
           ├─────────────────────────┐
           │                         ▼
           │                   ┌──────────┐
           │                   │  split   │
           │                   └──────────┘
           │                         │
           ├─────────────────────────┘
           │    setMode('setback')
           ├─────────────────────────┐
           │                         ▼
           │                   ┌──────────┐
           │                   │ setback  │
           │                   └──────────┘
           │                         │
           └─────────────────────────┘
                  setMode('idle')

Every mode transition emits a modechange event:

ts
draw.on('modechange', (e) => {
  console.log(`${e.previousMode} → ${e.mode}`);
});

Released under the MIT License.