Vector data · Angular, React and OpenLayers
A Koch snowflake of up to 786,433 points
Render a recursive Koch snowflake up to 786,433 vertices over Kraków in OpenLayers to demonstrate automatic Douglas-Peucker simplification.

What this example shows
Over OpenStreetMap of Kraków, a blue Koch snowflake fractal starts from an equilateral triangle and subdivides recursively as you release the Depth slider—multiplying segment count by 4 per step up to depth 9 (786,433 vertices) while keeping panning and zooming remarkably responsive.
When you need it
High-precision LiDAR contours, national river networks and un-generalised coastal polygons frequently contain hundreds of thousands of vertices inside a single LineString or Polygon.
Testing a deterministic 786k-vertex Koch curve illustrates how OpenLayers’ built-in per-resolution Douglas–Peucker simplification keeps canvas frame times fast.
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.
fractal.tsimport { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import {
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryLinestringComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import type { Coordinate } from 'ol/coordinate';
import { fromLonLat } from 'ol/proj';
const [centerX, centerY] = fromLonLat([19.9368, 50.0614]);
const radius = 10e6 / 1024;
const cos30 = Math.cos(Math.PI / 6);
const sin30 = Math.sin(Math.PI / 6);
const triangle: Coordinate[] = [
[centerX, centerY + radius],
[centerX + radius * cos30, centerY - radius * sin30],
[centerX - radius * cos30, centerY - radius * sin30],
[centerX, centerY + radius],
];
/** A Koch snowflake of up to 786 thousand vertices over Kraków, regenerated when the depth slider is released. */
@Component({
selector: 'app-fractal',
imports: [
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryLinestringComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="11"><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>
<aol-feature>
<aol-geometry-linestring
><aol-collection-coordinates [coordinates]="coordinates()"
/></aol-geometry-linestring>
</aol-feature>
</aol-source-vector>
</aol-layer-vector>
</aol-map>
<div class="example-controls">
<label>
Depth <input type="range" min="0" max="9" step="1" [value]="depth()" (change)="depth.set(number($event))" />
<output>{{ depth() }}</output>
({{ coordinates().length }} points)
</label>
</div>
`,
})
export class Fractal {
readonly depth = signal(5);
protected readonly coordinates = computed(() => makeFractal(this.depth()));
protected number(event: Event): number {
return (event.target as HTMLInputElement).valueAsNumber;
}
}
/** The triangle with the middle third of every segment replaced by a peak, `depth` times. */
function makeFractal(depth: number): Coordinate[] {
let coordinates = triangle;
for (let level = 0; level < depth; ++level) {
const next: Coordinate[] = [coordinates[0]];
for (let i = 1; i < coordinates.length; ++i) {
next.push(...injectPoints(coordinates[i - 1], coordinates[i]));
}
coordinates = next;
}
return coordinates;
}
/** The points after `start` on a segment: one third along, the peak of the _/\_ shape, two thirds along and `end`. */
function injectPoints(start: Coordinate, end: Coordinate): Coordinate[] {
const dx = end[0] - start[0];
const dy = end[1] - start[1];
const r = Math.sqrt(dx * dx + dy * dy) / (2 * cos30);
const a = Math.atan2(dy, dx) + Math.PI / 6;
return [
[start[0] + dx / 3, start[1] + dy / 3],
[start[0] + r * Math.cos(a), start[1] + r * Math.sin(a)],
[end[0] - dx / 3, end[1] - dy / 3],
end,
];
} 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.
fractal.react.tsximport { useEffect, useMemo, useRef, useState } from 'react';
import type { Coordinate as OLCoordinate } from 'ol/coordinate';
import { fromLonLat } from 'ol/proj';
import {
CollectionCoordinates,
Coordinate,
DefaultControls,
DefaultInteractions,
Feature,
LineStringGeometry,
Map,
OSMSource,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const [centerX, centerY] = fromLonLat([19.9368, 50.0614]);
const radius = 10e6 / 1024;
const cos30 = Math.cos(Math.PI / 6);
const sin30 = Math.sin(Math.PI / 6);
const triangle: OLCoordinate[] = [
[centerX, centerY + radius],
[centerX + radius * cos30, centerY - radius * sin30],
[centerX - radius * cos30, centerY - radius * sin30],
[centerX, centerY + radius],
];
/** A Koch snowflake of up to 786 thousand vertices over Kraków, regenerated when the depth slider is released. */
export function Fractal() {
const [depth, setDepth] = useState(5);
const coordinates = useMemo(() => makeFractal(depth), [depth]);
const slider = useRef<HTMLInputElement>(null);
// React's onChange fires on every step of a drag; the native change event fires once, on release.
useEffect(() => {
const input = slider.current as HTMLInputElement;
const release = () => setDepth(input.valueAsNumber);
input.addEventListener('change', release);
return () => input.removeEventListener('change', release);
}, []);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={11}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource>
<Feature>
<LineStringGeometry>
<CollectionCoordinates coordinates={coordinates} />
</LineStringGeometry>
</Feature>
</VectorSource>
</VectorLayer>
</Map>
<div className="example-controls">
<label>
Depth <input type="range" min="0" max="9" step="1" defaultValue={depth} ref={slider} />
<output>{depth}</output>({coordinates.length} points)
</label>
</div>
</>
);
}
/** The triangle with the middle third of every segment replaced by a peak, `depth` times. */
function makeFractal(depth: number): OLCoordinate[] {
let coordinates = triangle;
for (let level = 0; level < depth; ++level) {
const next: OLCoordinate[] = [coordinates[0]];
for (let i = 1; i < coordinates.length; ++i) {
next.push(...injectPoints(coordinates[i - 1], coordinates[i]));
}
coordinates = next;
}
return coordinates;
}
/** The points after `start` on a segment: one third along, the peak of the _/\_ shape, two thirds along and `end`. */
function injectPoints(start: OLCoordinate, end: OLCoordinate): OLCoordinate[] {
const dx = end[0] - start[0];
const dy = end[1] - start[1];
const r = Math.sqrt(dx * dx + dy * dy) / (2 * cos30);
const a = Math.atan2(dy, dx) + Math.PI / 6;
return [
[start[0] + dx / 3, start[1] + dy / 3],
[start[0] + r * Math.cos(a), start[1] + r * Math.sin(a)],
[end[0] - dx / 3, end[1] - dy / 3],
end,
];
}How the React version works
A Koch snowflake of up to 786 thousand vertices over Kraków; the depth slider recomputes it only when released, as (change) does in Angular.
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 |
|---|---|
the slider's (change) | a native change listener added in useEffect, because React's onChange fires at every step of a drag |
a computed of the coordinates | useMemo on [depth] |
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
makeFractal starts from triangle (radius 10e6 / 1024 projection units around Kraków) and applies injectPoints depth times—splitting each segment at 1/3, an outward equilateral peak, and 2/3 to quadruple segment count per iteration.
A computed signal feeds the resulting coordinate array into aol-collection-coordinates, while binding the slider’s (change) event (on pointer release rather than continuous input) avoids allocating millions of intermediate arrays mid-drag.
How it works
makeFractalsubdivides an initialtriangle(radius10e6 / 1024) recursivelydepthtimes around Kraków.injectPointsreplaces each segment with four sub-segments, reaching 3,073 points at depth 5 and 786,433 points at depth 9.- A
computedsignal derives the vertex array fromdepthand passes it toaol-collection-coordinates. - Listening to
(change)instead ofinputrecomputes the fractal once when the user releases the slider thumb.
Pitfalls
- Before painting each frame, OpenLayers’ 2D Canvas renderer simplifies geometries to a half-pixel tolerance at the active view resolution using Douglas–Peucker—so zooming all the way in draws detailed sub-peaks while zoomed-out pans stroke only visible vertices.
- Never run O(4^n) geometry generation on every continuous slider
inputtick when upper depths allocate three-quarters of a million coordinate tuples.
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.