Layers · Angular, React and OpenLayers
A semi-transparent layer over a grey map
Overlay translucent GUGiK shaded terrain relief at 60% opacity over a CSS-desaturated grayscale OpenStreetMap base layer in OpenLayers.

What this example shows
Around Kraków—where the Vistula valley cuts between the limestone uplands and the Carpathian foothills—GUGiK’s shaded elevation relief renders at 60% opacity over an OpenStreetMap street backdrop turned crisp black-and-white via a CSS filter.
Desaturating only the street layer eliminates competing green forest and yellow road fills, letting subtle hillshade ridges and street names remain simultaneously readable without post-zoom brightness flashes.
When you need it
Environmental thematic layers—shaded relief, flood hazard zones, acoustic noise contours and heatmaps—lose legibility rapidly when draped over brightly coloured standard street maps.
Turning an existing street tile service monochrome in the browser with one CSS rule gives you an instant neutral cartographic backdrop without hosting a separate grayscale tile pyramid.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
semi-transparent-layer.css/* The street map's layer has the class bw instead of ol-layer, so it has a canvas of its own and only it turns grey. */
app-semi-transparent-layer .bw {
filter: grayscale(100%);
}semi-transparent-layer.tsimport { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
SourceTileWMSComponent,
ViewComponent,
} from 'cc-openlayers';
/** A semi-transparent shaded relief over an OpenStreetMap background drawn in black and white. */
@Component({
selector: 'app-semi-transparent-layer',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
SourceTileWMSComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
// The rule styles the layer element that OpenLayers creates, outside this component's template.
encapsulation: ViewEncapsulation.None,
styleUrl: './semi-transparent-layer.css',
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="10">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile className="bw"><aol-source-osm /></aol-layer-tile>
<aol-layer-tile [opacity]="0.6">
<!-- the layer is transparent, so its tiles do not fade in over the ones they replace -->
<aol-source-tilewms
url="https://mapy.geoportal.gov.pl/wss/service/PZGIK/NMT/GRID1/WMS/ShadedRelief"
[params]="{ LAYERS: 'Raster', FORMAT: 'image/png', TRANSPARENT: 'TRUE' }"
crossOrigin="anonymous"
[transition]="0"
attributions="Shaded relief © GUGiK"
/>
</aol-layer-tile>
</aol-map>
<div class="example-controls">
<p class="example-hint">
The relief layer is drawn at 60 % opacity; a CSS filter turns the OpenStreetMap layer black and white.
</p>
</div>
`,
})
export class SemiTransparentLayer {} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
semi-transparent-layer.react.tsximport {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
TileWMSSource,
View,
} from 'cc-openlayers/react';
const reliefParams = { LAYERS: 'Raster', FORMAT: 'image/png', TRANSPARENT: 'TRUE' };
/** A semi-transparent shaded relief over an OpenStreetMap background drawn in black and white. */
export function SemiTransparentLayer() {
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={10}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer className="bw">
<OSMSource />
</TileLayer>
<TileLayer opacity={0.6}>
{/* the layer is transparent, so its tiles do not fade in over the ones they replace */}
<TileWMSSource
url="https://mapy.geoportal.gov.pl/wss/service/PZGIK/NMT/GRID1/WMS/ShadedRelief"
params={reliefParams}
crossOrigin="anonymous"
transition={0}
attributions="Shaded relief © GUGiK"
/>
</TileLayer>
</Map>
<div className="example-controls">
<p className="example-hint">
The relief layer is drawn at 60 % opacity; a CSS filter turns the OpenStreetMap layer black and white.
</p>
</div>
</>
);
}How the React version works
GUGiK's shaded relief from a WMS lies at 60 % opacity over OpenStreetMap, which a CSS filter draws in black and white. The relief tiles appear at once (transition: 0), because they are transparent.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. The CSS rule for the bw class is in the gallery stylesheet both versions use; the React file imports nothing.
From Angular to React
| Angular | React |
|---|---|
className="bw" on aol-layer-tile | className="bw" on TileLayer |
<aol-source-tilewms [params]> | <TileWMSSource params={reliefParams}> with a module constant |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
Architecture and OpenLayers mechanics
By default OpenLayers composites adjacent layers of the same className onto a shared <canvas> element for performance. Assigning a custom className (bw) to the OpenStreetMap layer forces OpenLayers to allocate a dedicated DOM wrapper and canvas for the base map alone, while ViewEncapsulation.None lets component CSS apply filter: grayscale(100%) strictly to .bw without greying out layers above it.
Above that monochrome canvas, the WMS shaded-relief layer renders into OpenLayers’ default layer container at [opacity] 0.6 with source [transition] set to 0—preventing translucent incoming tiles from briefly doubling up over outgoing parent tiles after a zoom.
How it works
- Giving the street layer a custom
className(bw) isolates it into its own DOM container, whichViewEncapsulation.Nonestyles with a cross-browser CSS grayscale filter. - Leaving the relief layer on the default class places it on a separate upper canvas untouched by the base map’s CSS filter.
- Setting
[opacity]to0.6blends transparent PNG hillshade tiles smoothly over the monochrome street network. - Setting
[transition]to0on the relief source disables tile fade-in so translucent tiles never flash dark over coarser parent tiles after zooming.
Pitfalls
- Applying context.filter inside a canvas prerender hook instead of a CSS rule on a dedicated
classNamecontainer fails in browser engines where 2D canvas context filters are disabled or unsupported, whereas CSS element filters work reliably across Chrome, Firefox and Safari. - If you forget to give the filtered layer its own
className, OpenLayers merges subsequent layers onto the same canvas and turns your colourful thematic overlays grey as well.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.