-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.js
124 lines (103 loc) · 3.57 KB
/
space.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var attempt = false;
var gameover = false;
function newGame() {
gameover = false;
document.getElementById('result').style.display = 'none';
document.getElementById('error').style.display = 'none';
document.getElementById('explanation').style.display = 'block';
attempt = new Attempt();
document.getElementById('secondsToPress').innerHTML = attempt.millisecondsToPress/1000;
}
function keyUp() {
if (attempt.isPressing) {
attempt.isPressing = false;
attempt.stopTime = attempt.getCurrentTime();
attempt.showResult();
}
}
function keyDown(e) {
keyCode = (e.keyCode ? e.keyCode : e.which);
if (keyCode == 32) {
//Space pressed
if (gameover) {
newGame();
} else if (!gameover && !attempt.startTime) {
attempt.spacePressed();
}
}
}
function Attempt() {
//A number between 0 and 10 by one decimal(0.6 for example)
this.millisecondsToPress = Math.floor((Math.random()*100))*100;
this.startTime = false;
this.stopTime = false;
this.isPressing = false;
this.getCurrentTime = function() {
var d = new Date();
return d.getTime();
}
this.showResult = function() {
if (this.startTime && this.stopTime && !this.isPressing) {
document.getElementById('result').style.display = 'block';
var result = 0;
var secondsPressed = this.stopTime - this.startTime;
result = Math.abs(secondsPressed - this.millisecondsToPress);
document.getElementById('pressed').innerHTML = secondsPressed/1000;
document.getElementById('secondsWrong').innerHTML = result/1000;
} else {
document.getElementById('error').style.display = 'block';
document.getElementById('error').innerHTML = 'Something went wrong. Press spacebar to try again.';
}
$('.arc_big').css('border-spacing', getRotationDegrees($('.arc_big')));
$('.arc_big').removeClass('spin');
$('.arc_small').css('border-spacing', getRotationDegrees($('.arc_small')));
$('.arc_small').removeClass('spin_fast');
$('.arc_small').animate({ borderSpacing: 0 }, {
step: function(now,fx) {
$(this).css('-webkit-transform','rotate('+now+'deg)');
$(this).css('-moz-transform','rotate('+now+'deg)');
$(this).css('-ms-transform','rotate('+now+'deg)');
$(this).css('-o-transform','rotate('+now+'deg)');
$(this).css('transform','rotate('+now+'deg)');
},
duration:'slow'
}, 'linear');
$('.arc_big').animate({ borderSpacing: 0 }, {
step: function(now,fx) {
$(this).css('-webkit-transform','rotate('+now+'deg)');
$(this).css('-moz-transform','rotate('+now+'deg)');
$(this).css('-ms-transform','rotate('+now+'deg)');
$(this).css('-o-transform','rotate('+now+'deg)');
$(this).css('transform','rotate('+now+'deg)');
},
duration:'slow'
}, 'linear');
gameover = true;
}
this.spacePressed = function() {
this.isPressing = true;
this.startTime = attempt.getCurrentTime();
$('.arc_big').addClass('spin');
$('.arc_small').addClass('spin_fast');
document.getElementById('explanation').style.display = 'none';
}
}
document.addEventListener('keyup', keyUp, false);
document.addEventListener('keydown', keyDown, false);
$(document).ready(function() {
newGame();
});
function getRotationDegrees(obj) {
var matrix = obj.css("-webkit-transform") ||
obj.css("-moz-transform") ||
obj.css("-ms-transform") ||
obj.css("-o-transform") ||
obj.css("transform");
if(matrix !== 'none') {
var values = matrix.split('(')[1].split(')')[0].split(',');
var a = values[0];
var b = values[1];
var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
} else { var angle = 0; }
return (angle < 0) ? angle +=360 : angle;
}