A toolbar to draw points, lines and shapes, edit and delete them, and undo or redo.
<script lang="ts">
import { Map, MapControlContainer } from '$lib/components/ui/map/index.js';
import {
MapDrawControl,
DRAW_MODES,
type DrawFeature,
type DrawTool
} from '$lib/components/ui/map-draw/index.js';
let mode = $state<DrawTool>('none');
let features = $state.raw<DrawFeature[]>([]);
const summary = $derived.by(() => {
if (features.length === 0) return 'Nothing drawn yet';
return DRAW_MODES.map((kind) => ({
kind,
count: features.filter((feature) => feature.properties.mode === kind).length
}))
.filter((entry) => entry.count > 0)
.map((entry) => `${entry.count} ${entry.kind}`)
.join(', ');
});
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [-0.1276, 51.5072], zoom: 14, bearing: 0, pitch: 0 }}>
<MapControlContainer position="top-left">
<MapDrawControl bind:mode onchange={(next) => (features = next)} />
</MapControlContainer>
</Map>
<p
class="absolute top-3 right-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
aria-live="polite"
>
<span class="text-muted-foreground">Tool:</span>
{mode} · {summary}
</p>
</div>
The draw control is a toolbar of drawing tools. Pick a tool and draw on the map. Pick “Select and edit” to move a shape or drag its corners, then delete it with the trash button. Undo and redo step through every change.
Drawing runs on terra-draw, loaded only when the control mounts. Shapes take their colours from your theme and update when it changes.
This page covers the control’s API. For saving drawings, loading them back and other workflows, read the Drawing guide.
Installation
It installs terra-draw and its MapLibre adapter, and needs the map.
pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.jsonnpx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.jsonyarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.jsonbunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.jsonUsage
Place the control inside a Control Container.
<script lang="ts">
import { Map, MapControlContainer } from '$lib/components/ui/map/index.js';
import { MapDrawControl, type DrawFeature } from '$lib/components/ui/map-draw/index.js';
let features = $state.raw<DrawFeature[]>([]);
</script>
<Map class="h-96">
<MapControlContainer position="top-left">
<MapDrawControl onchange={(next) => (features = next)} />
</MapControlContainer>
</Map>Tools
The DrawMode shapes, in the order of DRAW_MODES:
point: click to place a point.polyline: click each corner, then click the last one again to finish.polygon: click each corner, then click the first one to close it.rectangle: click one corner, then the opposite one.circle: click the centre, then a point on the edge.freehand: click, move the pointer around the shape, and click again to close it.
After the shapes come “Select and edit”, then “Delete selected”, “Undo” and “Redo”.
Choosing a tool
Bind mode to read or set the active tool. It holds a DrawTool: one of the shapes, select, or none. With none, the pointer pans and zooms the map as usual and the drawings stay. Pressing the active tool’s button again goes back to none.
<script lang="ts">
import type { DrawTool } from '$lib/components/ui/map-draw/index.js';
let mode = $state<DrawTool>('polygon');
</script>
<MapDrawControl bind:mode />Picking the buttons
modes sets which shape buttons show, and in which order. orientation lays the toolbar out as a column or a row.
<script lang="ts">
import { Map, MapControlContainer } from '$lib/components/ui/map/index.js';
import { MapDrawControl } from '$lib/components/ui/map-draw/index.js';
</script>
<Map class="size-full" viewport={{ center: [-122.4194, 37.7749], zoom: 13, bearing: 0, pitch: 0 }}>
<MapControlContainer position="bottom-left">
<MapDrawControl modes={['polygon', 'rectangle', 'circle']} orientation="horizontal" />
</MapControlContainer>
</Map>
<MapDrawControl modes={['polygon', 'rectangle', 'circle']} orientation="horizontal" />Getting the drawings
onchange gets every finished drawing as a DrawFeature[]. It runs after each create, edit, delete, undo and redo. A shape still being drawn is not in the list, and neither are the handles shown while editing.
Each DrawFeature is a plain GeoJSON Feature with an id, a Point, LineString or Polygon geometry, and properties.mode set to the DrawMode that drew it. Rectangles, circles and freehand shapes come back as polygons.
Keep the list in $state.raw: it is replaced as a whole on every change.
The engine
Bind ref to get the running terra-draw instance once it starts, for things the toolbar does not cover. onerror is called if the drawing engine fails to load. Without it, the error is logged to the console.
API Reference
MapDrawControl
Props
| Name | Type | Description |
|---|---|---|
mode bindable | DrawTool | The active tool. `none` leaves the pointer to the map. Defaults to 'none'. |
modes | readonly DrawMode[] | Shapes offered as buttons, in this order. Defaults to DRAW_MODES. |
orientation | 'vertical' | 'horizontal' | Lay the tool buttons out in a column or a row. Defaults to 'vertical'. |
features bindable | DrawFeature[] | The drawings on the map. Loaded when drawing starts, so saved shapes can be edited; pass a different list to replace them. Updated after every change, so `bind:features` keeps it in step. |
ref bindable | TerraDraw | null | The terra-draw instance, once it runs. Defaults to null. |
Events
| Name | Type | Description |
|---|---|---|
onchange | (features: DrawFeature[]) => void | Every finished drawing, after each create, edit, delete, undo or redo. |
onerror | (error: Error) => void | The drawing engine failed to load, or a passed feature could not be drawn. Logged when unset. |
Other attributes go to the underlying <div>.