Interactions · Angular, React and OpenLayers
Move and resize a given box
Pre-populate a draggable, edge-resizable bounding box in OpenLayers while disabling new box creation via createCondition never and drag true.

What this example shows
A crisp blue-outlined rectangular frame opens pre-positioned around Kraków’s Old Town and Wawel. Dragging anywhere inside the interior slides the entire frame across the city, dragging any edge or corner handle resizes it (for example expanding southeast over Kazimierz), and dragging outside the frame pans the map normally without ever accidentally replacing the box.
When you need it
Print-layout composers, fixed-quota imagery order forms and report-area selectors usually propose an initial bounding box centred on the user’s search address and ask the user only to nudge or resize that proposed frame.
Locking createCondition to never while enabling [drag]="true" turns ExtentInteraction into a dedicated frame-adjustment gizmo that never destroys the existing box on an accidental outside drag.
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.
extent-interaction-modify.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import { never } from 'ol/events/condition';
import { transformExtent } from 'ol/proj';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
ExtentInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
/** A box around the Old Town that the user moves and resizes with the extent interaction, but cannot draw anew. */
@Component({
selector: 'app-extent-interaction-modify',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
ExtentInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-interaction-extent [extent]="extent" [createCondition]="never" [drag]="true" [boxStyle]="boxStyle" />
<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-osm /></aol-layer-tile>
</aol-map>
<div class="example-controls">
<p class="example-hint">Drag the box to move it, or drag one of its edges or corners to resize it.</p>
</div>
`,
})
export class ExtentInteractionModify {
/** The Old Town and Wawel, in the view's projection. */
protected readonly extent = transformExtent([19.925, 50.048, 19.95, 50.068], 'EPSG:4326', 'EPSG:3857');
/** A pointer down never starts a new box. */
protected readonly never = never;
protected readonly boxStyle = new Style({
fill: new Fill({ color: 'rgba(255, 255, 255, 0.4)' }),
stroke: new Stroke({ color: '#3399CC', width: 1.25 }),
});
} 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.
extent-interaction-modify.react.tsximport { never } from 'ol/events/condition.js';
import { transformExtent } from 'ol/proj.js';
import Fill from 'ol/style/Fill.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
ExtentInteraction,
Map,
OSMSource,
TileLayer,
View,
} from 'cc-openlayers/react';
/** The Old Town and Wawel, in the view's projection. */
const extent = transformExtent([19.925, 50.048, 19.95, 50.068], 'EPSG:4326', 'EPSG:3857');
const boxStyle = new Style({
fill: new Fill({ color: 'rgba(255, 255, 255, 0.4)' }),
stroke: new Stroke({ color: '#3399CC', width: 1.25 }),
});
/** A box around the Old Town that the user moves and resizes with the extent interaction, but cannot draw anew. */
export function ExtentInteractionModify() {
return (
<>
<Map height="480px">
<DefaultInteractions />
{/* A pointer down never starts a new box. */}
<ExtentInteraction extent={extent} createCondition={never} drag={true} boxStyle={boxStyle} />
<DefaultControls />
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<p className="example-hint">Drag the box to move it, or drag one of its edges or corners to resize it.</p>
</div>
</>
);
}How the React version works
A box around the Old Town that the user moves and resizes with the extent interaction but cannot draw anew.
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.
From Angular to React
| Angular | React |
|---|---|
<aol-interaction-extent [extent] [boxStyle] [createCondition]> | <ExtentInteraction extent boxStyle createCondition> with module constants |
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.
Architecture and OpenLayers mechanics
Initial WGS84 degree bounds convert to Web Mercator via transformExtent and bind to [extent], styled with a clear blue-rimmed boxStyle.
Setting [drag]="true" enables interior whole-box translation, while setting createCondition to never blocks outside drags from starting a replacement rectangle—letting outside drags fall straight through to normal map panning.
How it works
[extent]initialises the pre-populated bounding box projected from degrees viatransformExtent.[drag]="true"translates the entire rectangle whenever a drag begins inside its interior away from the edges.- Setting
createConditiontoneverprevents drags outside the box from replacing it and preserves normal map panning. boxStyleapplies a clear blue outline and translucent white fill so edges and handles stand out over street maps.
Pitfalls
- If you enable
[drag]="true"without settingcreateConditiontonever, dragging outside the box defaults to always—wiping out the user’s frame and drawing a new box instead of panning the map. - Because edge/corner snapping claims a 10-pixel band around the perimeter, boxes shrunk narrower than ~20 pixels across expose only resize handles until enlarged again.
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.