Skip to content

Check if WebGL is supported

Check for WebGL browser support.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Check if WebGL is supported</title>
    <meta property="og:description" content="Check for WebGL browser support." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.css' />
    <script src='https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.js'></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
<div id="map"></div>

<script>
    function isWebglSupported() {
        if (window.WebGLRenderingContext) {
            const canvas = document.createElement('canvas');
            try {
                // Note that { failIfMajorPerformanceCaveat: true } can be passed as a second argument
                // to canvas.getContext(), causing the check to fail if hardware rendering is not available. See
                // https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
                // for more details.
                const context = canvas.getContext('webgl2') || canvas.getContext('webgl');
                if (context && typeof context.getParameter == 'function') {
                    return true;
                }
            } catch (e) {
                // WebGL is supported, but disabled
            }
            return false;
        }
        // WebGL not supported
        return false;
    }
    if (!isWebglSupported()) {
        alert('Your browser does not support WebGL');
    } else {
        const map = new maplibregl.Map({
            container: 'map',
            style:
            'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
            center: [-74.5, 40],
            zoom: 9
        });
    }
</script>
</body>
</html>