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.

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.tsimport { 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;
}
} 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.
layer-opacity.react.tsximport { useState, type ChangeEvent } from 'react';
import { getTopLeft, getWidth } from 'ol/extent';
import { EXTENT } from 'ol/proj/epsg3857';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
View,
WMTSSource,
useWMTSTileGrid,
} from 'cc-openlayers/react';
const size = getWidth(EXTENT) / 256;
const origin = getTopLeft(EXTENT);
const resolutions = Array.from({ length: 20 }, (_, z) => size / 2 ** z);
const matrixIds = Array.from({ length: 20 }, (_, z) => `EPSG:3857:${z}`);
function number(event: ChangeEvent<HTMLInputElement>): number {
return event.target.valueAsNumber;
}
/** OpenStreetMap over the national orthophoto, its opacity set by a slider. */
export function LayerOpacity() {
const [opacity, setOpacity] = useState(1);
const tileGrid = useWMTSTileGrid({ origin, resolutions, matrixIds });
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<WMTSSource
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}
tileGrid={tileGrid}
/>
</TileLayer>
<TileLayer opacity={opacity}>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<label>
Layer opacity
<input
type="range"
min="0"
max="1"
step="0.01"
value={opacity}
onChange={(event) => setOpacity(number(event))}
/>
<output>{opacity.toFixed(2)}</output>
</label>
</div>
</>
);
}How the React version works
OpenStreetMap lies over GUGiK's orthophoto, and a slider changes its opacity from 0 to 1. It is the simplest example of a layer prop driven by component state.
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. Both versions compute the WMTS tile grid of GUGiK's orthophoto (origin, 20 resolutions, ids EPSG:3857:z) in the same way; in React the useWMTSTileGrid hook creates it once from module constants.
From Angular to React
| Angular | React |
|---|---|
signal(1), (input)="opacity.set(...)" | useState(1), the slider's onChange |
[opacity]="opacity()" on aol-layer-tile | opacity={opacity} on TileLayer |
<aol-source-tilewmts> with <aol-tilegrid-wmts> inside | <WMTSSource tileGrid={tileGrid}> with useWMTSTileGrid(...) |
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. A change of opacity calls layer.setOpacity on the same layer.
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-tileon the bottom while the OpenStreetMap layer above it binds[opacity]to a reactive signal. - Reading
valueAsNumberon every sliderinputevent updates transparency smoothly while dragging rather than waiting for mouse release. - A
stepof 0.01 matches OpenLayers’ internal two-decimal opacity rounding, displayed alongside the control viatoFixed(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.
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.