trail
StableDraws where a moving marker, or each moving point of a GeoJSON layer, has just been, as a line kept in the browser.
Set assigns
max_age
max_distance
Drive the courier
Sample route: a courier going round twelve corners in the centre of Porto, made up for this example. Visitors who ask for less motion see it jump from corner to corner, and the trail joins the corners.
- @corner
- 0
- @playing
- true
- @max_points
- 20
- @max_age
- nil
- @max_distance
- 300
- @gradient
- true
<div id="trail-example">
<.example_frame error={@map_error} error_id="example-trail-map-error">
<%!-- map:loaded starts the server's clock, which moves the courier. --%>
<.map
id="example-trail-map"
center={@centre}
zoom={16}
style={basemap(@theme)}
events={["map:loaded", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The courier: a new lng_lat every second, which it takes the second
to reach, in a straight line. A move longer than
transition_max_distance metres is a jump. --%>
<.marker
id="example-trail-courier"
map_id="example-trail-map"
lng_lat={corner(@corner)}
transition={interval()}
transition_max_distance={jump()}
>
<span class="block size-3.5 rounded-full ring-2 ring-white" style="background-color: #7c5cd6" />
</.marker>
<%!-- The trail keeps, in the browser, where the marker has been drawn: the
LiveView sends the courier's positions only, never the path.
max_points counts the corners kept, max_age the seconds of the path
shown, and max_distance breaks the line at a longer step, where the
courier jumps. --%>
<.trail
id="example-trail"
map_id="example-trail-map"
marker="example-trail-courier"
max_points={@max_points}
max_age={@max_age}
max_distance={@max_distance}
paint={paint(@gradient)}
layout={%{"line-cap" => "round", "line-join" => "round"}}
/>
<div class="space-y-4 border-t border-line px-4 py-4">
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[1fr_auto]">
<%!-- Assigns: each control changes an assign; the trail follows. --%>
<.example_group id="example-trail-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-trail-options"
phx-change="options"
class="grid w-full gap-4 sm:max-w-lg sm:grid-cols-[1fr_auto] sm:items-end"
>
<.example_range field={@options[:max_points]} label="max_points" min="2" max="50" />
<.example_checkbox field={@options[:gradient]} label="line-gradient" />
</.form>
<div class="flex w-full flex-wrap gap-x-8 gap-y-4 pt-1">
<.example_group id="example-trail-max-ages" label="max_age" code>
<.example_button
:for={seconds <- max_ages()}
id={"example-trail-max-age-#{shown(seconds)}"}
code
pressed={@max_age == seconds}
phx-click="max_age"
phx-value-seconds={shown(seconds)}
>
{shown(seconds)}
</.example_button>
</.example_group>
<.example_group id="example-trail-max-distances" label="max_distance" code>
<.example_button
:for={metres <- max_distances()}
id={"example-trail-max-distance-#{shown(metres)}"}
code
pressed={@max_distance == metres}
phx-click="max_distance"
phx-value-metres={shown(metres)}
>
{shown(metres)}
</.example_button>
</.example_group>
</div>
</.example_group>
<%!-- The server's clock: paused, the courier moves a corner at a time. --%>
<.example_group id="example-trail-drive" label={gettext("Drive the courier")}>
<.example_button id="example-trail-play" pressed={@playing} phx-click="play">
<.icon
name={if(@playing, do: "hero-pause-mini", else: "hero-play-mini")}
class="size-4"
/>
{if(@playing, do: gettext("Pause"), else: gettext("Play"))}
</.example_button>
<.example_button id="example-trail-step" phx-click="step" disabled={@playing}>
{gettext("One step")}
</.example_button>
<.example_button id="example-trail-jump" phx-click="jump">
{gettext("Jump across")}
</.example_button>
</.example_group>
</div>
<.example_note id="example-trail-sample-data">
{gettext(
"Sample route: a courier going round twelve corners in the centre of Porto, made up for this example. Visitors who ask for less motion see it jump from corner to corner, and the trail joins the corners."
)}
</.example_note>
</div>
<.example_readout values={[
corner: @corner,
playing: @playing,
max_points: @max_points,
max_age: shown(@max_age),
max_distance: shown(@max_distance),
gradient: @gradient
]} />
</div>
This component sends no events of its own. Bindings such as phx-click on its content work as usual.