Skip to content

Commit 21882a2

Browse files
committed
Added Page Visibility handler and hooked into TimeStep.
1 parent 946266d commit 21882a2

4 files changed

Lines changed: 116 additions & 13 deletions

File tree

v3/src/boot/Game.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var Device = require('../device');
55

66
var AddToDOM = require('../dom/AddToDOM');
77
var DOMContentLoaded = require('../dom/DOMContentLoaded');
8+
var VisibilityHandler = require('./VisibilityHandler');
9+
var EventDispatcher = require('../events/EventDispatcher');
810

911
var TimeStep = require('./TimeStep');
1012
var CreateRenderer = require('./CreateRenderer');
@@ -26,6 +28,11 @@ var Game = function (config)
2628
this.isBooted = false;
2729
this.isRunning = false;
2830

31+
/**
32+
* @property {EventDispatcher} events - Global / Global Game System Events
33+
*/
34+
this.events = new EventDispatcher();
35+
2936
/**
3037
* @property {Phaser.AnimationManager} anims - Reference to the Phaser Animation Manager.
3138
*/
@@ -101,6 +108,11 @@ Game.prototype = {
101108
this.config.postBoot();
102109

103110
this.loop.start(this.step.bind(this));
111+
112+
VisibilityHandler(this.events);
113+
114+
this.events.on('DOCUMENT_HIDDEN', this.onHidden.bind(this));
115+
this.events.on('DOCUMENT_VISIBLE', this.onVisible.bind(this));
104116
},
105117

106118
step: function (time, delta)
@@ -132,6 +144,18 @@ Game.prototype = {
132144
}
133145

134146
renderer.postRender();
147+
},
148+
149+
onHidden: function ()
150+
{
151+
console.log('Game.hidden');
152+
this.loop.pause();
153+
},
154+
155+
onVisible: function ()
156+
{
157+
console.log('Game.visible');
158+
this.loop.resume();
135159
}
136160

137161
};

v3/src/boot/TimeStep.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ var TimeStep = function (game, config)
5050
this.lastTime = 0;
5151
this.frame = 0;
5252

53+
this._pauseTime = 0;
54+
5355
this.delta = 0;
5456
this.deltaIndex = 0;
5557
this.deltaHistory = [];
@@ -60,37 +62,60 @@ TimeStep.prototype.constructor = TimeStep;
6062

6163
TimeStep.prototype = {
6264

63-
start: function (callback)
65+
// Called when the visibility API says the game is 'hidden' (tab switch, etc)
66+
pause: function ()
6467
{
65-
if (this.started)
66-
{
67-
return this;
68-
}
68+
console.log('TimeStep.pause');
6969

70-
this.started = true;
71-
this.running = true;
70+
this._pauseTime = window.performance.now();
71+
},
72+
73+
// Called when the visibility API says the game is 'visible' again (tab switch, etc)
74+
resume: function ()
75+
{
76+
this.resetDelta();
77+
78+
this.startTime += this.time - this._pauseTime;
7279

80+
console.log('TimeStep.resume - paused for', (this.time - this._pauseTime));
81+
},
82+
83+
resetDelta: function ()
84+
{
7385
var now = window.performance.now();
7486

7587
this.time = now;
76-
this.startTime = now;
7788
this.lastTime = now;
7889
this.nextFpsUpdate = now + 1000;
7990
this.framesThisSecond = 0;
8091
this.frame = 0;
8192

8293
// Pre-populate smoothing array
8394

84-
var history = [];
85-
8695
for (var i = 0; i < this.deltaSmoothingMax; i++)
8796
{
88-
history[i] = this._target;
97+
this.deltaHistory[i] = this._target;
8998
}
9099

91100
this.delta = 0;
92101
this.deltaIndex = 0;
93-
this.deltaHistory = history;
102+
},
103+
104+
start: function (callback)
105+
{
106+
if (this.started)
107+
{
108+
return this;
109+
}
110+
111+
this.started = true;
112+
this.running = true;
113+
114+
this.deltaHistory = [];
115+
116+
this.resetDelta();
117+
118+
this.startTime = window.performance.now();
94119

95120
this.callback = callback;
96121

v3/src/boot/VisibilityHandler.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Event = require('../events/Event');
2+
3+
var VisibilityHandler = function (eventDispatcher)
4+
{
5+
var hiddenVar;
6+
7+
if (document.hidden !== undefined)
8+
{
9+
hiddenVar = 'visibilitychange';
10+
}
11+
else
12+
{
13+
var vendors = [ 'webkit', 'moz', 'ms' ];
14+
15+
vendors.forEach(function (prefix)
16+
{
17+
if (document[prefix + 'Hidden'] !== undefined)
18+
{
19+
document.hidden = function ()
20+
{
21+
return document[prefix + 'Hidden'];
22+
};
23+
24+
hiddenVar = prefix + 'visibilitychange';
25+
}
26+
27+
});
28+
}
29+
30+
var onChange = function (event)
31+
{
32+
if (document.hidden || event.type === 'pause')
33+
{
34+
eventDispatcher.dispatch(new Event('DOCUMENT_HIDDEN'));
35+
}
36+
else
37+
{
38+
eventDispatcher.dispatch(new Event('DOCUMENT_VISIBLE'));
39+
}
40+
};
41+
42+
// Does browser support it?
43+
// If not (like in IE9 or old Android) we need to fall back to blur / focus
44+
if (hiddenVar)
45+
{
46+
document.addEventListener(hiddenVar, onChange, false);
47+
}
48+
else
49+
{
50+
console.log('Fallback TODO');
51+
}
52+
};
53+
54+
module.exports = VisibilityHandler;

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'f039f9d0-30d8-11e7-9c0c-c51a7bcebb13'
2+
build: 'e3a798e0-344c-11e7-937e-fdfcde3f0dd0'
33
};
44
module.exports = CHECKSUM;

0 commit comments

Comments
 (0)