The layer is updated when the style changes.
import { Color, MathUtils } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import { GeoJSON } from 'ol/format.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';
// Defines geographic extent: CRS, min/max X, min/max Y
const extent = Extent.fromCenterAndSize('EPSG:3857', { x: 11393552, y: 44035 }, 1000000, 1000000);
// `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);
// Creates a custom vector layer
const features = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [102.0, 0.5],
},
properties: {
prop0: 'value0',
},
},
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[102.0, 0.0],
[103.0, 1.0],
[104.0, 0.0],
[105.0, 1.0],
],
},
properties: {
prop0: 'value0',
prop1: 0.0,
},
},
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0],
],
],
},
properties: {
prop0: 'value0',
prop1: { this: 'that' },
},
},
],
};
const style = new Style({
fill: new Fill({
color: 'cyan',
}),
stroke: new Stroke({
color: 'orange',
width: 5,
}),
image: new Circle({
radius: 20,
fill: new Fill({
color: 'orange',
}),
stroke: new Stroke({
color: 'black',
width: 5,
}),
}),
});
const source = new VectorSource({
format: new GeoJSON(),
data: features,
dataProjection: 'EPSG:4326',
style,
});
const layer = new ColorLayer({
name: 'geojson',
extent,
source,
});
map.addLayer(layer);
Inspector.attach(document.getElementById('panelDiv'), instance);
instance.notifyChange(map);
function bindSlider(id, fn) {
const slider = document.getElementById(id);
slider.oninput = function oninput() {
fn(slider.value);
layer.source.update();
};
}
let currentStyle = style;
bindSlider('thicknessSlider', v => currentStyle.getStroke().setWidth(v));
bindSlider('iconScaleSlider', v => currentStyle.getImage().setRadius(v));
bindSlider('iconOpacitySlider', v => currentStyle.getImage().setOpacity(v));
const button = document.getElementById('changeStyleBtn');
button.onclick = () => {
function randomColor() {
const color = new Color(
MathUtils.randFloat(0, 1),
MathUtils.randFloat(0, 1),
MathUtils.randFloat(0, 1),
);
return `#${color.getHexString()}`;
}
const newStyle = new Style({
fill: new Fill({
color: randomColor(),
}),
stroke: new Stroke({
color: randomColor(),
width: MathUtils.randInt(1, 20),
}),
image: new Circle({
radius: 20,
fill: new Fill({
color: randomColor(),
}),
stroke: new Stroke({
color: randomColor(),
width: MathUtils.randInt(1, 10),
}),
}),
});
// Here we test that setStyle() takes the new style into account
// and that the layer is repainted.
source.setStyle(newStyle);
currentStyle = newStyle;
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Giro3D - Dynamic layer updates</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<style>
body {
padding: 0;
margin: 0;
width: 100vw;
height: 100vh;
}
#viewerDiv {
width: 100%;
height: 100%;
}
#panelDiv {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="viewerDiv"></div>
<div id="panelDiv"></div>
<div class="m-2 position-absolute top-0 end-0">
<div class="card m-1">
<fieldset class="container m-1" id="styleOptions">
<label for="thicknessSlider" class="form-label">Line thickness</label>
<div class="input-group">
<input
type="range"
min="0"
max="10"
value="1"
class="slider"
id="thicknessSlider"
/>
</div>
<div class="my-2"></div>
<label for="iconScaleSlider" class="form-label">Icon radius</label>
<div class="input-group">
<input
type="range"
min="1"
max="100"
value="1"
step="1"
class="slider"
id="iconScaleSlider"
/>
</div>
<label for="iconOpacitySlider" class="form-label">Icon opacity</label>
<div class="input-group">
<input
type="range"
min="0"
max="1"
value="1"
step="0.05"
class="slider"
id="iconOpacitySlider"
/>
</div>
<div class="my-4"></div>
<!-- Assign new style -->
<button type="button" class="btn btn-primary" id="changeStyleBtn">
Assign random style
</button>
</fieldset>
</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.35.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}