Tile and image services · Angular, React and OpenLayers

Tiles made in the browser from pixel data

Generate procedural RGBA pixel-array tiles in the browser with OpenLayers DataTileSource and render them on the GPU via WebGLTileLayer.

The centre of Kraków at zoom 13 under translucent grey tiles, each outlined in white and labelled with its z, x and y over OpenStreetMap.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Over central Kraków at zoom 13, translucent grey 256 × 256 tiles outlined in white and stamped with their z / x / y coordinates are synthesised in memory as typed pixel arrays and composited over OpenStreetMap by WebGL.

When you need it

Scientific and environmental WebGIS apps frequently compute or decode gridded rasters client-side—such as NetCDF weather slices, custom binary elevation grids, hydrological simulation outputs or WebWorker-generated heat surfaces—rather than fetching pre-rendered PNGs.

DataTileSource lets a JavaScript or WebAssembly [loader] return raw numeric band arrays per (z, x, y) tile and delegates pyramid caching, frustum culling and GPU texture upload to OpenLayers.

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.

data-tiles.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceDataTileComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';
import { LayerWebGLTileComponent } from 'cc-openlayers/webgl';

const SIZE = 256;
const LINE_HEIGHT = 30;

/**
 * Tiles whose pixels a data tile source takes from a loader (here a canvas labelled with each tile's coordinates),
 * drawn over a street map by a WebGL tile layer: the canvas tile layer cannot draw pixel arrays.
 */
@Component({
  selector: 'app-data-tiles',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerWebGLTileComponent,
    MapComponent,
    SourceDataTileComponent,
    SourceOsmComponent,
    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-webgltile>
        <!-- No opacity transition, so labels of loading tiles do not overlap. -->
        <aol-source-datatile [loader]="loader" [transition]="0" />
      </aol-layer-webgltile>
    </aol-map>
  `,
})
export class DataTiles {
  private context?: CanvasRenderingContext2D | null;

  /** Draws the tile's coordinates on the shared canvas and gives its pixels. */
  protected readonly loader = (z: number, x: number, y: number): Uint8ClampedArray => {
    const context = (this.context ??= labelCanvas());
    if (!context) return new Uint8ClampedArray(SIZE * SIZE * 4);
    const half = SIZE / 2;
    context.clearRect(0, 0, SIZE, SIZE);
    context.fillStyle = 'rgba(100, 100, 100, 0.5)';
    context.fillRect(0, 0, SIZE, SIZE);
    context.fillStyle = 'black';
    context.fillText(`z: ${z}`, half, half - LINE_HEIGHT);
    context.fillText(`x: ${x}`, half, half);
    context.fillText(`y: ${y}`, half, half + LINE_HEIGHT);
    context.strokeRect(0, 0, SIZE, SIZE);
    return context.getImageData(0, 0, SIZE, SIZE).data;
  };
}

/** A tile-sized canvas set up for centred labels and a white outline of the tile. */
function labelCanvas(): CanvasRenderingContext2D | null {
  const canvas = document.createElement('canvas');
  canvas.width = SIZE;
  canvas.height = SIZE;
  const context = canvas.getContext('2d', { willReadFrequently: true });
  if (context) {
    context.strokeStyle = 'white';
    context.textAlign = 'center';
    context.font = '24px sans-serif';
  }
  return context;
}

Architecture and OpenLayers mechanics

aol-source-datatile invokes its [loader] function for each visible (z, x, y) coordinate on OpenStreetMap’s grid, expecting a flat typed pixel buffer containing 256 × 256 × 4 RGBA bytes. Here one reusable off-screen HTML5 canvas paints the translucent fill, border and coordinate label and extracts a fresh Uint8ClampedArray snapshot via getImageData, then is cleared and reused for the next tile.

Setting [transition] to 0 displays generated tiles immediately without overlapping labels during fades, and aol-layer-webgltile uploads the raw 4-band pixel buffers directly as GPU textures.

How it works

  • aol-source-datatile calls [loader] with (z, x, y) and accepts flat 4-band RGBA typed arrays per tile.
  • A single shared scratch canvas draws the outline and text label, returning an independent pixel copy via getImageData for each tile.
  • Matching OpenStreetMap’s default Web Mercator grid keeps every generated coordinate label aligned with the street tile below.
  • [transition] of 0 prevents incoming tile labels from briefly overlapping coarser parent labels during zoom changes.

Pitfalls

  • Standard 2D aol-layer-tile (CanvasTileLayer) cannot render raw numeric pixel arrays and throws an error—always pair numeric DataTile loaders with aol-layer-webgltile.
  • OpenLayers infers band count from array length: for default 4-band RGBA rendering on a 256 × 256 grid, the returned buffer must contain exactly 262,144 (256 * 256 * 4) values.

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.