X-Git-Url: https://git-public.kairo.at/?p=lantea.git;a=blobdiff_plain;f=js%2Fmap.js;h=ec9a05c0c0d324fdaf8384552d2bb8fd197e082b;hp=cf095cb13f3d199e4226791a4fbde8dfa5ebcb9c;hb=16e4f664efa5ad905ffa77ef38808c325c98185c;hpb=61b46e9e7d19b38d69ad53aa776bc5001109c5f3 diff --git a/js/map.js b/js/map.js index cf095cb..ec9a05c 100644 --- a/js/map.js +++ b/js/map.js @@ -72,11 +72,19 @@ function initMap() { gGLMapCanvas.getContext("experimental-webgl", {depth: false}); } catch(e) {} - // If we don't have a GL context, give up now if (!gMap.gl) { + // If we don't have a GL context, give up now showGLWarningDialog(); gMap.gl = null; } + else { + // GL context can be lost at any time, handle that. + // See http://www.khronos.org/webgl/wiki/HandlingContextLost + gGLMapCanvas.addEventListener("webglcontextlost", + gMap.handleContextLost, false); + gGLMapCanvas.addEventListener("webglcontextrestored", + gMap.handleContextRestored, false); + } gTrackCanvas = document.getElementById("track"); gTrackContext = gTrackCanvas.getContext("2d"); if (!gMap.activeMap) @@ -235,7 +243,7 @@ var gMap = { ' gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);\n' + ' vTextureCoord = aTextureCoord;\n' + '}'; }, - getFragShaderSource:function() { + getFragShaderSource: function() { return 'varying highp vec2 vTextureCoord;\n\n' + 'uniform sampler2D uImage;\n\n' + 'void main(void) {\n' + @@ -312,8 +320,13 @@ var gMap = { gMap.glTxCleanIntervalID = window.setInterval(gMap.cleanTextures, 30 * 1000); } - var throwEv = new CustomEvent("mapinit-done"); - gAction.dispatchEvent(throwEv); + if (!gAppInitDone) { + // We may be called when context was lost and destroyed, + // only send event when we are in app startup + // (gAppInitDone is set to true right after we return this event). + var throwEv = new CustomEvent("mapinit-done"); + gAction.dispatchEvent(throwEv); + } }, draw: function() { @@ -503,9 +516,21 @@ var gMap = { } } console.log("Cleaning complete, " + Object.keys(gMap.glTextures).length + " textures left)"); - //clearInterval(gMap.glTxCleanIntervalID); } }, + + handleContextLost: function(event) { + event.preventDefault(); + // GL context is gone, let's reset everything that depends on it. + clearInterval(gMap.glTxCleanIntervalID); + gMap.glTextures = {}; + }, + + handleContextRestored: function(event) { + // When GL context is back, init GL again and draw. + gMap.initGL(); + gMap.draw(); + }, } // Using scale(x, y) together with drawing old data on scaled canvas would be an improvement for zooming.