-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
152 lines (127 loc) · 4.29 KB
/
index.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html>
<head>
<title>Running App!</title>
<link rel="stylesheet" href="stylesheets/screen.css">
<script type="text/javascript" src="javascript/jquery.js" ></script>
<script type="text/javascript" src="javascript/jquery.fittext.js" ></script>
<script type="text/javascript" src="javascript/jquery.transition.js" ></script>
<script type="text/javascript">
var defaultAction = "run";
var defaultColor = "green";
var actionsArray = ["leap", "jump", "duck", "tornado", "chicken", "swim"];
var colorsArray = ["plum", "gold", "crimson", "darkgray", "coral", "aqua" ];
var isPlaying = false; // Are we playing?
var isAction = false; // Are we showing someing in actionsArray?
var defaultTimer = 3500; // 3 seconds
var actionTimer = 1500; // 1 second
// Keep text in viewport
$(document).ready(function() {
$('[data-behavior~=fit_text]').fitText(0.7);
});
var Game = {
// Main game loop
play: function(timer) {
var self = this;
this.interval = setInterval(function(){
self.animateAction();
if(isAction) {
self.resetDefaultAction();
self.resetTimer(defaultTimer);
} else {
self.pickRandomAction();
self.resetTimer(actionTimer);
}
}, timer);
isPlaying = true;
},
// Stop playing
pause: function() {
clearInterval(this.interval);
isPlaying = false;
},
// Reset timer to a new value
resetTimer: function(timer){
this.pause();
this.play(timer);
},
// Picks a random play action from the actionsArray
pickRandomAction: function() {
var random = Math.floor(Math.random() * actionsArray.length);
var color = colorsArray[random];
var action = actionsArray[random];
$('.action').text(action);
$('body').css( "background-color", color );
isAction = true;
},
// Resets the text to the default action
resetDefaultAction: function() {
$('.action').text(defaultAction);
$('body').css( "background-color", '' );
isAction = false;
},
// Handle start/stop clicks
toggleButton: function(button) {
if(isPlaying) {
$(button).removeClass('controls__button--playing').text('Resume');
this.pause();
Clock.pause();
} else {
$(button).addClass('controls__button--playing').text('Pause');
this.resetDefaultAction();
this.play(defaultTimer);
this.animateAction();
Clock.resume();
}
},
// Animate the action label
animateAction: function() {
$('[data-role~=action_container]').transitionClass('action-container__action--animated');
}
};
// React to start/stop button clicks
$(document).on('click', '[data-behavior~=toggle_play]', function(event) {
Game.toggleButton(this);
event.preventDefault();
});
// Handle tapping the spacebar
$(document).keyup(function(event){
if(event.keyCode == 32){
Game.toggleButton('[data-behavior~=toggle_play]');
}
});
// Clock timer
var Clock = {
totalSeconds: 0,
start: function () {
var self = this;
function pad(val) { return val > 9 ? val : "0" + val; }
this.interval = setInterval(function () {
self.totalSeconds += 1;
$('[data-role~=clock_minutes]').text(pad(Math.floor(self.totalSeconds / 60 % 60)));
$('[data-role~=clock_seconds]').text(pad(parseInt(self.totalSeconds % 60)));
}, 1000);
},
pause: function () {
clearInterval(this.interval);
delete this.interval;
},
resume: function () {
if (!this.interval) this.start();
}
};
</script>
</head>
<body>
<div class="action-container">
<div class="controls">
<a href="#" class="controls__button" data-behavior="toggle_play">Start</a>
</div>
<div class="clock">
<span data-role="clock_minutes">00</span>:<span data-role="clock_seconds">00</span>
</div>
<div class="action-container__action" data-role="action_container" data-behavior="fit_text">
<span class="action">run</span>!
</div>
</div>
</body>