javascripteventswebglpixi.js

How to handle WebGL CONTEXT_LOST_WEBGL errors more gracefully in PixiJS?


I have a React application that uses a data visualization library that uses PixiJS.

I occasionally get frustrating CONTEXT_LOST_WEBGL errors in Chrome that force the user to manually reload the page, in order for the page to be (re)rendered.

I cannot often or reliably reproduce the error, but I know that it happens as other people tell me the application occasionally shows no data. The situations that raise this error seem very context-dependent and therefore difficult to recapitulate — low-powered graphics adapters, or lots of tabs open at once, etc.

The end user would only know that there are CONTEXT_LOST_WEBGL errors if that user has the Developer Tools console window open. Otherwise, the web page just looks blank.

I have tried the following to set up my React application to reload the window without manual user intervention, when a webglcontextlost event occurs:

componentDidMount() {
  ...
  window.addEventListener("webglcontextlost", (e) => { window.location.reload(); });
  ...
}

I'm not sure it is working correctly, i.e., if the webglcontextlost event is being handled elsewhere. Or perhaps I am trying to subscribe to the wrong event?

Otherwise, to try to handle this more gracefully, is there a way in raw Javascript, or via a third-party library, to periodically measure available memory for WebGL, and to use that measurement to instead reload the page, when the available memory reaches some arbitrary threshold that might predict an imminent CONTEXT_LOST_WEBGL error condition?


Solution

  • The following code helped restart my Pixijs web application, when the WebGL context is lost:

      addCanvasWebGLContextLossEventListener = () => {
        const canvases = document.getElementsByTagName("canvas");
        if (canvases.length === 1) {
          const canvas = canvases[0];
          canvas.addEventListener('webglcontextlost', (event) => {
            window.location.reload();
          });
        }
      }
    
      removeCanvasWebGLContextLossEventListener = () => {
        const canvases = document.getElementsByTagName("canvas");
        if (canvases.length === 1) {
          const canvas = canvases[0];
          canvas.removeEventListener('webglcontextlost');
        }
      }
    

    For other applications with more than one canvas, some adjustments would be needed to add other listeners.

    The following code helped me simulate a lost context condition (and to restore from it, via the webglcontextlost event):

      simulateWebGLContextLoss = () => {
        // 
        // simulate loss of WebGL context, for the purposes
        // of improving user experience when the browser is 
        // overwhelmed
        //
        const canvases = document.getElementsByTagName("canvas");
        if (canvases.length === 1) {
          setTimeout(() => {
            const canvas = canvases[0];
            const webgl2Context = canvas.getContext("webgl2", {});
            if (webgl2Context) {
              console.log(`losing webgl2 context...`);
              webgl2Context.getExtension('WEBGL_lose_context').loseContext();
            }
            else {
              const webglContext = canvas.getContext("webgl", {});
              if (webglContext) {
                console.log(`losing webgl context...`);
                webglContext.getExtension('WEBGL_lose_context').loseContext();
              }
            }
          }, 5000);
        }
      }
    

    For React lifecycle setup:

      componentDidMount() {
        setTimeout(() => { 
          this.addCanvasWebGLContextLossEventListener();
        }, 2500);
      }
    
      componentWillUnmount() {
        this.removeCanvasWebGLContextLossEventListener();
      }
    

    A timeout is required, as the canvas element is not yet available when the component mounts. For my purposes, the short 2.5s timer provides enough time for the event handler to latch onto the canvas.