WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost

After updating nwjs to the latest release (0.63.0) I noticed a very annoying issue that I have difficulties solving, meaning, sometimes the scene either becomes totally black or freezes, however the game itself "behind the scene" still works, I can navigate through menu, events are running ok, but the player (me) don't see it. This happens in all nwjs releases since 0.53.0, at least according to my experiments - I didn't check strictly all releases, I've skipped middle releases 0.x.1 - 0.x.5.

I don't have a screen from console, but I've copied a warning message from the console to search for some answers over the internet. Console shows: Warning: WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost.

What I found over the internet (StackOverflow, developer.mozilla.org etc) are very different answers not related to MV engine, varying from damaged display driver, memory leaks, losing context after calling canvas.getContext, and so on, so as a last resort I'm asking here, how can I handle it and restore context without the completely reloading game?

(Huh, this topic perhaps should land in JS Plugin support... Not sure)
No one? Similar search results on this board did not provide any solid solution.
It very likely has more to do with how your system is handling the nwjs rather than the project itself.
What I recommend is maybe trying another way to execute, this way it uses different browser configurations:

You can also try the nw without the dev tools, it is lighter and maybe it won't happen the same problem. You can force it to render as canvas, I don't remember if its in the community plugin or something, but it is a possibility also. Last thing, can you post your PC configurations? This way is easier to know if it is a bottleneck on the configuration or if it is only a software case to be fixed.
Solid solution is quite difficult to provide with this info available, since root issue can be a lot of things.

But I had this issue when porting my MV plugins to MZ. Solution there was to re-initialize sprites/layers and make sure that they were destroyed properly when their container was destroyed.

But this is just one case that can cause WEB GL CTX LOSS.
@Raizen, that solution you provided somehow slipped my attention, but I will try it, and this is my hardware specification for further answers:

OS: Windows 7 Home Basic, 64bit 6.1.7601 SP1 build 7601 (But I'm thinking of switching to Zorin OS 15.1 Ultimate)
RAM: 8GB DDR3 (haha, pathetic, at least 16GB I should have)
GPU: AMD Radeon R7 370 2GB VRAM
CPU: AMD FX 8320 8-core 3,5GHz
SSD Storage 250GB + 1TB classic HDD.

For searching what might cause it I left the game on for the whole night with a heavy load on the map (a couple of parallel events, heavy rain, MOG's weather FX, god rays effects, and so on), nothing happened so it's hard for me to reproduce this "bug" and tell exactly in which moment it happens. I thought for a moment it might have to do something with the PIXI library, but I won't interfere in its code, since graphic libraries for html5 is completely something new for me.

@OcRam This is probably the wrong place to ask, but do you have a code that "reboots" sprites to without a need to reload the whole game?

Edit: That reminds me something, last time I noticed this error WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost. In the background was playing a Youtube HD clip of a random "YouTuber" and my default and only web browser is Brave.
@Raizen, that solution you provided somehow slipped my attention, but I will try it, and this is my hardware specification for further answers:

OS: Windows 7 Home Basic, 64bit 6.1.7601 SP1 build 7601 (But I'm thinking of switching to Zorin OS 15.1 Ultimate)
RAM: 8GB DDR3 (haha, pathetic, at least 16GB I should have)
GPU: AMD Radeon R7 370 2GB VRAM
CPU: AMD FX 8320 8-core 3,5GHz
SSD Storage 250GB + 1TB classic HDD.

For searching what might cause it I left the game on for the whole night with a heavy load on the map (a couple of parallel events, heavy rain, MOG's weather FX, god rays effects, and so on), nothing happened so it's hard for me to reproduce this "bug" and tell exactly in which moment it happens. I thought for a moment it might have to do something with the PIXI library, but I won't interfere in its code, since graphic libraries for html5 is completely something new for me.

@OcRam This is probably the wrong place to ask, but do you have a code that "reboots" sprites to without a need to reload the whole game?

Edit: That reminds me something, last time I noticed this error WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost. In the background was playing a Youtube HD clip of a random "YouTuber" and my default and only web browser is Brave.
Basically I solved that by just creating new sprite and remove it if it hasn't been removed for some reason (this function is called on Spriteset_Map.createLowerMapLayer):
Code:
    Spriteset_Map.prototype.createMyLayer = function () {
        // Clear layer container, just in case (you never know what other plugins do in this.terminate)
        if (this._myLayer !== undefined) this._tilemap.removeChild(this._myLayer);
        this._myLayer = new Sprite();
    };
Ooh, that's something new... Anyway, I wrote basing on that small plugin:
Code:
    var spriteMap = Spriteset_Map.prototype.createLowerLayer;
    Spriteset_Map.prototype.createLowerLayer = function() {
        spriteMap.call(this);
        this.createMyLayer();
    }
   
    Spriteset_Map.prototype.createMyLayer = function () {
        // Clear layer container, just in case (you never know what other plugins do in this.terminate)
        if (this._myLayer !== undefined) this._tilemap.removeChild(this._myLayer);
        this._myLayer = new Sprite();
    };

I'll play my game several times and see if the problem occurs, Three days of test should be enough to verify the issue, if the context is still losing, then I'll try on another PC, perhaps the rumors on the internet are true, about damaged display driver, or my graphic card slowly dies... When a movie (any kind, any player) is played full screen, the image more-less in the center of the screen is slightly shifted. It happens with games too (AAA and indie) however my friend who works as a repairman of IT hardware looked at my GPU 6 months ago and he did not anything suspicious (except leftovers of dust between fan arms - clumsy cleaning)

ttt.png
Ooh, that's something new... Anyway, I wrote basing on that small plugin:
Code:
    var spriteMap = Spriteset_Map.prototype.createLowerLayer;
    Spriteset_Map.prototype.createLowerLayer = function() {
        spriteMap.call(this);
        this.createMyLayer();
    }
   
    Spriteset_Map.prototype.createMyLayer = function () {
        // Clear layer container, just in case (you never know what other plugins do in this.terminate)
        if (this._myLayer !== undefined) this._tilemap.removeChild(this._myLayer);
        this._myLayer = new Sprite();
    };

I'll play my game several times and see if the problem occurs, Three days of test should be enough to verify the issue, if the context will be still losing, then I'll try on another PC, perhaps the rumors on the internet are true, about damaged display driver, or my graphic card slowly dies... When a movie (any kind, any player) is played full screen, the image more-less in the center of the screen is slightly shifted. It happens with games too (AAA and indie)

View attachment 223760
That was just an example without knowing what plugins are in use etc... That shouldn't fix the issue - or if it does - then it is just coincidence (PC had a bad day or something when it lost WEB GL CONTEXT 😛).

It might take huge amount of time to fix that - IF it is code related issue with new NWJS.
Well lets hope it will, we both - as well all users - might have a surprise, anyway. I'm getting back to test the game, maybe during these tests I will solve another problem - how to motivate myself to finish writing and eventing that damn 6th chapter - my fault, because I wanted something new and create dialogues like in witcher 3 - picked decisions affects other NPCs and triggers different dialogues.

@Raizen - I played game on localhost few months ago, nothing strange happened, only Firefox have problem,.
@autodidact mentioned it in my game's thread here.

Original thread: https://forums.rpgmakerweb.com/threads/webgl-context_lost_webgl-losecontext-context-lost.146874/