From 5dd2ab70fabaf7bf0f74d0975f7d9edce1f264d2 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Fri, 12 Sep 2014 04:16:28 +0200 Subject: [PATCH] try saving power by only drawing when we are actually visible --- js/map.js | 35 ++++++++++++++++++++++++++--------- manifest.appcache | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/js/map.js b/js/map.js index b3ddc11..ffafab4 100644 --- a/js/map.js +++ b/js/map.js @@ -129,6 +129,8 @@ function loadPrefs(aEvent) { document.getElementById("body").addEventListener("keydown", mapEvHandler, false); + document.addEventListener("visibilitychange", visibilityEvHandler, false); + console.log("Events added."); document.getElementById("copyright").innerHTML = gMapStyles[gMap.activeMap].copyright; @@ -377,7 +379,9 @@ var gMap = { var imgObj = new Image(); imgObj.onload = function() { gMap.loadImageToTexture(imgObj, aTileKey); - window.requestAnimationFrame(function(aTimestamp) { gMap.drawGL() }); + if (document.hidden != true) { // Only draw if we're actually visible. + window.requestAnimationFrame(function(aTimestamp) { gMap.drawGL() }); + } URL.revokeObjectURL(imgURL); } imgObj.src = imgURL; @@ -386,7 +390,9 @@ var gMap = { } } } - window.requestAnimationFrame(function(aTimestamp) { gMap.drawGL() }); + if (document.hidden != true) { // Only draw if we're actually visible. + window.requestAnimationFrame(function(aTimestamp) { gMap.drawGL() }); + } }, drawGL: function() { @@ -645,13 +651,15 @@ function decodeIndex(encodedIdx) { } function drawTrack() { - gLastDrawnPoint = null; - gCurPosMapCache = undefined; - gTrackContext.clearRect(0, 0, gTrackCanvas.width, gTrackCanvas.height); - if (gTrack.length) { - for (var i = 0; i < gTrack.length; i++) { - drawTrackPoint(gTrack[i].coords.latitude, gTrack[i].coords.longitude, - (i + 1 >= gTrack.length)); + if (document.hidden != true) { // Only draw if we're actually visible. + gLastDrawnPoint = null; + gCurPosMapCache = undefined; + gTrackContext.clearRect(0, 0, gTrackCanvas.width, gTrackCanvas.height); + if (gTrack.length) { + for (var i = 0; i < gTrack.length; i++) { + drawTrackPoint(gTrack[i].coords.latitude, gTrack[i].coords.longitude, + (i + 1 >= gTrack.length)); + } } } } @@ -960,6 +968,15 @@ var mapEvHandler = { } }; +function visibilityEvHandler() { + // Immediately draw if we just got visible. + if (document.hidden != true) { + gMap.draw(); + } + // No need to handle the event where we become invisible as we care only draw + // when we are visible anyhow. +} + var geofake = { tracking: false, lastPos: {x: undefined, y: undefined}, diff --git a/manifest.appcache b/manifest.appcache index 02b8be8..37528eb 100644 --- a/manifest.appcache +++ b/manifest.appcache @@ -1,6 +1,6 @@ CACHE MANIFEST -# 2014-08-17 +# 2014-09-11 index.html manifest.webapp js/map.js -- 2.43.0