Interactions · Angular, React and OpenLayers

Style the sketch while drawing

Customize the active in-progress sketch appearance in OpenLayers Draw interaction per geometry type using flat circle, stroke and fill styles.

A map of Kraków with a finished polygon over Kazimierz and a second one being drawn over the Old Town in the sketch style: blue fill, yellow outline, red point.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

While sketching points, lines, polygons or circles over Kraków, the active rubber-band geometry in progress stands out with a bright red cursor vertex dot, a yellow rubber-band outline for lines and polygons (blue for circles) and an opaque blue or yellow fill—then switches cleanly to OpenLayers’ calm default blue layer style the moment you finish the shape.

When you need it

When users digitise new parcels, routes or exclusion zones on top of dozens of existing features, an unstyled sketch that looks identical to finished polygons makes it hard to tell which vertex is still rubber-banding.

Assigning a high-contrast sketch style on the Draw interaction itself separates “work in progress” visually from committed data.

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.

draw-features-style.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import type { FlatStyleLike } from 'ol/style/flat';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent } from 'cc-openlayers/editing';

type DrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle' | 'None';

/** Geometries drawn with the draw interaction, whose sketch is styled for each geometry type. */
@Component({
  selector: 'app-draw-features-style',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    DrawInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    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-osm /></aol-layer-tile>
      <aol-layer-vector><aol-source-vector #source [wrapX]="false" /></aol-layer-vector>
      <!-- The interaction is made anew for each type; None draws nothing. -->
      @for (current of drawTypes; track current) {
        @if (current === type()) {
          <aol-interaction-draw [type]="current" [source]="source.instance" [style]="styles[current]" />
        }
      }
    </aol-map>
    <div class="example-controls">
      <label>
        Geometry type
        <select (change)="type.set(value($event))">
          @for (option of types; track option) {
            <option [value]="option" [selected]="option === type()">{{ option }}</option>
          }
        </select>
      </label>
      <p class="example-hint">
        Click the last point to finish. Hold Shift to draw lines, polygons and circles freehand.
      </p>
    </div>
  `,
})
export class DrawFeaturesStyle {
  readonly type = signal<DrawType>('Point');
  protected readonly drawTypes: Exclude<DrawType, 'None'>[] = ['Point', 'LineString', 'Polygon', 'Circle'];
  protected readonly types: DrawType[] = [...this.drawTypes, 'None'];
  protected readonly styles: Record<Exclude<DrawType, 'None'>, FlatStyleLike> = {
    Point: {
      'circle-radius': 5,
      'circle-fill-color': '#ff0000',
    },
    LineString: {
      'circle-radius': 5,
      'circle-fill-color': '#ff0000',
      'stroke-color': '#ffff00',
      'stroke-width': 2,
    },
    Polygon: {
      'circle-radius': 5,
      'circle-fill-color': '#ff0000',
      'stroke-color': '#ffff00',
      'stroke-width': 2,
      'fill-color': '#0000ff',
    },
    Circle: {
      'circle-radius': 5,
      'circle-fill-color': '#ff0000',
      'stroke-color': '#0000ff',
      'stroke-width': 2,
      'fill-color': '#ffff00',
    },
  };

  protected value(event: Event): DrawType {
    return (event.target as HTMLSelectElement).value as DrawType;
  }
}

Architecture and OpenLayers mechanics

Passing a geometry-keyed styles lookup into [style] on aol-interaction-draw styles only OpenLayers’ internal sketch overlay—configuring circle-radius and circle-fill-color for the cursor snap/vertex cursor alongside stroke-color and fill-color for the rubber-band line, polygon or circle.

Clicking the final vertex (or holding Shift to trace freehand) commits the finished feature into the vector source, where the host vector layer’s own style takes over.

How it works

  • Per-type flat styles bound to [style] on aol-interaction-draw control the active rubber-band sketch overlay.
  • circle-radius and circle-fill-color render the red cursor vertex indicator while stroke-color and fill-color style the unfinished line, polygon or circle.
  • Clicking the last placed vertex completes lines and polygons, and holding Shift switches temporarily to freehand dragging.
  • Committed features move into the vector layer’s source and adopt the layer’s style rather than keeping the temporary sketch style.

Pitfalls

  • Styles passed to aol-interaction-draw apply strictly while drawing is in progress—remember to configure a [style] on your destination aol-layer-vector as well if committed features need custom colours.
  • If your flat sketch style omits circle-* properties (or a point symboliser in a Style function), the cursor vertex dot disappears completely during polygon and line drawing.

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.