Skip to content

custom_layer

Stable

A custom WebGL layer drawn with your own GLSL shaders, whose uniforms follow the LiveView's assigns.

u_color

basemap

before_id

min_zoom

max_zoom

@color
[0.2, 0.6, 0.9]
@speed
1.0
@point_size
3.0
@opacity
0.8
@basemap
dark
@before_id
nil
@min_zoom
nil
@max_zoom
nil
lib/maplibrex_demo_web/examples/custom_layer_example.html.heex
<div id="custom-layer-example">
  <%!-- The shaders are strings, and every other attribute is an assign the
        layer follows after mount. The component can come before its map:
        the layer is added once the map can take it. --%>
  <.custom_layer
    id="example-custom-layer"
    map_id="example-custom-layer-map"
    vertex_shader={vertex_shader()}
    fragment_shader={fragment_shader()}
    uniforms={@uniforms}
    before_id={@before_id}
    min_zoom={@min_zoom}
    max_zoom={@max_zoom}
  />

  <.example_frame error={@map_error} error_id="example-custom-layer-map-error">
    <%!-- Dark in both themes: the light points would vanish on a light
          basemap. --%>
    <.map
      id="example-custom-layer-map"
      center={[0, 20]}
      zoom={1.6}
      style={style_url(@basemap)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <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-[auto_1fr]">
      <.example_group id="example-custom-layer-colors" label="u_color" code>
        <.example_button
          :for={{name, color} <- colors()}
          id={"example-custom-layer-color-#{name}"}
          code
          pressed={@color == color}
          phx-click="color"
          phx-value-color={name}
        >
          <.example_swatch color={css_color(color)} />{inspect(color)}
        </.example_button>
      </.example_group>

      <.example_group id="example-custom-layer-basemaps" label="basemap" code>
        <.example_button
          :for={basemap <- basemaps()}
          id={"example-custom-layer-basemap-#{basemap}"}
          code
          pressed={@basemap == basemap}
          phx-click="basemap"
          phx-value-basemap={basemap}
        >
          {basemap}
        </.example_button>
      </.example_group>

      <.example_group id="example-custom-layer-before-id" label="before_id" code>
        <.example_button
          :for={before_id <- before_ids()}
          id={"example-custom-layer-before-id-#{before_id || "nil"}"}
          code
          pressed={@before_id == before_id}
          phx-click="before_id"
          phx-value-before_id={before_id || ""}
        >
          {inspect(before_id)}
        </.example_button>
      </.example_group>

      <div class="flex flex-wrap gap-x-8 gap-y-4">
        <.example_group id="example-custom-layer-min-zoom" label="min_zoom" code>
          <.example_button
            :for={zoom <- min_zooms()}
            id={"example-custom-layer-min-zoom-#{zoom || "nil"}"}
            code
            pressed={@min_zoom == zoom}
            phx-click="min_zoom"
            phx-value-zoom={zoom || ""}
          >
            {inspect(zoom)}
          </.example_button>
        </.example_group>

        <.example_group id="example-custom-layer-max-zoom" label="max_zoom" code>
          <.example_button
            :for={zoom <- max_zooms()}
            id={"example-custom-layer-max-zoom-#{zoom || "nil"}"}
            code
            pressed={@max_zoom == zoom}
            phx-click="max_zoom"
            phx-value-zoom={zoom || ""}
          >
            {inspect(zoom)}
          </.example_button>
        </.example_group>
      </div>
    </div>

    <.form
      for={@options}
      id="example-custom-layer-options"
      phx-change="options"
      class="grid gap-4 sm:grid-cols-3"
    >
      <.example_range field={@options[:speed]} label="speed" min="0" max="5" step="0.1" />
      <.example_range
        field={@options[:point_size]}
        label="point_size"
        min="1"
        max="10"
        step="0.5"
      />
      <%!-- Pushed to the layer with update_uniforms_<id>: see the module. --%>
      <.example_range field={@options[:opacity]} label="opacity" min="0" max="1" step="0.05" />
    </.form>
  </div>

  <.example_readout values={[
    color: inspect(@color),
    speed: @speed,
    point_size: @point_size,
    opacity: @opacity,
    basemap: @basemap,
    before_id: inspect(@before_id),
    min_zoom: inspect(@min_zoom),
    max_zoom: inspect(@max_zoom)
  ]} />
</div>