-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
68 lines (51 loc) · 1.97 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { WaveGroup } from './wavegroup.js';
import { Figure } from './figure.js';
import { FpsCtrl } from './fpsCtrl.js';
class App {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
this.waveGroup = new WaveGroup();
this.figure = new Figure();
const mobile = this.isMobile();
const startEvt = (mobile) ? 'touchstart' : 'mousedown';
const moveEvt = (mobile) ? 'touchmove' : 'mousemove';
const endEvt = (mobile) ? 'touchend' : 'mouseup';
const fpsCtrl = new FpsCtrl(60, this.animate.bind(this));
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
document.getElementById('github').addEventListener('click', () => {
window.open('https://github.com/unsignd/Falling');
});
window.addEventListener(moveEvt, this.figure.update.bind(this.figure), false);
window.addEventListener(startEvt, this.figure.startClick.bind(this.figure), false);
window.addEventListener(endEvt, this.figure.endClick.bind(this.figure), false);
fpsCtrl.start();
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
this.canvas.width = this.stageWidth * 2;
this.canvas.height = this.stageHeight * 2;
this.ctx.scale(2, 2);
this.waveGroup.resize(this.stageWidth, this.stageHeight);
this.figure.rects = [];
}
animate() {
this.ctx.clearRect(0, 0, this.stageWidth, this.stageHeight);
this.figure.draw(this.ctx, this.stageHeight);
this.waveGroup.draw(this.ctx);
this.animate.bind(this);
}
isMobile() {
if (navigator.userAgent.indexOf('Mobile') != -1) {
return true;
} else {
return false;
}
}
}
window.onload = () => {
new App();
};