Illustrates the use of mask layers.
A MaskLayer
can be used to mask part of a map. By default, the transparent part of the mask makes the map transparent. By inverting the mask, the transparent part of the mask makes the map opaque.
import XYZ from "ol/source/XYZ.js";
import { Fill, Stroke, Style } from "ol/style.js";
import { GeoJSON } from "ol/format.js";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import TiledImageSource from "@giro3d/giro3d/sources/TiledImageSource.js";
import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import MaskLayer, { MaskMode } from "@giro3d/giro3d/core/layer/MaskLayer.js";
import VectorSource from "@giro3d/giro3d/sources/VectorSource.js";
function bindNumericalDropDown(id, onChange) {
const element = document.getElementById(id);
if (!(element instanceof HTMLSelectElement)) {
throw new Error(
"invalid binding element: expected HTMLSelectElement, got: " +
element.constructor.name,
);
}
element.onchange = () => {
onChange(parseInt(element.value));
};
const callback = (v) => {
element.value = v.toString();
onChange(parseInt(element.value));
};
return [callback, parseInt(element.value), element];
}
const extent = Extent.fromCenterAndSize(
"EPSG:3857",
{ x: 260000, y: 6251379 },
32000,
32000,
);
const instance = new Instance({
target: "view",
crs: extent.crs,
backgroundColor: null,
});
const apiKey =
"pk.eyJ1IjoidG11Z3VldCIsImEiOiJjbGJ4dTNkOW0wYWx4M25ybWZ5YnpicHV6In0.KhDJ7W5N3d1z3ArrsDjX_A";
const map = new Map({ extent });
instance.add(map);
// Adds a satellite basemap
const basemap = new ColorLayer({
name: "basemap",
extent,
source: new TiledImageSource({
source: new XYZ({
url: `https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.webp?access_token=${apiKey}`,
projection: extent.crs,
crossOrigin: "anonymous",
}),
}),
});
map.addLayer(basemap);
const outlineStyle = new Style({
stroke: new Stroke({ color: "red", width: 2 }),
});
// Display the footprint using a red outline. This layer is not necessary for the mask to work,
// and is only present for illustration purposes.
const outline = new ColorLayer({
name: "outline",
source: new VectorSource({
data: {
url: "https://3d.oslandia.com/giro3d/vectors/paris.geojson",
format: new GeoJSON(),
},
style: outlineStyle,
}),
});
map.addLayer(outline);
// The mask layer uses an opaque fill style.
const maskStyle = new Style({
fill: new Fill({ color: "white" }),
});
// Create the actual mask layer with the same source as the outline.
const mask = new MaskLayer({
name: "mask",
source: new VectorSource({
data: {
url: "https://3d.oslandia.com/giro3d/vectors/paris.geojson",
format: new GeoJSON(),
},
style: maskStyle,
}),
});
map.addLayer(mask);
const center = extent.centerAsVector3();
instance.view.camera.position.set(center.x, center.y - 1, 40000);
const controls = new MapControls(instance.view.camera, instance.domElement);
controls.target = center;
controls.saveState();
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.maxPolarAngle = Math.PI / 2.3;
instance.view.setControls(controls);
Inspector.attach("inspector", instance);
bindNumericalDropDown("layerState", (newMode) => {
switch (newMode) {
case 1:
mask.visible = true;
mask.maskMode = MaskMode.Normal;
break;
case 2:
mask.visible = true;
mask.maskMode = MaskMode.Inverted;
break;
default:
mask.visible = false;
break;
}
instance.notifyChange(map);
});
<!doctype html>
<html lang="en">
<head>
<title>Mask layers</title>
<meta charset="UTF-8" />
<meta name="name" content="mask_layer" />
<meta name="description" content="Illustrates the use of mask layers." />
<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"
/>
<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">
<h5 class="card-header">Mask layer</h5>
<div class="card-body">
<form>
<div class="row">
<div class="dropdown">
<select
class="btn btn-primary dropdown-toggle"
id="layerState"
autocomplete="off"
>
<option value="0">Disabled</option>
<option value="1" selected>Enabled</option>
<option value="2">Enabled (invert mask)</option>
</select>
</div>
</div>
</form>
</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": "mask_layer",
"dependencies": {
"@giro3d/giro3d": "0.39.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}