diff --git a/.changeset/big-views-flow.md b/.changeset/big-views-flow.md new file mode 100644 index 0000000..84737e1 --- /dev/null +++ b/.changeset/big-views-flow.md @@ -0,0 +1,5 @@ +--- +'svelte-maplibre': patch +--- + +fix: check if webgl is enabled and call onerror otherwise diff --git a/src/lib/MapLibre.svelte b/src/lib/MapLibre.svelte index 4b6b615..5442777 100644 --- a/src/lib/MapLibre.svelte +++ b/src/lib/MapLibre.svelte @@ -253,9 +253,30 @@ } } + function isWebglSupported() { + if (!window.WebGLRenderingContext) { + return false; + } + const canvas = document.createElement('canvas'); + try { + 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; + } + function createMap(element: HTMLDivElement) { onHashChange(); + if (!isWebglSupported()) { + handleError(new ErrorEvent('', { error: new Error('WebGL is disabled or not supported') })); + return; + } + map = mapContext.map = new maplibre.Map( flush({ container: element, @@ -282,7 +303,7 @@ attributionControl, transformRequest, cooperativeGestures, - aroundCenter + aroundCenter, }) );