Skip to content

background_layer

Stable

Paints the map background with a colour or a pattern.

Set assigns

@color
#b9d9eb
@opacity
1.0
lib/maplibrex_demo_web/examples/background_layer_example.html.heex
<div id="background-layer-example">
  <.example_frame
    error={@map_error}
    error_id="example-background-map-error"
    hosts={["demotiles.maplibre.org"]}
  >
    <%!-- A blank style, in both themes: the background and the land below
          are all the map draws, in the colour chosen here. --%>
    <.map
      id="example-background-map"
      center={[15, 25]}
      zoom={1.3}
      style={blank_style()}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- The background has no source, so it reaches the map first and the
        land is drawn over it. --%>
  <.background_layer
    id="example-background-layer"
    map_id="example-background-map"
    paint={%{"background-color" => @color, "background-opacity" => @opacity}}
  />

  <.vector_tile_source
    id="example-background-countries"
    map_id="example-background-map"
    tiles={["https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf"]}
    max_zoom={6}
    attribution="Natural Earth, MapLibre demo tiles"
    events={["source:error"]}
  />

  <.fill_layer
    id="example-background-land"
    map_id="example-background-map"
    source_id="example-background-countries"
    source_layer="countries"
    paint={%{"fill-color" => land(@theme), "fill-outline-color" => "#9a9db0"}}
  />

  <div class="space-y-3 border-t border-line px-4 py-4">
    <.example_group id="example-background-assigns" label={gettext("Set assigns")}>
      <.example_button
        :for={{name, hex} <- colors()}
        id={"example-background-color-#{name}"}
        code
        pressed={@color == hex}
        phx-click="color"
        phx-value-color={hex}
      >
        <.example_swatch color={hex} />{hex}
      </.example_button>

      <.form
        for={@options}
        id="example-background-options"
        phx-change="options"
        class="w-full pt-2 sm:max-w-xs"
      >
        <.example_range
          field={@options[:opacity]}
          label="background-opacity"
          min="0"
          max="1"
          step="0.05"
        />
      </.form>
    </.example_group>
  </div>

  <.example_readout values={[color: @color, opacity: @opacity]} />
</div>