Skip to content

Change the Background Color

In the globe projection the area around the planet is the map container background. Recolor it with plain CSS - the buttons switch the background behind the globe between presets.

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: [10, 25], zoom: 2 });
const container = map.getContainer();
const presets = { Space: '#0b1026', Sky: '#bfe3ff', Light: '#ffffff' };
function applyBackground(color) { container.style.background = color; }
map.on('style.load', function () { map.setProjection({ type: 'globe' }); });
applyBackground(presets.Space);
const box = document.getElementById('bg');
Object.keys(presets).forEach(function (name) {
  const btn = document.createElement('button');
  btn.textContent = name;
  if (name === 'Space') btn.classList.add('active');
  btn.addEventListener('click', function () {
    applyBackground(presets[name]);
    box.querySelectorAll('button').forEach(function (b) { b.classList.remove('active'); });
    btn.classList.add('active');
  });
  box.appendChild(btn);
});
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%; }
    .bg-presets { position:absolute; top:10px; left:10px; z-index:1; background:#fff; border-radius:6px; padding:6px; box-shadow:0 1px 4px rgba(0,0,0,.3); font:13px/1.2 system-ui,sans-serif; }
    .bg-presets button { display:inline-block; margin:2px; padding:5px 9px; border:none; border-radius:4px; background:#f1f1f1; cursor:pointer; }
    .bg-presets button.active { background:#1a73e8; color:#fff; }
  </style>
</head>
<body>
  <div class="bg-presets" id="bg"></div>
  <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: [10, 25], zoom: 2 });
const container = map.getContainer();
const presets = { Space: '#0b1026', Sky: '#bfe3ff', Light: '#ffffff' };
function applyBackground(color) { container.style.background = color; }
map.on('style.load', function () { map.setProjection({ type: 'globe' }); });
applyBackground(presets.Space);
const box = document.getElementById('bg');
Object.keys(presets).forEach(function (name) {
  const btn = document.createElement('button');
  btn.textContent = name;
  if (name === 'Space') btn.classList.add('active');
  btn.addEventListener('click', function () {
    applyBackground(presets[name]);
    box.querySelectorAll('button').forEach(function (b) { b.classList.remove('active'); });
    btn.classList.add('active');
  });
  box.appendChild(btn);
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>