Add a Pattern to a Polygon
Fill a polygon with a repeating image pattern.
const API_KEY = 'toursprung';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
zoom: 1,
attributionControl: { compact: false }
});
map.on('load', async () => {
map.addSource('source', {
type: 'geojson',
data: {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-30, -25],
[-30, 35],
[ 30, 35],
[ 30, -25],
[-30, -25]
]
]
}
}
});
const image = await map.loadImage(
'https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/60px-Cat_silhouette.svg.png'
);
map.addImage('pattern', image.data);
map.addLayer({
id: 'pattern-layer',
type: 'fill',
source: 'source',
paint: {
'fill-pattern': 'pattern'
}
});
});<!DOCTYPE html>
<html lang="en">
<head>
<title>Add a Pattern to a Polygon – Maptoolkit Maps JS</title>
<meta property="og:description" content="Fill a polygon with a repeating image pattern." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.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>
const API_KEY = 'toursprung';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
zoom: 1,
attributionControl: { compact: false }
});
map.on('load', async () => {
map.addSource('source', {
type: 'geojson',
data: {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-30, -25],
[-30, 35],
[ 30, 35],
[ 30, -25],
[-30, -25]
]
]
}
}
});
const image = await map.loadImage(
'https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/60px-Cat_silhouette.svg.png'
);
map.addImage('pattern', image.data);
map.addLayer({
id: 'pattern-layer',
type: 'fill',
source: 'source',
paint: {
'fill-pattern': 'pattern'
}
});
});
</script>
</body>
</html>