From eae1a76341a591ed42d7837b69bf2c9aa3195935 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 25 Feb 2018 14:05:58 +0100 Subject: [PATCH] remoteMap: 60fps, option to pause when window is deselected to save CPU --- static/remoteMap.html | 2 ++ static/remoteMap.js | 45 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/static/remoteMap.html b/static/remoteMap.html index b4920c59..19cf4926 100644 --- a/static/remoteMap.html +++ b/static/remoteMap.html @@ -15,6 +15,8 @@

Map size: (default 32)

Tile size: (default 8)

+

FPS:

+

Pause when window is deselected (saves CPU)

\ No newline at end of file diff --git a/static/remoteMap.js b/static/remoteMap.js index 5689ebb5..ca6f320c 100644 --- a/static/remoteMap.js +++ b/static/remoteMap.js @@ -37,6 +37,13 @@ var remoteMapConfig = JSON.parse(localStorage.remoteMapConfig); localStorage.remoteMapConfig = JSON.stringify(config); } } + let pauseOnBlur = document.querySelector("#pauseOnBlur"); + if(pauseOnBlur){ + pauseOnBlur.checked = localStorage.remoteMapPauseOnBlur || true; + pauseOnBlur.onchange = function(){ + localStorage.remoteMapPauseOnBlur = pauseOnBlur.checked; + } + } })(); var socket = io.connect(document.location.origin); @@ -109,25 +116,25 @@ Mousetrap.bind("s", e => { console.log("s"); cache.walkUp(); playerPosition.y += remoteMapConfig.tileSize; - clear();drawFromCache(); + clear();//drawFromCache(); }); Mousetrap.bind("d", e => { console.log("d"); cache.walkLeft(); playerPosition.x += remoteMapConfig.tileSize; - clear();drawFromCache(); + clear();//drawFromCache(); }); Mousetrap.bind("w", e => { console.log("w"); cache.walkDown(); playerPosition.y -= remoteMapConfig.tileSize; - clear();drawFromCache(); + clear();//drawFromCache(); }); Mousetrap.bind("a", e => { console.log("a"); cache.walkRight(); playerPosition.x -= remoteMapConfig.tileSize; - clear();drawFromCache(); + clear();//drawFromCache(); }); var entityCache = new Array(remoteMapConfig.mapSize); // populate cache with arrays of arrays @@ -192,6 +199,36 @@ function drawFromCache(){ }); }); } +window.requestAnimationFrame(renderLoop); +var fpsTimings = { + lastFrame: Date.now(), + averageLength: 60, + sum:0, + counter: document.querySelector("#fpsCounter"), +} +var isPaused = false; +window.onblur = function() { + if(localStorage.remoteMapPauseOnBlur == "true"){ + isPaused = true; + console.log("paused"); + } +} +window.onfocus = function() { + if(isPaused){ + isPaused = false; + console.log("unpaused"); + window.requestAnimationFrame(renderLoop); + } +} +function renderLoop(){ + let newTimestamp = Date.now(); + fpsTimings.sum = fpsTimings.sum / fpsTimings.averageLength * (fpsTimings.averageLength - 1); + fpsTimings.sum += newTimestamp - fpsTimings.lastFrame; + fpsTimings.counter.value = (1000 / (fpsTimings.sum / fpsTimings.averageLength)).toPrecision(4); + fpsTimings.lastFrame = newTimestamp; + if(!isPaused) drawFromCache(); + if(!isPaused) window.requestAnimationFrame(renderLoop); +} var entityImages = {}; // cache to store images and details about entities, populated by drawEntity(); function drawEntity(entity, dontCache){