Options
The layer is updated when the style changes.
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';
/**
* Binds a button.
* @param {string} id The id of the <button> element.
* @param {() => void} onClick The click handler.
*/
function bindButton(id, onClick) {
document.getElementById(id).onclick = () => {
onClick();
};
}
/**
* Binds a toggle switch or checkbox.
* @param {string} id The id of the <input> element.
* @param {(v: boolean) => void} onChange The callback when the dropdown value changes.
* @returns {(v: boolean) => void} The function to update the value from outside.
*/
function bindToggle(id, onChange) {
/** @type {HTMLInputElement} */
const toggle = document.getElementById(id);
toggle.oninput = function oninput() {
onChange(toggle.checked);
};
return v => {
toggle.checked = v;
onChange(toggle.checked);
};
}
/**
* Binds a {@link HTMLInputElement} in slider mode.
* @param {string} id The id of the <input> element.
* @param {(v: number) => void} onChange The callback when the slider value changes.
* @returns {(v: number) => void} The function to update the value from outside.
*/
function bindSlider(id, onChange) {
/** @type {HTMLInputElement} */
const slider = document.getElementById(id);
slider.oninput = function oninput() {
onChange(slider.valueAsNumber);
};
return v => {
slider.valueAsNumber = v;
onChange(slider.valueAsNumber);
};
}
// Defines geographic extent: CRS, min/max X, min/max Y
const extent = Extent.fromCenterAndSize('EPSG:3857', { x: 11393552, y: 44035 }, 1000000, 500000);
// `viewerDiv` will contain Giro3D' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');
// Creates a Giro3D instance
const instance = new Instance(viewerDiv, {
crs: extent.crs(),
renderer: {
clearColor: 0xffffff,
},
});
// Instanciates camera
const center = extent.centerAsVector3();
instance.camera.camera3D.position.set(center.x, center.y - 1, 1000000);
// Creates controls
const controls = new MapControls(instance.camera.camera3D, viewerDiv);
// Then looks at extent's center
controls.target = center;
controls.saveState();
instance.useTHREEControls(controls);
const map = new Map('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(document.getElementById('panelDiv'), instance);
instance.notifyChange(map);
source.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());
layer.source.update();
});
const setPointRadius = bindSlider('point-radius', v => {
style.getImage().setRadius(v);
style.setImage(style.getImage());
layer.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());
layer.source.update();
});
bindToggle('show-line', v => {
if (v) {
source.source.addFeature(line);
} else {
source.source.removeFeature(line);
}
source.update();
});
bindToggle('show-polygon', v => {
if (v) {
source.source.addFeature(polygon);
} else {
source.source.removeFeature(polygon);
}
source.update();
});
bindToggle('show-point', v => {
if (v) {
source.source.addFeature(point);
} else {
source.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);
});
<!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/latest/css/example.css"
/>
</head>
<body>
<div id="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
<div id="panelDiv" 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="10"
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="20"
value="1.5"
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>
{
"name": "layer_update",
"dependencies": {
"@giro3d/giro3d": "0.37.3"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}