draw
StableLets the user draw and edit shapes on the map, such as geofences, from a toolbar of its own or the application's buttons, sends them to the LiveView as GeoJSON, and shows the shapes the LiveView gives it.
The mode is set by
With no mode given, the drawing's toolbar sets it in the browser, and tells the LiveView with draw:mode_changed.
New geofence
Start a shape on the map, and a form for its name opens here.
fill_color
@geofences
Choose a tool in the map's top-left corner. A polygon takes a click per corner and closes on its first one; a rectangle or a circle takes two clicks. With select, drag a geofence or its corners, or the middle of a polygon's edge to add a corner, and press Delete to remove it.
- @set_by
- toolbar
- @mode
- nil
- @drawing
- nil
- @cancelled
- 0
- @fill_color
- #7c5cd6
- @geofences
- 2
- @next_id
- 3
<div id="draw-example">
<.example_frame error={@map_error} error_id="example-draw-map-error">
<.map
id="example-draw-map"
center={[4.8434, 45.7565]}
zoom={12.8}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The drawing's own toolbar, in the map's top-left corner. No mode is
given: the toolbar sets it in the browser, and draw:mode_changed
tells the LiveView. Its labels are the page's, in its language.
draw:started and draw:cancelled open and close the form below. Its
hook comes from maplibrex/draw (assets/js/app.js). --%>
<.draw
:if={@set_by == "toolbar"}
id="example-draw"
map_id="example-draw-map"
modes={[:polygon, :rectangle, :circle, :select]}
features={@geofences}
fill_color={@fill_color}
fill_opacity={0.25}
outline_color={@fill_color}
outline_width={2}
toolbar="top-left"
toolbar_labels={toolbar_labels()}
events={[
"draw:created",
"draw:updated",
"draw:deleted",
"draw:started",
"draw:cancelled",
"draw:mode_changed"
]}
/>
<%!-- The LiveView sets the mode, from @mode: with the toolbar, a button
asks for its mode with draw:mode_changed; without it, the page's own
buttons below set it. The geofences are the truth for the ids they
name: an edit the LiveView puts back stays, a geofence taken out
goes. --%>
<.draw
:if={@set_by != "toolbar"}
id="example-draw"
map_id="example-draw-map"
mode={@mode}
modes={[:polygon, :rectangle, :circle, :select]}
features={@geofences}
fill_color={@fill_color}
fill_opacity={0.25}
outline_color={@fill_color}
outline_width={2}
toolbar={if(@set_by == "toolbar_mode", do: "top-left")}
toolbar_labels={toolbar_labels()}
events={[
"draw:created",
"draw:updated",
"draw:deleted",
"draw:started",
"draw:cancelled",
"draw:mode_changed"
]}
/>
<%!-- The page's own toolbar, when the drawing has none: each button sets
the mode, and the active one is tinted. --%>
<.control_group
:if={@set_by == "buttons"}
id="example-draw-tools"
map_id="example-draw-map"
position="top-left"
>
<.control_button
:for={mode <- draw_modes()}
id={"example-draw-tool-#{mode}"}
map_id="example-draw-map"
tooltip={tooltip(mode)}
active={@mode == mode}
class="grid! place-items-center"
phx-click="draw_mode"
phx-value-mode={mode}
>
<.tool_icon mode={mode} />
</.control_button>
</.control_group>
<div class="space-y-4 border-t border-line px-4 py-4">
<%!-- Who sets the mode. --%>
<.example_group id="example-draw-setter" label={gettext("The mode is set by")}>
<.example_button
:for={setter <- setters()}
id={"example-draw-setter-#{setter}"}
pressed={@set_by == setter}
phx-click="set_by"
phx-value-setter={setter}
>
{setter_label(setter)}
</.example_button>
<p id="example-draw-setter-note" class="w-full pt-1 text-label text-ink-muted">
{setter_note(@set_by)}
</p>
</.example_group>
<%!-- The new geofence: draw:started opens the form, draw:created and
draw:cancelled close it. --%>
<div
id="example-draw-draft"
role="group"
aria-labelledby="example-draw-draft-label"
class="min-w-0"
>
<p id="example-draw-draft-label" class="mb-2 text-label font-bold text-ink-subtle">
{gettext("New geofence")}
</p>
<p :if={!@draft} id="example-draw-draft-idle" class="text-ui text-ink-muted">
{gettext("Start a shape on the map, and a form for its name opens here.")}
</p>
<.form
:if={@draft}
for={%{}}
id="example-draw-draft-form"
phx-change="draft"
phx-submit="draft"
class="flex flex-wrap items-end gap-x-4 gap-y-2"
>
<.input
type="text"
id="example-draw-draft-name"
name="name"
value={@draft.name}
label={gettext("Name")}
autocomplete="off"
wrapper_class="w-56 text-label font-bold text-ink-subtle"
/>
<p class="pb-2 text-ui text-ink-muted">
{gettext("A %{mode} under way. Finish it on the map, or press Escape to give it up.",
mode: @draft.mode
)}
</p>
</.form>
</div>
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-draw-colors" label="fill_color" code>
<.example_button
:for={{name, hex} <- colors()}
id={"example-draw-color-#{name}"}
code
pressed={@fill_color == hex}
phx-click="fill_color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
</.example_group>
<%!-- @geofences as the LiveView stores them: id, mode and, for a
circle, its radius in metres. --%>
<.example_group id="example-draw-fences" label="@geofences" code>
<span
:for={fence <- @geofences}
id={"example-draw-fence-#{fence["id"]}"}
class="inline-flex h-8 items-center gap-1.5 rounded-ui border border-line px-2.5 font-mono text-label text-ink"
>
<span class="text-ink-subtle">{fence["id"]}</span>
<span :if={name(fence)} class="font-sans font-bold">{name(fence)}</span>
{kind(fence)}
<span :if={radius(fence)} class="text-ink-muted">{radius(fence)}</span>
</span>
</.example_group>
</div>
<.example_note id="example-draw-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Choose a tool in the map's top-left corner. A polygon takes a click per corner and closes on its first one; a rectangle or a circle takes two clicks. With select, drag a geofence or its corners, or the middle of a polygon's edge to add a corner, and press Delete to remove it."
)}
</.example_note>
</div>
<%!-- draw:created, draw:updated and draw:deleted keep @geofences current;
draw:started and draw:cancelled set drawing and count the shapes
given up. --%>
<.example_readout values={[
set_by: @set_by,
mode: @mode || "nil",
drawing: (@draft && @draft.mode) || "nil",
cancelled: @cancelled,
fill_color: @fill_color,
geofences: length(@geofences),
next_id: @next_id
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier of the drawing, also the start of the ids of the sources and layers Terra Draw adds to the map (see On the map in the module docs). Read when the component mounts |
| map_id required | string | — | ID of the map to draw on. Read when the component mounts |
| mode | any | — |
a mode of modes, or nil (the default) for none
|
| modes | list |
[:polygon, :rectangle, :circle, :line, :point, :select]
|
the modes to set up |
| features | list |
[]
|
the shapes to show and edit: GeoJSON Features, each with an id of the LiveView's |
| fill_color | string |
nil
|
the fill colour of polygons, rectangles and circles |
| fill_opacity | any |
nil
|
their fill opacity, from 0 to 1 |
| outline_color | string |
nil
|
the colour of their outlines, and of lines |
| outline_width | any |
nil
|
the width of their outlines, and of lines, in pixels |
| point_color | string |
nil
|
the colour of points |
| point_radius | any |
nil
|
the radius of points, in pixels |
| selected_color | string |
nil
|
the colour of the selected shape |
| handle_color | string |
nil
|
the colour of the handles the user drags |
| toolbar | any |
nil
|
the corner of the map for a toolbar: "top-left", "top-right", ... |
| toolbar_labels | map |
%{}
|
the labels of the toolbar's buttons, by mode or :cancel
|
| events | any |
[]
|
a list of event names, or :all
|