Vector data · Angular, React and OpenLayers

Points drawn in every frame

Animate 200 moving points at 60 fps directly on an OpenLayers tile-layer canvas in postrender using getVectorContext and frameState timestamps.

Yellow points with red rims along a looping curve around Poland at zoom 5 over OpenStreetMap, the head of the curve a black dot.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Across Poland at zoom 5 over OpenStreetMap, a sinuous chain of 200 yellow-and-red points led by a black head marker orbits smoothly around Kraków once every 30 seconds at full display refresh rate—drawn with zero VectorLayer or Feature instances.

When you need it

High-frequency telemetry streams—radar sweeps, particle animations, drone swarms or simulated traffic—update coordinates on every 16 ms animation frame where mutating Feature objects and rebuilding spatial indices would waste CPU and memory.

When animated markers only need visual rendering without Feature selection or attribute popups, immediate postrender vector drawing delivers maximum throughput.

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.

dynamic-data.ts
import { ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import type { Coordinate } from 'ol/coordinate';
import MultiPoint from 'ol/geom/MultiPoint';
import Point from 'ol/geom/Point';
import { fromLonLat } from 'ol/proj';
import { getVectorContext } from 'ol/render';
import type RenderEvent from 'ol/render/Event';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

const POINTS = 200;
/** Rotation period in ms. */
const PERIOD = 30000;
/**
 * The radii of the curve in Web Mercator metres, a tenth of the original's: up to 1,100 km of the projection from
 * Kraków, the curve circles Poland and fits the map at zoom 5.
 */
const BIG_RADIUS = 7e5;
const SMALL_RADIUS = 2e5;
const OFFSET = 2e5;
const CENTER = fromLonLat([19.9368, 50.0614]);

const dot = new Style({
  image: new CircleStyle({
    radius: 5,
    fill: new Fill({ color: 'yellow' }),
    stroke: new Stroke({ color: 'red', width: 1 }),
  }),
});
const headInner = new Style({ image: new CircleStyle({ radius: 2, fill: new Fill({ color: 'blue' }) }) });
const headOuter = new Style({ image: new CircleStyle({ radius: 5, fill: new Fill({ color: 'black' }) }) });

/** The 200 points of the curve at a time in ms; the last one is the head. */
export function curveAt(time: number): Coordinate[] {
  const theta = (2 * Math.PI * time) / PERIOD;
  return Array.from({ length: POINTS }, (_, index) => {
    const t = theta + (2 * Math.PI * index) / POINTS;
    const x =
      (BIG_RADIUS + SMALL_RADIUS) * Math.cos(t) + OFFSET * Math.cos(((BIG_RADIUS + SMALL_RADIUS) * t) / SMALL_RADIUS);
    const y =
      (BIG_RADIUS + SMALL_RADIUS) * Math.sin(t) + OFFSET * Math.sin(((BIG_RADIUS + SMALL_RADIUS) * t) / SMALL_RADIUS);
    return [CENTER[0] + x, CENTER[1] + y];
  });
}

/** Points that move along a curve, drawn straight onto the tile layer's canvas after each render. */
@Component({
  selector: 'app-dynamic-data',
  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]="5">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile [postrender]="postrender"><aol-source-osm /></aol-layer-tile>
    </aol-map>
  `,
})
export class DynamicData {
  private readonly map = viewChild.required(MapComponent);

  protected readonly postrender = (event: RenderEvent): void => {
    const context = getVectorContext(event);
    const coordinates = curveAt(event.frameState?.time ?? 0);
    context.setStyle(dot);
    context.drawGeometry(new MultiPoint(coordinates));
    const head = new Point(coordinates[coordinates.length - 1]);
    context.setStyle(headOuter);
    context.drawGeometry(head);
    context.setStyle(headInner);
    context.drawGeometry(head);
    // The next frame is requested at once, so the points keep moving.
    this.map().instance.render();
  };
}

Architecture and OpenLayers mechanics

Inside the base tile layer’s postrender event, curveAt evaluates the parametric positions of all 200 points from frameState’s authoritative render timestamp—keeping angular speed independent of monitor refresh rate (60 Hz vs 120 Hz).

Calling getVectorContext obtains an immediate canvas renderer that paints all 200 coordinates in a single MultiPoint draw call followed by the black lead point, and calling render() schedules the next animation frame continuously.

How it works

  • curveAt computes 200 parametric coordinates completing one full orbit every 30 seconds up to 1,100 projection km around Kraków.
  • In the tile layer’s postrender listener, getVectorContext draws all 200 points as one MultiPoint plus the leading point.
  • Driving positions from frameState time keeps motion smooth and frame-rate-independent.
  • Calling render() at the end of each pass requests the next animation frame.

Pitfalls

  • Calling render() unconditionally inside postrender runs a continuous 60+ fps GPU/CPU loop—pause the loop whenever animation stops, the tab hides or the component unmounts to save laptop and mobile battery.
  • Shapes painted via getVectorContext are transient pixels rather than retained Features, so forEachFeatureAtPixel and Select interactions will not detect them.

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.