-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaction.html
237 lines (229 loc) · 6.31 KB
/
reaction.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Reaction Tester</title>
<style>
*,
body {
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
background-color: #c8f9fa;
}
canvas {
display: none;
width: 100vw;
height: 100vh;
}
.centered {
height: 500px;
width: 500px;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -200px;
}
.restart {
display: none;
}
.btn {
background-color: green;
padding: 1rem;
margin: 2rem;
font-size: large;
}
.orange {
background-color: orange;
}
.red {
background-color: red;
}
</style>
</head>
<body onresize="resize()">
<canvas id="canvas" height="1000" width="1000"></canvas>
<div class="centered start">
<h3>Start the Game - Click on the Green highlighted tabs quickly</h3>
<button class="btn" onclick="event.stopPropagation(); start(1.5);">
Easy
</button>
<button class="btn orange" onclick="event.stopPropagation(); start(1.2);">
Medium
</button>
<button class="btn red" onclick="event.stopPropagation(); start(1);">
Hard
</button>
</div>
<div class="centered restart">
<h3>Your Score: <span id="score"></span></h3>
<button class="btn" onclick="event.stopPropagation(); location.reload();">
Restart
</button>
</div>
<script type="text/javascript">
// Constants
var canvas = document.querySelector("#canvas");
var ctx = canvas.getContext("2d");
const noOfXTiles = 4;
const noOfYTiles = 4;
const background = "#000000";
const padColour = "#c8f9fa";
const activePadColour = "#003d15";
const lineColour = "#5102fa";
var padList = [];
var activePad = 0;
var timer;
var score = 0;
var difficulty = 1;
var pad = {
x: 0,
y: 0,
h: 0,
w: 0,
color: padColour,
draw: function () {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.w, this.h);
ctx.closePath();
},
isHit: function (cX, cY) {
return (
this.x <= cX &&
cX <= this.x + this.w &&
this.y <= cY &&
cY <= this.y + this.h
);
},
};
/**
* Resize(Redraw) the canvas to fit the whole browser size
*/
function resize() {
var width = ctx.canvas.clientWidth;
var height = ctx.canvas.clientHeight;
ctx.canvas.width = width;
ctx.canvas.height = height;
ctx.beginPath();
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.closePath();
drawPad(height, width);
startTimer();
}
/**
* Start a game
* @param d game difficulty
*/
function start(d) {
difficulty = d;
document.querySelector(".start").style.display = "none";
score = 0;
document.querySelector("#canvas").style.display = "block";
resize();
//capture mouse click event to check if correct pad was clicked
document.addEventListener("click", onDocumentClick);
}
/**
* End the game
*/
function end() {
clearTimer();
document.querySelector(".restart").style.display = "block";
document.querySelector("#canvas").style.display = "none";
document.querySelector("#score").innerHTML = score;
//remove mouse click event to check if correct pad was clicked
document.removeEventListener("click", onDocumentClick);
}
/**
* On click of document
* @param e click event
*/
function onDocumentClick(e) {
clearTimer();
isClickOnTarget(activePad, e.clientX, e.clientY) ? resize() : end();
}
/**
* Start the timer
*/
function startTimer() {
clearTimer();
timer = setTimeout(end, difficulty * 1000);
}
/**
* Clear the timer
*/
function clearTimer() {
clearTimeout(timer);
}
/**
* Draw the pads
* @param h height
* @param w width
*/
function drawPad(h, w) {
padList = [];
// get a random number between 0 and no of pads
activePad = Math.floor(Math.random() * noOfXTiles * noOfYTiles);
var tagWidth = w / noOfXTiles;
var tagHeight = h / noOfYTiles;
var current = 0;
//draw the pads
for (var i = 0; i < noOfXTiles; i++) {
for (var j = 0; j < noOfYTiles; j++) {
var p = Object.create(pad);
p.x = i * tagWidth;
p.y = j * tagHeight;
p.h = tagHeight;
p.w = tagWidth;
if (current == activePad) {
p.color = activePadColour;
}
current++;
p.draw();
padList.push(p);
}
}
//draw the horizontal lines
for (var i = noOfYTiles, x = 0; i > 0; i--) {
x += tagWidth;
ctx.beginPath();
ctx.strokeStyle = lineColour;
ctx.moveTo(x, 0);
ctx.lineWidth = 5;
ctx.lineTo(x, h);
ctx.stroke();
ctx.closePath();
}
//draw the vertical lines
for (var i = noOfXTiles, y = 0; i > 0; i--) {
y += tagHeight;
ctx.beginPath();
ctx.strokeStyle = lineColour;
ctx.moveTo(0, y);
ctx.lineWidth = 5;
ctx.lineTo(w, y);
ctx.stroke();
ctx.closePath();
}
}
/**
* Check if mouseclick event coordinate lie inside the active pad
* @param active current active pad index
* @param cX x coordinate of mouseclick
* @param cY y coordinate of mouseclick
*/
function isClickOnTarget(active, cX, cY) {
var isHit = padList[active].isHit(cX, cY);
if (isHit) {
score++;
}
return isHit;
}
</script>
</body>
</html>