-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.html
262 lines (206 loc) · 6.61 KB
/
play.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>THAT'S MY CHAIR</title>
<link rel="stylesheet" href="static/css/bootstrap.min.css">
<style>
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var number = 10;
var canvas = null;
var ctx = null;
var chairs = [];
var players = [];
var duration;
var myVar;
var started = false;
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'dQw4w9WgXcQ',
events: {
'onReady': onPlayerReady,
'onStateChange': onStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
$('#player').hide();
event.target.playVideo();
started = true;
}
function onStateChange(event) {
if (started) {
duration = Math.random() * 1000 * 15 + 3000;
setTimeout('pause()', duration);
}
}
function square(x) {
return x * x;
}
function pause() {
clearInterval(myVar);
player.pauseVideo();
date = new Date();
started = false;
$('canvas').click(function() {
d = new Date() - date;
players[0].delay = d;
if (number == 2) {
var r = confirm("Your response time was: " + players[0].delay + ". Restart?");
if (r == true) {
number = 10;
}
restart(number);
}
else {
var r = confirm("Your response time was: " + players[0].delay + ". Next level?");
if (r == true) {
number -= 1;
}
restart(number);
}
});
function restart(number) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
chairs = [];
players = [];
setup(number);
player.playVideo();
started = true;
}
for (var i = 1; i < players.length; i++) {
players[i].delay = Math.random() * 1000 + 100;
}
/*for (var i = 0; i < players.length; i++) {
var closest = chairs[0];
player = players[i];
for (var j = 0; j < chairs.length; j++) {
chair = chairs[i];
if ((square(player.x - chair.x) + square(player.y - chair.y)) < (square(player.x - closest.x) + square(player.y - closest.y))) {
closest = chair[i];
}
player.chair = closest;
}
ctx.drawImage(player.picture, player.chair.x, player.chair.y);
}*/
}
var radius = 130;
var outer_radius = 210;
var x_offset = 450;
var y_offset = 220;
function setup(number) {
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 800;
if ($('#playground').children().length > 0){
$('canvas').replaceWith(canvas);
}
else {
$('#playground').append(canvas);
}
var angle = 0;
var index = 0;
var drawChairs = function() {
var x = Math.sin(angle) * radius + x_offset;
var y = Math.cos(angle) * radius + y_offset;
angle = angle + 2*Math.PI/(number - 1);
chairs[index].x = x;
chairs[index].y = y;
index++;
}
for (var i = 0; i < number - 1; i++) {//initialize chair objects
var img = new Image();
img.onload = drawChairs;
img.src = '/static/chair.jpg';
var chair = new Chair(img);
chairs.push(chair);
}
var outer_angle = 0;
var human_index = 0;
var drawPlayer = function() {
var x = Math.sin(outer_angle) * outer_radius + x_offset;
var y = Math.cos(outer_angle) * outer_radius + y_offset;
players[human_index].x = x;
players[human_index].y = y;
players[human_index].angle = outer_angle;
outer_angle = outer_angle + 2*Math.PI/number;
human_index++;
}
for (var i = 0; i < number; i++) {//initialize players objects
var img = new Image();
img.onload = function() {
drawPlayer();
}
if (i == 0) {
img.src = '/static/you.jpg';
var person = new Human(img);
}
else {
img.src = '/static/smiley.jpg';
var person = new Player(img);
}
players.push(person);
}
function Player(picture) {
this.picture = picture;
this.chair = null;
}
function Human(picture) {
this.picture = picture;
this.chair = null;
this.human = true;
}
function Chair(picture) {
this.picture = picture;
this.person = null;
this.x = 0;
this.y = 0;
}
function animate(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "bold 12px sans-serif";
ctx.fillText("Click screen", 450, 240);
ctx.fillText("when music stops", 440, 270);
for (var i = 0; i < chairs.length; i++) {
var chair = chairs[i];
ctx.drawImage(chair.picture, chair.x, chair.y);
}
for (var i = 0; i < players.length; i++) {
var person = players[i];
ctx.drawImage(person.picture, person.x, person.y);
person.angle = person.angle + Math.PI/360;
person.x = Math.sin(person.angle) * outer_radius + x_offset;
person.y = Math.cos(person.angle) * outer_radius + y_offset;
}
}
myVar = setInterval(function(){animate()},50);
};
function Main() {
setup(number);
return false;
};
$(document).ready(Main);
</script>
</head>
<body>
<div id="playground"></div>
<!--<form>
<input type="submit" value="Next Level">
</form>-->
<div id="player"></div>
</body>
</html>