HoloMap.Layer.Custom (HoloMap v0.4.0)

Copy Markdown View Source

A layer that draws with WebGL code the application provides, such as a three.js scene or deck.gl layers.

<HoloMap.Layer.Custom id="buildings" factory="buildings3d"
                      options={%{height_scale: 1.5}} before_id="labels" />

MapLibre takes such a layer as an object, a CustomLayerInterface: a render(gl, frame) it calls on every frame, and optionally onAdd(map, gl), onRemove(map, gl), prerender(gl, frame) and a renderingMode of "2d" or "3d". No prop can carry one, so the application registers a function that builds it, under a name, from a script its layout loads, and factory names it:

(window.HoloMap.registry ||= []).push((register) => {
  register.layer("buildings3d", (options, { map, cid, id }) => new BuildingsLayer(options));
});

The Registering JavaScript guide shows how to load that script with Hologram, and a three.js layer and a deck.gl one.

Props

PropTypeNotes
idstringRequired. The layer id, and its identity across re-renders
factorystringRequired. The name the factory is registered under with register.layer
optionsmapHanded to the factory, and to update. Sent as written: height_scale stays height_scale
before_idstringInsert beneath this layer. Ignored while that layer is absent

options crosses into the browser as JSON, so it holds what JSON can: maps, lists, strings, numbers, booleans and nil.

MapLibre sees the layer as the component's id, of type "custom", whatever the object holds. The runtime hands MapLibre a stand-in that reads and writes the object and calls its methods on it, so the object itself is not changed.

Lifecycle

The layer is a style layer like the others. It is added once the style has loaded, stacks in template order among the other layers, and before_id places it as it places theirs. The factory is called with a copy of options and %{map, cid, id}, and MapLibre calls the object's onAdd with the map and its WebGL context. Removing the component calls onRemove.

An options change calls the object's update(options), when it has one, and has the map draw again. An object without update, and a factory change, are built again: the old object's onRemove, then the factory, and the new object goes in the same place.

A style change keeps the layer. MapLibre cannot describe a custom layer as JSON, so a style change it diffs leaves the layer on the map, the same object, and the runtime puts it back in its place among the new style's layers. A style built from scratch drops it without telling the object: the runtime calls onRemove for MapLibre, and adds the same object to the new style, which calls onAdd again. A style whose own layer has the layer's id takes the id, as it does any layer's: the layer comes off, with onRemove, and the browser runtime warns. After a WebGL context loss, MapLibre calls onRemove, logs that it cannot restore the layer, and the runtime adds the same object again once the context is back, on the new context, with onAdd. Every onAdd is followed by one onRemove before the next, and the map's teardown calls the last.

A factory nothing has registered yet leaves the layer off the map, with a console warning once for each name on each map, and the layer is added in its place as soon as one is. A factory that throws, or returns something that is not an object, is reported through the map's on_error, or the browser console without one, and so is the object's onAdd, onRemove or update throwing. The first error each of render, prerender and renderToTerrainTile throws is reported, and the frame goes on: those run on every frame. A layer without render is refused by MapLibre, which says so through on_error. The rest of the map is not affected.

The layer has no events: MapLibre finds no features on a custom layer.

Summary

Functions

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Returns the list of property definitions for the compiled component.

Builds the specification of a custom layer: its factory, its options, as written, and its before_id, which places it as any layer's does.

Functions

__is_hologram_component__()

@spec __is_hologram_component__() :: boolean()

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Examples

iex> __is_hologram_component__()
true

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.

build(factory, options, before_id)

@spec build(String.t(), map() | nil, String.t() | nil) :: String.t()

Builds the specification of a custom layer: its factory, its options, as written, and its before_id, which places it as any layer's does.

iex> HoloMap.Layer.Custom.build("buildings3d", %{height_scale: 1.5}, "labels")
~s({"beforeId":"labels","factory":"buildings3d","options":{"height_scale":1.5},"type":"custom"})