101
|
1 |
/** |
|
2 |
* scripts/stageview.js |
|
3 |
* |
|
4 |
* This is the starting point for your application. |
|
5 |
* Take a look at http://browserify.org/ for more info |
|
6 |
*/ |
|
7 |
|
|
8 |
/* global document: false */ |
|
9 |
|
|
10 |
'use strict'; |
|
11 |
|
|
12 |
|
|
13 |
var PIXI = require('pixi'); |
|
14 |
var _ = require('lodash'); |
|
15 |
|
|
16 |
var defaultConfig = { |
|
17 |
externalRefresh: false, |
|
18 |
logger: undefined, |
|
19 |
sceneWidth: 1920, |
|
20 |
sceneHeight: 1080, |
|
21 |
framerate: 25, |
|
22 |
sceneBgColor: 0xFFFFFF, |
|
23 |
canvasContainer: 'canvasContainer', |
|
24 |
timeContainer: 'timeStarted' |
|
25 |
}; |
|
26 |
|
|
27 |
function StageView(options) { |
|
28 |
|
|
29 |
var _this = this; |
|
30 |
var opts = _(options).defaults(defaultConfig).value(); |
|
31 |
|
|
32 |
var externalRefresh = opts.externalRefresh; |
|
33 |
|
|
34 |
this.logger = opts.logger; |
|
35 |
this.framerate = opts.framerate; |
|
36 |
var sceneBgColor = opts.sceneBgColor; |
|
37 |
var sceneWidth = opts.sceneWidth; |
|
38 |
var sceneHeight = opts.sceneHeight; |
|
39 |
var canvasContainer = opts.canvasContainer; |
|
40 |
var timeContainer = opts.timeContainer; |
|
41 |
|
|
42 |
//create an new instance of a pixi stage |
|
43 |
this.stage = new PIXI.Stage(sceneBgColor); |
|
44 |
//create a renderer instance. |
|
45 |
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight); |
|
46 |
var components = []; |
|
47 |
|
|
48 |
this.init = function() { |
|
49 |
|
|
50 |
if(typeof(canvasContainer) === 'string') { |
|
51 |
canvasContainer = document.getElementById(canvasContainer); |
|
52 |
} |
|
53 |
if(typeof(timeContainer) === 'string') { |
|
54 |
timeContainer = document.getElementById(timeContainer); |
|
55 |
} |
|
56 |
|
|
57 |
canvasContainer.appendChild(renderer.view); |
|
58 |
|
|
59 |
components.forEach(function(c){ |
|
60 |
c.init(); |
|
61 |
}); |
|
62 |
}; |
|
63 |
|
|
64 |
this.registerComponent = function(component) { |
|
65 |
components.push(component); |
|
66 |
this.stage.addChild(component.container); |
|
67 |
}; |
|
68 |
|
|
69 |
this.refresh = function() { |
|
70 |
components.forEach(function(c){ |
|
71 |
c.refresh(); |
|
72 |
}); |
|
73 |
renderer.render(this.stage); |
|
74 |
}; |
|
75 |
|
|
76 |
// Init page and intervals |
|
77 |
var refreshInterval; |
|
78 |
var refreshTimeInterval; |
|
79 |
var startTs; |
|
80 |
|
|
81 |
this.updateTime = function(){ |
|
82 |
var nbSec = (Date.now() - startTs) / 1000; |
|
83 |
var hours = Math.floor( nbSec / 3600 ) % 24; |
|
84 |
var minutes = Math.floor( nbSec / 60 ) % 60; |
|
85 |
var seconds = Math.floor(nbSec % 60); |
|
86 |
var timeStr = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds); |
|
87 |
timeContainer.innerHTML = timeStr; |
|
88 |
}; |
|
89 |
|
|
90 |
this.start = function() { |
|
91 |
|
|
92 |
startTs = Date.now(); |
|
93 |
if(!externalRefresh) { |
|
94 |
refreshInterval = setInterval(function() {_this.refresh();}, 1000/this.framerate); |
|
95 |
} |
|
96 |
refreshTimeInterval = setInterval(function() {_this.updateTime();}, 1000); |
|
97 |
|
|
98 |
components.forEach(function(c){ |
|
99 |
c.start(); |
|
100 |
}); |
|
101 |
}; |
|
102 |
|
|
103 |
this.stop = function() { |
|
104 |
if(!externalRefresh) { |
|
105 |
clearInterval(refreshInterval); |
|
106 |
} |
|
107 |
clearInterval(refreshTimeInterval); |
|
108 |
|
|
109 |
components.forEach(function(c){ |
|
110 |
c.stop(); |
|
111 |
}); |
|
112 |
}; |
|
113 |
|
|
114 |
|
|
115 |
this.log = function(m) { |
|
116 |
if(this.logger) { |
|
117 |
this.logger.log(m); |
|
118 |
} |
|
119 |
}; |
|
120 |
|
|
121 |
|
|
122 |
return this; |
|
123 |
} |
|
124 |
|
|
125 |
module.exports = { |
|
126 |
StageView: StageView |
|
127 |
}; |