From 7d52de5eba814de9925dbca2c64f2186722a674b Mon Sep 17 00:00:00 2001 From: Ni Rui Date: Mon, 8 Jan 2024 20:15:06 +0800 Subject: [PATCH] Better test for client WebGL availability --- ui/widgets/screen_console.vue | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/ui/widgets/screen_console.vue b/ui/widgets/screen_console.vue index 919e28ec..ebdb4ede 100644 --- a/ui/widgets/screen_console.vue +++ b/ui/widgets/screen_console.vue @@ -125,6 +125,24 @@ const termDefaultFontSize = 16; const termMinFontSize = 8; const termMaxFontSize = 36; +function webglSupported() { + try { + if (typeof window !== "object") { + return false; + } + if (typeof window.WebGLRenderingContext !== "function") { + return false; + } + if (typeof window.WebGL2RenderingContext !== "function") { + return false; + } + return document.createElement('canvas').getContext('webgl') && + document.createElement('canvas').getContext('webgl2'); + } catch(e) { + } + return false; +} + class Term { constructor(control) { const resizeDelayInterval = 500; @@ -230,16 +248,11 @@ class Term { this.term.loadAddon(this.fit); this.term.loadAddon(new WebLinksAddon()); this.term.loadAddon(new Unicode11Addon()); - if (() => { - try { - return !!window.WebGLRenderingContext && - document.createElement('canvas').getContext('webgl'); - } catch(e) { - return false; + try { + if (webglSupported()) { + this.term.loadAddon(new WebglAddon()); } - }) { - this.term.loadAddon(new WebglAddon()); - } + } catch(e) {} this.term.unicode.activeVersion = '11'; this.refit(); }