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.

The shaded relief of the region around Kraków at 60 % opacity over a black and white street map, the Vistula valley between the uplands and the Carpathian foothills.
A screenshot of the example. Shaded relief © GUGiK. Map data © OpenStreetMap contributors.

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.ts
import { 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 {}

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, which ViewEncapsulation.None styles 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] to 0.6 blends transparent PNG hillshade tiles smoothly over the monochrome street network.
  • Setting [transition] to 0 on 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 className container 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.

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.