Essentials · Angular, React and OpenLayers

Whole zoom levels with constrainResolution

Snap OpenLayers pinch, wheel and button zooms to crisp integer tile levels with constrainResolution on the view to eliminate raster label blur.

A street map of central Kraków at zoom 13, its tiles drawn at their own size.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Over central Kraków at zoom 13, continuous pinch gestures on touch screens and trackpads still scale smoothly beneath your fingers during movement, but as soon as you lift your fingers—or stop rolling the mouse wheel—the camera settles onto the nearest whole zoom level.

Every settled view displays OpenStreetMap’s 256 × 256 PNG tiles at exact 1:1 native pixel resolution with crisp street labels and sharp linework.

When you need it

Maps built around pre-rendered raster basemaps—such as OpenStreetMap, topographic scans or historical cartography—suffer noticeable text halo blur whenever the camera stops on fractional zoom levels like 12.37.

Enabling resolution snapping on reading-focused street maps guarantees native raster sharpness after every gesture while preserving fluid continuous scaling while the gesture is in flight.

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.

pinch-zoom.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** Pinch zooming restricted to whole zoom levels by the view's constrainResolution. */
@Component({
  selector: 'app-pinch-zoom',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="13" [constrainResolution]="true">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
    </aol-map>
  `,
})
export class PinchZoom {}

Architecture and OpenLayers mechanics

Setting [constrainResolution]="true" on aol-view applies integer resolution rounding at the camera constraint layer rather than inside a single input handler. During an active two-finger pinch, OpenLayers lets intermediate frames track finger spread smoothly, then animates the view cleanly onto the closest integer zoom step when the gesture ends.

Because the constraint lives on the view itself, mouse-wheel ticks, zoom buttons, keyboard +/− shortcuts and programmatic extent fits all honour the exact same integer zoom grid.

How it works

  • [constrainResolution]="true" on the view snaps settled camera resolutions to whole zoom levels while keeping active pinch gestures fluid.
  • Wheel zoom, on-map +/− buttons and keyboard shortcuts all end on integer zoom levels automatically because the rule belongs to the view.
  • At integer zoom levels, raster OpenStreetMap tiles map 1:1 to screen pixels without bilinear upscaling or downscaling blur.

Pitfalls

  • Calling fit() on a polygon with resolution snapping active chooses the coarsest integer zoom that still contains the entire geometry, which can leave wider margins around a parcel than unconstrained fractional zoom.
  • Precision CAD, surveying or vector-tile maps where users need fine intermediate scales between levels 16 and 17 should leave resolution snapping off.

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.