Check if WebGL is Supported
Check if WebGL is supported before initializing the map.
const API_KEY = 'toursprung';
const statusEl = document.getElementById('status');
function isWebGLSupported() {
try {
const canvas = document.createElement('canvas');
return !!(window.WebGLRenderingContext &&
(canvas.getContext('webgl') || canvas.getContext('experimental-webgl')));
} catch (e) {
return false;
}
}
if (!isWebGLSupported()) {
statusEl.textContent = 'WebGL is not supported in this browser.';
statusEl.style.background = '#ffcccc';
} else {
statusEl.textContent = 'WebGL is supported.';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.39085, 47.27574],
zoom: 12,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Check if WebGL is Supported – Maptoolkit Maps JS</title>
<meta property="og:description" content="Check if WebGL is supported before initializing the map." />
<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%; }
#status {
position: absolute;
top: 10px;
left: 10px;
background: white;
padding: 10px 14px;
border-radius: .4rem;
font-family: sans-serif;
font-size: 14px;
z-index: 100;
pointer-events: none;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="status"></div>
<script>
const API_KEY = 'toursprung';
const statusEl = document.getElementById('status');
function isWebGLSupported() {
try {
const canvas = document.createElement('canvas');
return !!(window.WebGLRenderingContext &&
(canvas.getContext('webgl') || canvas.getContext('experimental-webgl')));
} catch (e) {
return false;
}
}
if (!isWebGLSupported()) {
statusEl.textContent = 'WebGL is not supported in this browser.';
statusEl.style.background = '#ffcccc';
} else {
statusEl.textContent = 'WebGL is supported.';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.39085, 47.27574],
zoom: 12,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
}
</script>
</body>
</html>