Layers · Angular, React and OpenLayers

Change the opacity of a layer

Cross-fade OpenStreetMap over Kraków’s aerial orthophotomap using a reactive range slider bound to the opacity input of aol-layer-tile.

The orthophotomap of central Kraków with OpenStreetMap over it at half opacity, and the layer opacity slider at 0.50 below the map.
A screenshot of the example. Orthophotomap © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

Central Kraków renders with Poland’s national aerial orthophotomap as the bottom base layer and OpenStreetMap street tiles stacked directly above it at 50% transparency. Dragging the range slider beneath the viewport adjusts street-layer opacity smoothly from 0.00 (pure aerial imagery) to 1.00 (opaque street map) with a live two-decimal read-out.

When you need it

Analysts comparing parcel boundaries, road alignments or utility plans against aerial photography constantly blend overlaid layers rather than toggling them abruptly on and off.

A continuous opacity slider lets users dial in the exact balance where street names and house numbers remain legible without hiding roof outlines and vegetation underneath.

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.

layer-opacity.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  SourceTileWMTSComponent,
  TileGridWMTSComponent,
  ViewComponent,
} from 'cc-openlayers';
import { getTopLeft, getWidth } from 'ol/extent';
import { EXTENT } from 'ol/proj/epsg3857';

const size = getWidth(EXTENT) / 256;

/** OpenStreetMap over the national orthophoto, its opacity set by a slider. */
@Component({
  selector: 'app-layer-opacity',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    SourceTileWMTSComponent,
    TileGridWMTSComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="13"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile>
        <aol-source-tilewmts
          attributions='Tiles © <a href="https://www.geoportal.gov.pl/" target="_blank">GUGiK</a>'
          url="https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/StandardResolution"
          layer="ORTOFOTOMAPA"
          matrixSet="EPSG:3857"
          format="image/jpeg"
          projection="EPSG:3857"
          [style]="'default'"
          [wrapX]="true"
        >
          <aol-tilegrid-wmts [origin]="origin" [resolutions]="resolutions" [matrixIds]="matrixIds" />
        </aol-source-tilewmts>
      </aol-layer-tile>
      <aol-layer-tile [opacity]="opacity()"><aol-source-osm /></aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      <label>
        Layer opacity
        <input type="range" min="0" max="1" step="0.01" [value]="opacity()" (input)="opacity.set(number($event))" />
        <output>{{ opacity().toFixed(2) }}</output>
      </label>
    </div>
  `,
})
export class LayerOpacity {
  readonly opacity = signal(1);
  protected readonly origin = getTopLeft(EXTENT);
  protected readonly resolutions = Array.from({ length: 20 }, (_, z) => size / 2 ** z);
  protected readonly matrixIds = Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`);

  protected number(event: Event): number {
    return (event.target as HTMLInputElement).valueAsNumber;
  }
}

Architecture and OpenLayers mechanics

Layers declared sequentially inside the map template draw in document order: the orthophotomap’s aol-layer-tile paints first, and the OpenStreetMap aol-layer-tile paints over it with [opacity] driven by a signal.

Listening to the range slider’s continuous input event writes valueAsNumber directly into the signal on every pointer tick. Because OpenLayers quantises layer alpha to two decimal places internally, a slider step of 0.01 matches the finest effective increment and toFixed(2) formats the numeric label cleanly.

How it works

  • The orthophotomap mounts as the first aol-layer-tile on the bottom while the OpenStreetMap layer above it binds [opacity] to a reactive signal.
  • Reading valueAsNumber on every slider input event updates transparency smoothly while dragging rather than waiting for mouse release.
  • A step of 0.01 matches OpenLayers’ internal two-decimal opacity rounding, displayed alongside the control via toFixed(2).
  • Standard raster tile layers apply alpha per tile draw call without allocating an extra off-screen compositing canvas.

Pitfalls

  • Setting [opacity] to 0 makes a layer completely transparent visually, yet OpenLayers still fetches and decodes every tile in view—bind [visible] to false whenever a 0% layer should also stop network requests.
  • During the default 250 ms tile fade-in after zooming, OpenLayers briefly draws incoming tiles over their coarser parent tiles, making translucent layers look slightly denser for a quarter-second unless source tile transitions are set to 0.

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.