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';
// Defines geographic extent: CRS, min/max X, min/max Y
const extent = Extent.fromCenterAndSize('EPSG:3857', { x: 260000, y: 6251379 }, 32000, 32000);
// `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: false,
},
});
const apiKey =
'pk.eyJ1IjoidG11Z3VldCIsImEiOiJjbGJ4dTNkOW0wYWx4M25ybWZ5YnpicHV6In0.KhDJ7W5N3d1z3ArrsDjX_A';
// Adds the map that will contain the layers.
const map = new Map('Paris', { extent, segments: 128 });
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({
format: new GeoJSON(),
data: 'https://3d.oslandia.com/giro3d/vectors/paris.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({
format: new GeoJSON(),
data: 'https://3d.oslandia.com/giro3d/vectors/paris.geojson',
style: maskStyle,
}),
});
map.addLayer(mask);
// Sets the camera position
const center = extent.centerAsVector3();
instance.camera.camera3D.position.set(center.x, center.y - 1, 40000);
// Creates controls
const controls = new MapControls(instance.camera.camera3D, viewerDiv);
// Then looks at extent's center
controls.target = center;
controls.saveState();
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.maxPolarAngle = Math.PI / 2.3;
instance.useTHREEControls(controls);
Inspector.attach(document.getElementById('panelDiv'), instance);
// Bind events
document.getElementById('layerState').addEventListener('change', e => {
const newMode = parseInt(e.target.value, 10);
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>
<meta charset="UTF-8">
<title>Giro3D - Mask layers</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;
}
#viewerDiv 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="viewerDiv"></div>
<div id="panelDiv"></div>
<div class="m-2 position-absolute top-0 end-0">
<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">
<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.35.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}