Projections · Angular, React and OpenLayers

Immediate rendering in geographic coordinates

Draw 1,000 age-faded orange points in WGS84 degrees on every animation frame in OpenLayers using getVectorContext() and useGeographic().

Esri's dark grey map of Kraków and its surroundings scattered with orange dots of different sizes and brightness, the newest points large and bright, the older ones small and faint.
A screenshot of the example. Base map: Esri World Dark Gray (Esri, HERE, Garmin, OpenStreetMap contributors and the GIS user community).

What this example shows

Over Esri’s World Dark Gray basemap around Kraków, a rolling stream of 1,000 orange point flashes appears directly in [longitude, latitude] coordinates—newest arrivals glowing large and bright while older points shrink and fade away smoothly at full display frame rate.

When you need it

High-rate geographic telemetry—lightning detections, packet traces or sensor pings arriving natively in WGS 84 degrees—can bypass Feature and VectorSource overhead completely when drawn straight onto a layer canvas.

Combining OpenLayers’ immediate rendering API with useGeographic() lets per-frame canvas code pass raw degree geometries without manual fromLonLat calls.

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.

immediate-geographic.shared.ts
import { upAndDown } from 'ol/easing.js';
import Point from 'ol/geom/Point.js';
import { getVectorContext } from 'ol/render.js';
import type RenderEvent from 'ol/render/Event.js';
import Circle from 'ol/style/Circle.js';
import Fill from 'ol/style/Fill.js';
import Style from 'ol/style/Style.js';

/** The area the points appear in, around Kraków: [west, south, east, north] in degrees. */
const AREA = [19.45, 49.9, 20.43, 50.23];

/** The centre of the map, in longitude and latitude. */
export const CENTER = [19.94, 50.065];

/** A random point in `AREA`, in longitude and latitude. */
export function randomPoint(): Point {
  const [west, south, east, north] = AREA;
  return new Point([west + (east - west) * Math.random(), south + (north - south) * Math.random()]);
}

/** The circle every point is drawn as; each frame sets its opacity and scale. */
export const image = new Circle({
  radius: 8,
  fill: new Fill({ color: 'rgb(255, 153, 0)' }),
});

/** The style that carries the circle. */
export const style = new Style({
  image: image,
});

/** The number of points kept. */
export const n = 1000;

/** Draws `geometries` by age, then replaces the oldest with a new random one. */
export function drawPoints(event: RenderEvent, geometries: Point[]): void {
  const vectorContext = getVectorContext(event);
  for (let i = 0; i < n; ++i) {
    const importance = upAndDown(Math.pow((n - i) / n, 0.15));
    if (importance < 0.1) {
      continue;
    }
    image.setOpacity(importance);
    image.setScale(importance);
    vectorContext.setStyle(style);
    vectorContext.drawGeometry(geometries[i]);
  }
  geometries.push(randomPoint());
  geometries.shift();
}
immediate-geographic.ts
import { ChangeDetectionStrategy, Component, DestroyRef, inject, viewChild } from '@angular/core';
import type Point from 'ol/geom/Point';
import { clearUserProjection, useGeographic } from 'ol/proj';
import type RenderEvent from 'ol/render/Event';
import {
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceImageTileComponent,
  ViewComponent,
} from 'cc-openlayers';
import { CENTER, drawPoints, n, randomPoint } from './immediate-geographic.shared';

/**
 * A thousand points around Kraków drawn on every frame with the immediate rendering API, in geographic coordinates:
 * after the base layer renders, `getVectorContext` draws each point, sized and faded by its age, and the oldest point
 * gives way to a new random one.
 */
@Component({
  selector: 'app-immediate-geographic',
  imports: [
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceImageTileComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [center]="center" [zoom]="11" />
      <aol-layer-tile [postrender]="draw">
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="16"
          attributions="Tiles © Esri — Esri, HERE, Garmin, © OpenStreetMap contributors, and the GIS user community"
        />
      </aol-layer-tile>
    </aol-map>
  `,
})
export class ImmediateGeographic {
  protected readonly center = CENTER;
  /** The points from the oldest to the newest. */
  readonly geometries: Point[] = Array.from({ length: n }, randomPoint);
  private readonly map = viewChild.required(MapComponent);

  constructor() {
    useGeographic();
    inject(DestroyRef).onDestroy(clearUserProjection);
  }

  protected readonly draw = (event: RenderEvent): void => {
    drawPoints(event, this.geometries);
    this.map().instance.render();
  };
}

Architecture and OpenLayers mechanics

With useGeographic() active, randomPoint() generates Point geometries in decimal degrees inside AREA. On every tile-layer [postrender]="draw" pass, getVectorContext(event) projects those degree geometries to Web Mercator on the fly—adjusting a single shared circle style via image.setOpacity(importance) and image.setScale(importance) before vectorContext.drawGeometry(geometries[i]) (skipping points below 0.1).

Appending one new point (geometries.push(randomPoint())), dropping the oldest (geometries.shift()) and calling this.map().instance.render() drives the continuous animation.

How it works

  • [postrender]="draw" uses getVectorContext(event) to paint directly onto the base tile layer’s canvas.
  • useGeographic() lets randomPoint() keep coordinates in AREA as decimal degrees while immediate drawing projects them to Web Mercator.
  • Mutating image.setOpacity(importance) and image.setScale(importance) before vectorContext.drawGeometry(geometries[i]) avoids per-point style allocations.
  • geometries.push(randomPoint()) and geometries.shift() maintain a fixed-size 1,000-point ring buffer across this.map().instance.render() frames.

Pitfalls

  • Geometries painted via getVectorContext exist only as canvas pixels—neither forEachFeatureAtPixel nor getFeaturesAtPixel can select or inspect them.
  • Requesting render() at the end of every postrender keeps the entire map (including basemap composition) running at 60+ fps continuously; pause the loop when the tab hides or streaming stops.

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.