Pattern Fill
Register an image with map.addImage() and reference it from fill-pattern. This example builds a striped pattern from raw pixel data.
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [13.405, 52.52], zoom: 11 }); // Berlin
const area = { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[13.33, 52.55], [13.48, 52.55], [13.48, 52.49], [13.33, 52.49], [13.33, 52.55]]] } };
map.on('load', function () {
const size = 16;
const data = new Uint8Array(size * size * 4);
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const i = (y * size + x) * 4;
const on = ((x + y) % 8) < 4;
data[i] = 232; data[i + 1] = 85; data[i + 2] = 26; data[i + 3] = on ? 200 : 50;
}
}
map.addImage('stripes', { width: size, height: size, data: data });
map.addSource('area', { type: 'geojson', data: area });
map.addLayer({ id: 'area', type: 'fill', source: 'area', paint: { 'fill-pattern': 'stripes' } });
});
map.addControl(new MaptoolkitLogoControl());<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [13.405, 52.52], zoom: 11 }); // Berlin
const area = { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[13.33, 52.55], [13.48, 52.55], [13.48, 52.49], [13.33, 52.49], [13.33, 52.55]]] } };
map.on('load', function () {
const size = 16;
const data = new Uint8Array(size * size * 4);
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const i = (y * size + x) * 4;
const on = ((x + y) % 8) < 4;
data[i] = 232; data[i + 1] = 85; data[i + 2] = 26; data[i + 3] = on ? 200 : 50;
}
}
map.addImage('stripes', { width: size, height: size, data: data });
map.addSource('area', { type: 'geojson', data: area });
map.addLayer({ id: 'area', type: 'fill', source: 'area', paint: { 'fill-pattern': 'stripes' } });
});
map.addControl(new MaptoolkitLogoControl());
</script>
</body>
</html>