Illustrates various types of opacity (map, map background, and layer)

100%
index.js
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';

// Defines geographic extent: CRS, min/max X, min/max Y
const extent = new Extent(
    'EPSG:3857',
    -4553934 - 1000000,
    -4553934 + 1000000,
    -3910697 - 1000000,
    -3910697 + 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: false,
    },
});

// Instanciates camera
instance.camera.camera3D.position.set(-4553934, -3910697, 4600000);

// Instanciates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
controls.target = new Vector3(-4553934, -3910696, 0);
controls.enableDamping = true;
controls.dampingFactor = 0.25;

instance.useTHREEControls(controls);

const map = new Map('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: 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(document.getElementById('panelDiv'), instance);

instance.notifyChange(map);

// GUI
function bindSlider(id, action) {
    const slider = document.getElementById(id);
    slider.oninput = () => {
        action(slider.value);
        instance.notifyChange(map);
    };
}

bindSlider('map-opacity', v => {
    map.opacity = v;
});
bindSlider('bg-opacity', v => {
    map.materialOptions.backgroundOpacity = v;
});
bindSlider('blue-opacity', v => {
    blueTriangle.opacity = v;
});
bindSlider('red-opacity', v => {
    redSquare.opacity = v;
});
index.html
<!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/css/example.css"
        />

        <style>
        #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" 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">
            <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>
package.json
{
    "name": "transparent_map_bg",
    "dependencies": {
        "@giro3d/giro3d": "0.37.3"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}