Essentials · Angular, React and OpenLayers
A minimum zoom that follows the width of the map
Compute OpenLayers minZoom dynamically from viewport width on initial render and resize so a world map fills every screen on crisp integer zoom levels.

What this example shows
A world overview centred on Kraków highlights Poland in red and locks its furthest zoom-out floor to the smallest integer zoom level at which a single 256-pixel Web Mercator world fills the container width. Resizing the browser window narrower or wider recomputes that floor dynamically so the user can never zoom out into empty grey margins or repeated side-by-side continents.
When you need it
Global choropleths, aviation trackers, climate dashboards and worldwide store locators need a responsive “whole world” zoom floor that adapts cleanly from mobile phones to ultrawide desktop monitors.
A hardcoded minimum zoom of 1 or 2 breaks across responsive breakpoints—either clipping continents on narrow screens or shrinking Earth into a tiny strip surrounded by repeated worlds on wide displays. Deriving the limit from actual pixel width guarantees edge-to-edge coverage on sharp whole tile levels.
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.
min-zoom.tsimport { ChangeDetectionStrategy, Component, ElementRef, afterNextRender, signal, viewChild } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceGeoJSONComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
/** A view whose minimum zoom keeps one world in sight, recomputed when the window is resized; Poland is marked on it. */
@Component({
selector: 'app-min-zoom',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceGeoJSONComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { '(window:resize)': 'measure()' },
template: `
<div #viewport>
@if (minZoom() !== undefined) {
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="initialZoom()" [minZoom]="minZoom()">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector [style]="poland">
<aol-source-geojson
url="assets/poland/poland.geojson"
featureProjection="EPSG:3857"
attributions="Boundaries: PRG, GUGiK"
/>
</aol-layer-vector>
</aol-map>
}
</div>
<div class="example-controls">
<p class="example-hint">Zoom out as far as you can, then resize the window: the limit follows its width.</p>
</div>
`,
})
export class MinZoom {
/** Poland in the red of its flag, to stand out on the world map. */
protected readonly poland = { 'fill-color': 'rgba(220, 20, 60, 0.5)', 'stroke-color': '#dc143c', 'stroke-width': 1 };
protected readonly minZoom = signal<number | undefined>(undefined);
protected readonly initialZoom = signal(0);
private readonly viewport = viewChild.required<ElementRef<HTMLElement>>('viewport');
constructor() {
afterNextRender(() => {
this.initialZoom.set(this.zoomForWidth());
this.measure();
});
}
protected measure(): void {
this.minZoom.set(this.zoomForWidth());
}
/** The zoom at which the world, 256 pixels wide at zoom 0, fills the width of the page. */
private zoomForWidth(): number {
return Math.max(0, Math.ceil(Math.log2(this.viewport().nativeElement.clientWidth / 256)));
}
} 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.
min-zoom.react.tsximport { useEffect, useRef, useState } from 'react';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
GeoJSONSource,
Map,
OSMSource,
TileLayer,
VectorLayer,
View,
} from 'cc-openlayers/react';
/** Poland in the red of its flag, to stand out on the world map. */
const poland = { 'fill-color': 'rgba(220, 20, 60, 0.5)', 'stroke-color': '#dc143c', 'stroke-width': 1 };
/** A view whose minimum zoom keeps one world in sight, recomputed when the window is resized; Poland is marked on it. */
export function MinZoom() {
const [minZoom, setMinZoom] = useState<number | undefined>(undefined);
const [initialZoom, setInitialZoom] = useState(0);
const viewport = useRef<HTMLDivElement>(null);
useEffect(() => {
/** The zoom at which the world, 256 pixels wide at zoom 0, fills the width of the page. */
const zoomForWidth = () =>
Math.max(0, Math.ceil(Math.log2((viewport.current as HTMLDivElement).clientWidth / 256)));
const measure = () => setMinZoom(zoomForWidth());
setInitialZoom(zoomForWidth());
measure();
window.addEventListener('resize', measure);
return () => window.removeEventListener('resize', measure);
}, []);
return (
<>
<div ref={viewport}>
{minZoom !== undefined && (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={initialZoom} minZoom={minZoom}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer style={poland}>
<GeoJSONSource
url="assets/poland/poland.geojson"
featureProjection="EPSG:3857"
attributions="Boundaries: PRG, GUGiK"
/>
</VectorLayer>
</Map>
)}
</div>
<div className="example-controls">
<p className="example-hint">Zoom out as far as you can, then resize the window: the limit follows its width.</p>
</div>
</>
);
}How the React version works
The view's minimum zoom is computed from the element's width so that the world always fills the map, and computed again when the window is resized. Poland is outlined on the world map.
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. A GeoJSON source reads Poland's outline with a flat style object.
From Angular to React
| Angular | React |
|---|---|
afterNextRender(() => ...) | useEffect(() => ..., []) |
host: { '(window:resize)': 'measure()' } | window.addEventListener('resize', measure) in an effect, removed in its cleanup |
viewChild.required<ElementRef>('viewport') | useRef<HTMLDivElement>(null) |
@if (minZoom() !== undefined) | {minZoom !== undefined && (...)} |
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. The map is created only after the first measurement, as in Angular; the resize listener lives as long as the component.
Architecture and OpenLayers mechanics
In a standard Web Mercator tile pyramid, zoom level 0 is 256 CSS pixels wide and each level doubles that width. Dividing the measured container width by 256, taking Math.log2 and rounding up with Math.ceil yields the first whole zoom level where one world is at least as wide as the viewport.
Because layout width is unavailable before initial DOM paint, measurement runs inside afterNextRender on startup, writes into a signal bound to [minZoom]="minZoom()", and refreshes through a (window:resize) host listener.
How it works
- Since Web Mercator is 256 pixels wide at zoom 0 and doubles per level,
Math.ceilofMath.log2of (width / 256) gives the lowest integer zoom that fills the map width. [minZoom]="minZoom()"binds the dynamic limit to the view while(window:resize)updates it whenever the browser window changes size.afterNextRenderdefers the initial DOM width measurement until layout is complete and starts the view at that computed floor.- Rounding up to an integer level keeps base-map tiles at native 1:1 resolution instead of stopping at a blurry fractional scale.
Pitfalls
- Window resize listeners do not fire when a collapsible sidebar or split-pane divider resizes the map container without changing the browser window; attach a ResizeObserver to the host element in panel-based layouts.
- When a view enables multi-world wrapping, OpenLayers disables its internal single-world stop, making this explicit width-based floor essential to prevent horizontal world repetition.
- Guard against zero-width reads if the component mounts inside a hidden tab or collapsed accordion.
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.