sky
StableMapLibre's sky above the horizon of a pitched map, with fog over 3D terrain and an atmosphere around a globe.
Set assigns
The sky is drawn above the horizon only: lower the pitch below about 69 degrees and it goes out of view. The fog shows over the terrain.
- @palette
- day
- @sky_color
- #199EF3
- @pitch
- 80
- @sky_horizon_blend
- 0.5
- @horizon_fog_blend
- 0.8
- @fog_ground_blend
- 0.5
- @enabled
- true
- @terrain
- false
- @status
- waiting
<div id="sky-example">
<.example_frame
error={@map_error}
error_id="example-sky-map-error"
hosts={["tiles.openfreemap.org", "tiles.mapterhorn.com"]}
>
<%!-- The sky shows above the horizon only: from a pitch of about 69
degrees on a flat map. max_pitch lets the camera tilt past
MapLibre's default of 60. --%>
<.map
id="example-sky-map"
center={[11.3909, 47.2757]}
zoom={11}
pitch={@pitch}
bearing={-20}
max_pitch={85}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- A map has one sky. It comes back after a change of basemap, and goes
with the component. --%>
<.sky
:if={@enabled}
map_id="example-sky-map"
sky_color={@sky_color}
horizon_color={@horizon_color}
fog_color={@fog_color}
sky_horizon_blend={@sky_horizon_blend}
horizon_fog_blend={@horizon_fog_blend}
fog_ground_blend={@fog_ground_blend}
events={["sky:enabled", "sky:disabled"]}
/>
<%!-- The fog is drawn over 3D terrain only. --%>
<.raster_dem_source
:if={@terrain}
id="example-sky-dem"
map_id="example-sky-map"
url="https://tiles.mapterhorn.com/tilejson.json"
/>
<.terrain
:if={@terrain}
map_id="example-sky-map"
source_id="example-sky-dem"
exaggeration={1.5}
events={["terrain:error"]}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<%!-- Assigns: each control changes an assign; sky and map follow. --%>
<.example_group id="example-sky-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{palette, colors} <- palettes()}
id={"example-sky-palette-#{palette}"}
pressed={@palette == palette}
phx-click="palette"
phx-value-palette={palette}
>
<.example_swatch color={colors.sky_color} />{palette}
</.example_button>
<.form
for={@options}
id="example-sky-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:grid-cols-2 lg:grid-cols-4 lg:items-end"
>
<.example_range field={@options[:pitch]} label="pitch" min="0" max="85" />
<.example_range
field={@options[:sky_horizon_blend]}
label="sky_horizon_blend"
min="0"
max="1"
step="0.05"
/>
<.example_range
field={@options[:horizon_fog_blend]}
label="horizon_fog_blend"
min="0"
max="1"
step="0.05"
/>
<.example_range
field={@options[:fog_ground_blend]}
label="fog_ground_blend"
min="0"
max="1"
step="0.05"
/>
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:terrain]} label="<.terrain>" />
</.form>
</.example_group>
<.example_note id="example-sky-hint" icon="hero-information-circle-micro">
{gettext(
"The sky is drawn above the horizon only: lower the pitch below about 69 degrees and it goes out of view. The fog shows over the terrain."
)}
</.example_note>
</div>
<%!-- sky:enabled and sky:disabled keep status current. --%>
<.example_readout values={[
palette: @palette,
sky_color: @sky_color,
pitch: @pitch,
sky_horizon_blend: @sky_horizon_blend,
horizon_fog_blend: @horizon_fog_blend,
fog_ground_blend: @fog_ground_blend,
enabled: @enabled,
terrain: @terrain,
status: @status
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="sky-example">
<.example_frame
error={@map_error}
error_id="example-sky-map-error"
hosts={["tiles.openfreemap.org", "tiles.mapterhorn.com"]}
>
<%!-- The sky shows above the horizon only: from a pitch of about 69
degrees on a flat map. max_pitch lets the camera tilt past
MapLibre's default of 60. --%>
<.map
id="example-sky-map"
center={[11.3909, 47.2757]}
zoom={11}
pitch={@pitch}
bearing={-20}
max_pitch={85}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- A map has one sky. It comes back after a change of basemap, and goes
with the component. --%>
<.sky
:if={@enabled}
map_id="example-sky-map"
sky_color={@sky_color}
horizon_color={@horizon_color}
fog_color={@fog_color}
sky_horizon_blend={@sky_horizon_blend}
horizon_fog_blend={@horizon_fog_blend}
fog_ground_blend={@fog_ground_blend}
events={["sky:enabled", "sky:disabled"]}
/>
<%!-- The fog is drawn over 3D terrain only. --%>
<.raster_dem_source
:if={@terrain}
id="example-sky-dem"
map_id="example-sky-map"
url="https://tiles.mapterhorn.com/tilejson.json"
/>
<.terrain
:if={@terrain}
map_id="example-sky-map"
source_id="example-sky-dem"
exaggeration={1.5}
events={["terrain:error"]}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<%!-- Assigns: each control changes an assign; sky and map follow. --%>
<.example_group id="example-sky-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{palette, colors} <- palettes()}
id={"example-sky-palette-#{palette}"}
pressed={@palette == palette}
phx-click="palette"
phx-value-palette={palette}
>
<.example_swatch color={colors.sky_color} />{palette}
</.example_button>
<.form
for={@options}
id="example-sky-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:grid-cols-2 lg:grid-cols-4 lg:items-end"
>
<.example_range field={@options[:pitch]} label="pitch" min="0" max="85" />
<.example_range
field={@options[:sky_horizon_blend]}
label="sky_horizon_blend"
min="0"
max="1"
step="0.05"
/>
<.example_range
field={@options[:horizon_fog_blend]}
label="horizon_fog_blend"
min="0"
max="1"
step="0.05"
/>
<.example_range
field={@options[:fog_ground_blend]}
label="fog_ground_blend"
min="0"
max="1"
step="0.05"
/>
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:terrain]} label="<.terrain>" />
</.form>
</.example_group>
<.example_note id="example-sky-hint" icon="hero-information-circle-micro">
{gettext(
"The sky is drawn above the horizon only: lower the pitch below about 69 degrees and it goes out of view. The fog shows over the terrain."
)}
</.example_note>
</div>
<%!-- sky:enabled and sky:disabled keep status current. --%>
<.example_readout values={[
palette: @palette,
sky_color: @sky_color,
pitch: @pitch,
sky_horizon_blend: @sky_horizon_blend,
horizon_fog_blend: @horizon_fog_blend,
fog_ground_blend: @fog_ground_blend,
enabled: @enabled,
terrain: @terrain,
status: @status
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.SkyExample do
@moduledoc """
The example of `<.sky>`: MapLibre's sky above the horizon of a pitched
map, its colours and blends set from assigns, turned off and on with
`:if`. 3D terrain can join it, to show the fog the sky draws over it.
MapLibre draws the sky above the horizon only: on a flat map from a pitch
of about 69 degrees, past its default maximum pitch of 60, so the map has
a `max_pitch` and opens at 80. `atmosphere_blend`, the last attribute, is
drawn around a globe only, and keeps MapLibre's default here.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/liberty"
# The sky, horizon and fog colours of each palette.
@palettes [
{"day", %{sky_color: "#199EF3", horizon_color: "#ffffff", fog_color: "#d6e4f0"}},
{"dusk", %{sky_color: "#2b3a67", horizon_color: "#f4a261", fog_color: "#e9c46a"}},
{"night", %{sky_color: "#0b1026", horizon_color: "#3d5a80", fog_color: "#293241"}}
]
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign_palette("day")
|> assign(pitch: 80, sky_horizon_blend: 0.5, horizon_fog_blend: 0.8, fog_ground_blend: 0.5)
|> assign(enabled: true, terrain: false, status: "waiting", map_error: nil)
|> assign_options()
{:ok, socket}
end
# Driven from the server: the sky follows its colours and blends, the map
# follows pitch, :if takes the sky off and puts it back, and the last
# checkbox adds or removes the terrain.
@impl true
def handle_event("palette", %{"palette" => palette}, socket) do
if List.keymember?(@palettes, palette, 0) do
{:noreply, assign_palette(socket, palette)}
else
{:noreply, socket}
end
end
def handle_event("options", params, socket) do
{:noreply,
socket
|> assign(
pitch: number(params["pitch"]),
sky_horizon_blend: blend(params["sky_horizon_blend"]),
horizon_fog_blend: blend(params["horizon_fog_blend"]),
fog_ground_blend: blend(params["fog_ground_blend"]),
enabled: params["enabled"] == "true",
terrain: params["terrain"] == "true"
)
|> assign_options()}
end
# Events from the sky: set the first time, taken off when it goes. Its
# changes in place and its return after a style change send neither.
def handle_event("sky:enabled", _payload, socket) do
{:noreply, assign(socket, :status, "enabled")}
end
def handle_event("sky:disabled", _payload, socket) do
{:noreply, assign(socket, :status, "disabled")}
end
# The camera from the map, so the pitch slider follows a pitch set by
# dragging the map, and errors from the map and the terrain.
def handle_event("map:moved", %{"pitch" => pitch}, socket) do
{:noreply, socket |> assign(:pitch, pitch) |> assign_options()}
end
def handle_event(event, %{"error" => message}, socket)
when event in ["map:error", "terrain:error"] do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_palette(socket, palette) do
{^palette, colors} = List.keyfind(@palettes, palette, 0)
assign(socket, Map.put(colors, :palette, palette))
end
defp assign_options(socket) do
form =
socket.assigns
|> Map.take([
:pitch,
:sky_horizon_blend,
:horizon_fog_blend,
:fog_ground_blend,
:enabled,
:terrain
])
|> Map.new(fn {key, value} -> {Atom.to_string(key), value} end)
|> to_form()
assign(socket, :options, form)
end
defp palettes, do: @palettes
# Slider values arrive as strings: "0.5", "1", "80".
defp blend(value) do
case Float.parse(value || "") do
{float, _rest} -> min(max(float, 0.0), 1.0)
:error -> 0.5
end
end
defp number(value) do
case Integer.parse(value) do
{integer, ""} -> integer
_ -> value |> Float.parse() |> elem(0)
end
end
end