From 3b799388c25db45855688deb55f4648fb62ee7a3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 2 Jul 2024 23:16:19 +0200 Subject: [PATCH] web: Improved canvas webgl creation logic --- dist/web/source/wasm-config.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/dist/web/source/wasm-config.js b/dist/web/source/wasm-config.js index f57d9f5dc..66dc0931f 100644 --- a/dist/web/source/wasm-config.js +++ b/dist/web/source/wasm-config.js @@ -111,13 +111,25 @@ var Module = { print: (function() { })(), printErr: function(text) { }, canvas: (function() { - let canvas = document.getElementById('canvas'); - // As a default initial behavior, pop up an alert when webgl context is lost. To make your - // application robust, you may want to override this behavior before shipping! - // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2 - canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); + const canvas = document.getElementById('canvas'); + canvas.addEventListener("webglcontextlost", function(e) { + alert('WebGL context lost, please reload the page'); + e.preventDefault(); + }, false); - return canvas; + if (typeof WebGL2RenderingContext !== 'undefined') { + let gl = canvas.getContext('webgl2', { stencil: true }); + if (!gl) { + console.error('WebGL 2 not available, falling back to WebGL'); + gl = canvas.getContext('webgl', { stencil: true }); + } + if (!gl) { + alert('WebGL not available with stencil buffer'); + } + return canvas; + } else { + alert('WebGL 2 not supported by this browser'); + } })(), setStatus: function(text) { }, totalDependencies: 0,