The layer is updated when the style changes.

Options

100%
index.js
import { Color, MathUtils } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";

import { Feature } from "ol";
import { LineString, Point, Polygon } from "ol/geom.js";
import { Circle, Fill, Stroke, Style } from "ol/style.js";

import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import VectorSource from "@giro3d/giro3d/sources/VectorSource.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";

function bindButton(id, onClick) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLButtonElement)) {
    throw new Error(
      "invalid binding element: expected HTMLButtonElement, got: " +
        element.constructor.name,
    );
  }

  element.onclick = () => {
    onClick(element);
  };

  return element;
}

function bindToggle(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLButtonElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.checked);
  };

  const callback = (v) => {
    element.checked = v;
    onChange(element.checked);
  };

  return [callback, element.checked, element];
}

function bindSlider(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLInputElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.valueAsNumber);
  };

  const setValue = (v) => {
    element.valueAsNumber = v;
    onChange(element.valueAsNumber);
  };

  const initialValue = element.valueAsNumber;

  return [setValue, initialValue, element];
}

const extent = Extent.fromCenterAndSize(
  "EPSG:3857",
  { x: 11393552, y: 44035 },
  1000000,
  500000,
);

const instance = new Instance({
  target: "view",
  crs: extent.crs,
  backgroundColor: "white",
});

const center = extent.centerAsVector3();
instance.view.camera.position.set(center.x, center.y - 1, 1000000);

const controls = new MapControls(instance.view.camera, instance.domElement);

controls.target = center;
controls.saveState();

instance.view.setControls(controls);

const map = new Map({ extent });
instance.add(map);

const fillColor = new Color("orange");
const strokeColor = new Color("red");

const image = new Circle({
  radius: 20,
  fill: new Fill({
    color: `#${fillColor.getHexString()}`,
  }),
  stroke: new Stroke({
    color: `#${strokeColor.getHexString()}`,
    width: 5,
  }),
});

const fill = new Fill({
  color: `#${fillColor.getHexString()}`,
});

const stroke = new Stroke({
  color: `#${strokeColor.getHexString()}`,
  width: 5,
});

let style = new Style({
  fill,
  stroke,
  image,
});

const polygon = new Feature(
  new Polygon([
    [
      [100.0, 0.0],
      [101.0, 0.0],
      [101.0, 1.0],
      [100.0, 1.0],
      [100.0, 0.0],
    ],
  ]).transform("EPSG:4326", "EPSG:3857"),
);

const line = new Feature(
  new LineString([
    [102.0, 0.0],
    [103.0, 1.0],
    [104.0, 0.0],
    [105.0, 1.0],
  ]).transform("EPSG:4326", "EPSG:3857"),
);

const point = new Feature(
  new Point([102.0, 0.5]).transform("EPSG:4326", "EPSG:3857"),
);

const source = new VectorSource({
  data: [],
  dataProjection: "EPSG:3857",
  style,
});

const layer = new ColorLayer({ source });

map.addLayer(layer);

Inspector.attach("inspector", instance);

instance.notifyChange(map);

source.addFeatures([point, line, polygon]);

const [setStrokeWidth] = bindSlider("stroke-width", (v) => {
  style.getStroke().setWidth(v);
  style.getImage().getStroke().setWidth(v);
  style.getImage().setRadius(style.getImage().getRadius());
  source.update();
});
const [setPointRadius] = bindSlider("point-radius", (v) => {
  style.getImage().setRadius(v);
  style.setImage(style.getImage());
  source.update();
});
const [setOpacity] = bindSlider("style-opacity", (v) => {
  style
    .getImage()
    .getStroke()
    .setColor(
      `rgba(${strokeColor.r * 255}, ${strokeColor.g * 255}, ${strokeColor.b * 255}, ${v})`,
    );
  style
    .getImage()
    .getFill()
    .setColor(
      `rgba(${fillColor.r * 255}, ${fillColor.g * 255}, ${fillColor.b * 255}, ${v})`,
    );
  style
    .getStroke()
    .setColor(
      `rgba(${strokeColor.r * 255}, ${strokeColor.g * 255}, ${strokeColor.b * 255}, ${v})`,
    );
  style
    .getFill()
    .setColor(
      `rgba(${fillColor.r * 255}, ${fillColor.g * 255}, ${fillColor.b * 255}, ${v})`,
    );

  style.getImage().setRadius(style.getImage().getRadius());

  source.update();
});

bindToggle("show-line", (v) => {
  if (v) {
    source.addFeature(line);
  } else {
    source.removeFeature(line);
  }
  source.update();
});
bindToggle("show-polygon", (v) => {
  if (v) {
    source.addFeature(polygon);
  } else {
    source.removeFeature(polygon);
  }
  source.update();
});
bindToggle("show-point", (v) => {
  if (v) {
    source.addFeature(point);
  } else {
    source.removeFeature(point);
  }
  source.update();
});

bindButton("randomize", () => {
  strokeColor.r = MathUtils.randFloat(0, 1);
  strokeColor.g = MathUtils.randFloat(0, 1);
  strokeColor.b = MathUtils.randFloat(0, 1);

  fillColor.r = MathUtils.randFloat(0, 1);
  fillColor.g = MathUtils.randFloat(0, 1);
  fillColor.b = MathUtils.randFloat(0, 1);

  const pointRadius = MathUtils.randFloat(0.1, 20);
  const strokeWidth = MathUtils.randFloat(1, 20);
  const opacity = MathUtils.randFloat(0, 1);

  const newStyle = new Style({
    fill: new Fill({
      color: `rgba(${fillColor.r * 255}, ${fillColor.g * 255}, ${fillColor.b * 255}, ${opacity})`,
    }),
    stroke: new Stroke({
      color: `rgba(${strokeColor.r * 255}, ${strokeColor.g * 255}, ${strokeColor.b * 255}, ${opacity})`,
      width: strokeWidth,
    }),
    image: new Circle({
      radius: pointRadius,
      fill: new Fill({
        color: `rgba(${fillColor.r * 255}, ${fillColor.g * 255}, ${fillColor.b * 255}, ${opacity})`,
      }),
      stroke: new Stroke({
        color: `rgba(${strokeColor.r * 255}, ${strokeColor.g * 255}, ${strokeColor.b * 255}, ${opacity})`,
        width: strokeWidth,
      }),
    }),
  });

  setPointRadius(pointRadius);
  setStrokeWidth(strokeWidth);
  setOpacity(opacity);

  style = newStyle;

  // Here we test that setStyle() takes the new style into account
  // and that the layer is repainted.
  source.setStyle(newStyle);
});
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Dynamic layer updates</title>
    <meta charset="UTF-8" />
    <meta name="name" content="layer_update" />
    <meta
      name="description"
      content="The layer is updated when the style changes."
    />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="icon" href="https://giro3d.org/images/favicon.svg" />
    <link
      href="https://giro3d.org/assets/bootstrap-custom.css"
      rel="stylesheet"
    />
    <script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://giro3d.org/next/css/example.css"
    />
  </head>

  <body>
    <div id="view" class="m-0 p-0 w-100 h-100"></div>
    <div
      id="inspector"
      class="position-absolute top-0 start-0 mh-100 overflow-auto"
    ></div>

    <div class="side-pane-with-status-bar">
      <!-- Top color layer -->
      <div class="card">
        <div class="card-header">Options</div>

        <div class="card-body" id="top-options">
          <!-- Show point -->
          <div class="input-group my-2">
            <div>
              <div class="form-check form-switch">
                <input
                  class="form-check-input"
                  checked
                  type="checkbox"
                  role="switch"
                  id="show-point"
                  autocomplete="off"
                />
                <label class="form-check-label" for="show-point"
                  >Show point</label
                >
              </div>
            </div>
          </div>

          <!-- Show polygon -->
          <div class="input-group my-2">
            <div>
              <div class="form-check form-switch">
                <input
                  class="form-check-input"
                  checked
                  type="checkbox"
                  role="switch"
                  id="show-polygon"
                  autocomplete="off"
                />
                <label class="form-check-label" for="show-polygon"
                  >Show polygon</label
                >
              </div>
            </div>
          </div>

          <!-- Show line -->
          <div class="input-group my-2">
            <div>
              <div class="form-check form-switch">
                <input
                  class="form-check-input"
                  checked
                  type="checkbox"
                  role="switch"
                  id="show-line"
                  autocomplete="off"
                />
                <label class="form-check-label" for="show-line"
                  >Show line</label
                >
              </div>
            </div>
          </div>

          <!-- Stroke width -->
          <div class="input-group my-2">
            <label for="stroke-width" class="form-label">Stroke width</label>
            <div class="input-group">
              <input
                type="range"
                min="1"
                step="1"
                max="20"
                value="5"
                class="form-range"
                id="stroke-width"
                autocomplete="off"
              />
            </div>
          </div>

          <!-- Point radius -->
          <div class="input-group my-2">
            <label for="point-radius" class="form-label">Point radius</label>
            <div class="input-group">
              <input
                type="range"
                min="0.1"
                step="0.05"
                max="100"
                value="20"
                class="form-range"
                id="point-radius"
                autocomplete="off"
              />
            </div>
          </div>

          <!-- Style opacity -->
          <div class="input-group my-2">
            <label for="style-opacity" class="form-label">Style opacity</label>
            <div class="input-group">
              <input
                type="range"
                min="0"
                step="0.05"
                max="1"
                value="1"
                class="form-range"
                id="style-opacity"
                autocomplete="off"
              />
            </div>
          </div>

          <hr />

          <!-- Randomize style -->
          <button type="button" class="btn btn-primary w-100" id="randomize">
            Randomize style
          </button>
        </div>
      </div>
    </div>

    <script type="module" src="index.js"></script>
    <script>
      /* activate popovers */
      const popoverTriggerList = [].slice.call(
        document.querySelectorAll('[data-bs-toggle="popover"]'),
      );
      popoverTriggerList.map(
        // bootstrap is used as script in the template, disable warning about undef
        // eslint-disable-next-line no-undef
        (popoverTriggerEl) =>
          new bootstrap.Popover(popoverTriggerEl, {
            trigger: "hover",
            placement: "left",
            content: document.getElementById(
              popoverTriggerEl.getAttribute("data-bs-content"),
            ).innerHTML,
            html: true,
          }),
      );
    </script>
  </body>
</html>
package.json
{
    "name": "layer_update",
    "dependencies": {
        "@giro3d/giro3d": "git+https://gitlab.com/giro3d/giro3d.git"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}