-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
332 lines (295 loc) · 11.1 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<html>
<title>Basic Counting Game</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
body {
background: WHITE;
font-family: Verdana, Helvetica, Arial, sans-serif;
}
span.letter {
font-size: 16em;
font-weight: bold;
/*text-shadow: 8px 8px 11px rgba(150, 150, 150, 0.74);*/
text-shadow: -5px 0 black, 0 5px black, 5px 0 black, 0 -5px black;
z-index:-99;
}
#instructions {
z-index:99;
position: absolute;
bottom:40px;
font-size: 1.2em;
width:100%;
text-align:center;
}
select {
background: WHITE;
border:1px #444444 solid;
}
#infoAndCredits {
z-index:99;
position: absolute;
left:8px;
bottom:8px;
padding:2px;
font-size:0.8em;
}
#credits {
font-size:0.7em;
}
</style>
<script>
var max_answer = 6;
var min_answer = 2;
var allElements = [];
var count = 0;
var keyheld = false;
var voice = null;
var voices = null;
var gameTimer = null;
var celebrationTimer = null;
var processingKey = false;
var imageNumber = 0;
var correctInARow = 0;
var wrongInARow = 0;
var sounds = {};
var fixedImage = null;
var images = [
{
word: "Balls",
image: "https://upload.wikimedia.org/wikipedia/commons/7/7f/Generic_football.png",
imageCredits: "<a href='https://www.freeimages.com/photo/soccer-ball-1466760'>Ball by Christopher Bruno</a> & <a href='https://commons.wikimedia.org/wiki/File:Generic_football.png'>Ed g2s (CC BY-SA 2.0)</a>"
},
{
word: "Umbrellas",
image: "https://upload.wikimedia.org/wikipedia/commons/0/06/Umbrella.png",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Umbrella.png'>Umbrella - cdc.gov (PD)</a>"
},
{
word: "Banananas",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Banana_on_white_background_POST%C3%89RIS.jpg/193px-Banana_on_white_background_POST%C3%89RIS.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Banana_on_white_background_POST%C3%89RIS.jpg'>Bananana by User:Filo gèn (CC BY-SA 4.0)</a>"
},
{
word: "Flowers",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Lily_flower_on_white_background.jpg/250px-Lily_flower_on_white_background.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Lily_flower_on_white_background.jpg'>Lily flower PD image from pixnio.com</a>"
},
{
word: "Fox",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Vulpes_vulpes_(white_background).jpg/250px-Vulpes_vulpes_(white_background).jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Vulpes_vulpes_(white_background).jpg'>Fox PD image from Iconographia Zoologica</a>"
},
{
word: "Ducks",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Duck-293474_white_background.jpg/292px-Duck-293474_white_background.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Duck-293474_white_background.jpg'>Duck by Mariomassone & Kapa65, CC0</a>"
},
{
word: "Elephants",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Indian_elephant_white_background.jpg/250px-Indian_elephant_white_background.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Indian_elephant_white_background.jpg'>Indian elephant etching by S. Milne via wellcome trust (CC BY 4.0)</a>"
},
{
word: "Lemons",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Lemon_with_white_background.jpg/320px-Lemon_with_white_background.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Lemon_with_white_background.jpg'>Lemon photo by User:Bordercolliez CC0</a>"
},
{
word: "Eggs",
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/Egg_on_white_background.jpg/250px-Egg_on_white_background.jpg",
imageCredits: "<a href='https://commons.wikimedia.org/wiki/File:Egg_on_white_background.jpg'>Egg photo by Paolo Neo, public domain</a>"
},
{
word: "Fans",
image: "./images/fan-stopped.png",
imageCredits: "<a href='https://pixabay.com/illustrations/fan-appliance-home-equipment-3581090/'>Fan image by joealfaraby, public domain</a>",
celebration: "SwapImage",
celebrationImage: "./images/fan-animated.gif"
}
];
function init() {
fixedImage = getParameterByName("fixed-image");
newGame();
}
document.onkeyup = function(e) {
keyheld = false;
}
document.onkeydown = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode && !keyheld) {
var button = String.fromCharCode(charCode);
if (charCode>=48 && charCode<=57) {
number = charCode - 48;
console.log("number " + number);
numberPressed(number);
}
}
};
document.onclick = function() {
};
function numberPressed(number) {
document.getElementById('instructions').style.display = 'none';
if (number == count) {
correctNumberPressed();
} else {
wrongNumberPressed();
}
}
function correctNumberPressed() {
if (processingKey) return;
processingKey = true;
correctInARow++;
wrongInARow = 0;
clearAll();
if (correctInARow==2) {
playSound("well-done");
correctInARow = 0;
} else {
playSound("correct");
}
if (images[imageNumber]['celebration']=="SwapImage") {
showImageByURL(count, images[imageNumber]['celebrationImage']);
} else {
document.getElementById('credits').style.display = "none";
celebrationTimer = setInterval( function(){ numberFlickerCelebration() }, 80);
}
// New game after 1.3 seconds
setTimeout(function() {
clearInterval(celebrationTimer);
document.body.style.backgroundColor = "WHITE";
newGame();
processingKey = false;
}, 1300);
}
function numberFlickerCelebration() {
if (!processingKey) return;
drawColourfulText(count);
if (Math.random()<0.3) document.body.style.backgroundColor = randomLightColour();
}
function wrongNumberPressed() {
if (processingKey) return
clearAll();
wrongInARow++;
correctInARow = 0;
if (wrongInARow==5) {
playSound("not-guessing")
wrongInARow = 0;
} else {
playSound("wrong");
}
// Re-draw images afer 2 seconds
gameTimer = setTimeout(function() {
showImageByNumber(count, imageNumber);
processingKey = false;
}, 3000);
}
function newGame() {
clearAll();
// Choose a random count, not the same as the previous
prevCount = count;
do {
count = Math.floor(Math.random() * (max_answer - min_answer + 1)) + min_answer;
}
while (count==prevCount)
chooseImage();
showImageByNumber(count, imageNumber);
}
function chooseImage() {
if (fixedImage!=null) {
// fixedImage ("fixed-image" URL param) is set
for (var i = 0; i < images.length; i++) {
if (images[i]["word"].toLowerCase() == fixedImage) {
imageNumber = i;
}
}
} else {
imageNumber = Math.floor(Math.random() * images.length);
}
}
function clearAll() {
while (allElements.length > 0) {
var removed = allElements.shift();
document.body.removeChild(removed);
}
}
function showImageByNumber(count, imageNumber) {
var imageData = images[imageNumber];
var imageURL = imageData['image'];
var imageCredits = imageData['imageCredits'];
showImageByURL(count, imageURL);
document.getElementById('credits').style.display = "block";
document.getElementById('credits').innerHTML = imageCredits;
}
function showImageByURL(count, imageURL) {
for (i = 0; i < count; i++) {
var elem = document.createElement("span");
elem.setAttribute("class", "image");
elem.innerHTML = "<img src='" + imageURL + "'>";
elem.style.position = "absolute";
document.body.appendChild(elem);
// TODO: position with CSS grid instead of this dodgy coordinate logic
//var x = Math.round(Math.random() * (window.innerWidth - elem.clientWidth));
//var y = Math.round(Math.random() * (window.innerHeight - elem.clientHeight));
var x = 50 + (i % 3) * 300; //elem.clientWidth;
var y = Math.floor(i / 3) * 300;
elem.style.left = x + "px";
elem.style.top = y + "px";
allElements.push(elem);
}
}
function drawColourfulText(text) {
var elem = document.createElement("span");
elem.setAttribute("class", "letter");
elem.textContent = text;
elem.style.position = "absolute";
document.body.appendChild(elem);
var x = Math.round(Math.random() * (window.innerWidth - elem.clientWidth));
var y = Math.round(Math.random() * (window.innerHeight - (elem.clientHeight - 100))) - 70;
elem.style.left = x + "px";
elem.style.top = y + "px";
elem.style.color = randomLightColour(); //colours[Math.floor(Math.random()*colours.length)];
allElements.push(elem);
}
function randomLightColour() {
var hue = Math.floor(Math.random() * 360);
var saturation = 100;
var varience = 50;
return 'hsl(' + hue + ', ' + saturation + '%, ' + varience + '%)';
}
// http://stackoverflow.com/a/901144/338265
function getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function playSound(name) {
if (typeof sounds[name] === 'undefined') sounds[name] = new Audio(name + ".mp3");
sounds[name].play()
}
</script>
</html>
<body onLoad="init();">
<div id="instructions">
<p><b>Hit the number keys</b></p>
<p>(if you need to learn how to count)</p>
</div>
<div id="infoAndCredits">
(<a href="https://github.com/harry-wood/counting-basic" title="README description, code, and issue tracker">Github</a>)
<span id="credits">
</span>
</div>
</body>
</html>