Illustrates various types of opacity (map, map background, and layer)
import { Vector3 } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";
import { Fill, Style } from "ol/style.js";
import GeoJSON from "ol/format/GeoJSON.js";
import VectorSource from "@giro3d/giro3d/sources/VectorSource.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 Inspector from "@giro3d/giro3d/gui/Inspector.js";
import { bindSlider } from "./widgets/bindSlider";
const extent = new Extent(
"EPSG:3857",
-4553934 - 1000000,
-4553934 + 1000000,
-3910697 - 1000000,
-3910697 + 1000000,
);
const instance = new Instance({
target: "view",
crs: extent.crs,
backgroundColor: null,
});
instance.view.camera.position.set(-4553934, -3910697, 4600000);
const controls = new MapControls(instance.view.camera, instance.domElement);
controls.target = new Vector3(-4553934, -3910696, 0);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
instance.view.setControls(controls);
const map = new Map({ extent, backgroundColor: "green" });
instance.add(map);
const rectangle = {
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[-46, -30],
[-41, -30],
[-41, -35],
[-46, -35],
[-46, -30],
],
],
},
};
const triangle = {
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[-45, -31],
[-39, -31],
[-39, -35],
[-45, -31],
],
],
},
};
function makeGeoJSONLayer(name, geojson, color) {
const style = new Style({
fill: new Fill({
color,
}),
});
const source = new VectorSource({
data: {
content: geojson,
format: new GeoJSON(),
},
style,
dataProjection: "EPSG:4326",
});
const layer = new ColorLayer({
name,
extent,
source,
});
return layer;
}
const redSquare = makeGeoJSONLayer("redSquare", rectangle, "#aa0000");
const blueTriangle = makeGeoJSONLayer("blueTriangle", triangle, "#0000aa");
map.addLayer(redSquare);
map.addLayer(blueTriangle);
Inspector.attach("inspector", instance);
instance.notifyChange(map);
// GUI
bindSlider("map-opacity", (v) => {
map.opacity = v;
instance.notifyChange(map);
});
bindSlider("bg-opacity", (v) => {
map.backgroundOpacity = v;
instance.notifyChange(map);
});
bindSlider("blue-opacity", (v) => {
blueTriangle.opacity = v;
instance.notifyChange(map);
});
bindSlider("red-opacity", (v) => {
redSquare.opacity = v;
instance.notifyChange(map);
});
<!doctype html>
<html lang="en">
<head>
<title>Types of opacity</title>
<meta charset="UTF-8" />
<meta name="name" content="transparent_map_bg" />
<meta
name="description"
content="Illustrates various types of opacity (map, map background, and layer)"
/>
<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/examples/css/example.css"
/>
<style>
#view canvas {
background-image: linear-gradient(45deg, #aaaaaa 25%, transparent 25%),
linear-gradient(-45deg, #aaaaaa 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #aaaaaa 75%),
linear-gradient(-45deg, transparent 75%, #aaaaaa 75%);
background-size: 20px 20px;
background-position:
0 0,
0 10px,
10px -10px,
-10px 0px;
}
</style>
</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">
<div class="card">
<div class="card-body">
<!-- Map global opacity -->
<label for="map-opacity" class="form-label">Map opacity</label>
<div class="input-group">
<input
type="range"
min="0"
max="1"
value="1"
step="0.05"
class="form-range"
id="map-opacity"
autocomplete="off"
/>
</div>
<div class="my-2"></div>
<!-- Map background opacity -->
<label for="bg-opacity" class="form-label"
>Map background opacity</label
>
<div class="input-group">
<input
type="range"
min="0"
max="1"
value="1"
step="0.05"
class="form-range"
id="bg-opacity"
autocomplete="off"
/>
</div>
<div class="my-2"></div>
<!-- Blue layer opacity -->
<label for="blue-opacity" class="form-label"
>Blue layer opacity</label
>
<div class="input-group">
<input
type="range"
min="0"
max="1"
value="1"
step="0.05"
class="form-range"
id="blue-opacity"
autocomplete="off"
/>
</div>
<div class="my-2"></div>
<!-- Red layer opacity -->
<label for="red-opacity" class="form-label">Red layer opacity</label>
<div class="input-group">
<input
type="range"
min="0"
max="1"
value="1"
step="0.05"
class="form-range"
id="red-opacity"
autocomplete="off"
/>
</div>
</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": "transparent_map_bg",
"dependencies": {
"@giro3d/giro3d": "0.41.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}