-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
525 lines (394 loc) · 11.6 KB
/
client.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// shared config
var width = 960,
height = 540,
modulus = 10000;
// helper functions
function el(id){return document.getElementById(id)}
// ** Calibration code **
// the things we are trying to find out about the client
var x, y, offset;
// todo - load from session storage
// observed calibration timestamps
var calibrations = [
// {type, time}
];
// elements
var
calibrateRight = el('calibrate-right'),
calibrateLeft = el('calibrate-left'),
calibrateDown = el('calibrate-down'),
calibrateReset = el('calibrate-reset');
// handlers for populating the calibrations array
var handlers = (function(){
return {
right: make('right'),
left: make('left'),
down: make('down'),
prevent: prevent
}
function make(name){
return function(e){
prevent(e)
calibrations.push({
type: calibrated[name] ? 'ignore' : name,
time: + new Date()
})
bean.fire(calibrations, 'modified')
}
}
function prevent(e){
if(e && e.preventDefault) e.preventDefault()
}
})()
// no click for now - want make it instant
bean.on(calibrateRight, 'mousedown touchstart', handlers.right)
bean.on(calibrateLeft, 'mousedown touchstart', handlers.left)
bean.on(calibrateDown, 'mousedown touchstart', handlers.down)
// disable clicks
bean.on(calibrateRight, 'click', handlers.prevent)
bean.on(calibrateLeft, 'click', handlers.prevent)
bean.on(calibrateDown, 'click', handlers.prevent)
// resetting
bean.on(calibrateReset, 'click', function(e){
e.preventDefault();
if(window.confirm("Discard calibration data?")){
reset_calibration()
}
});
bean.on(calibrations, 'modified', function(){
calibrateReset.style.display = 'inline'
})
// ** Calibration Status **
var calibrated = {left:null,right:null,down:null};
// look for calibration
function process(){
if(!calibrated.left){
calibrated.left = point(
calibrations.filter(function(c){
return c.type == 'left'
}).map(function(d){
return d.time
})
);
}
if(!calibrated.right){
calibrated.right = point(
calibrations.filter(function(c){
return c.type == 'right'
}).map(function(d){
return d.time
})
);
}
if(!calibrated.down){
calibrated.down = point(
calibrations.filter(function(c){
return c.type == 'down'
}).map(function(d){
return d.time
})
);
}
var right = calibrated.right,
left = calibrated.left,
down = calibrated.down;
if(offset && x && y) return console.log("completed")
if(right !== null && left !== null){
// global
offset = ps.average([left, right], modulus);
// global
// -1 -> 1
x = (ps.distance(offset, right, modulus) / (0.25*modulus));
// 0 -> 1
x = (x/2)+0.5;
// 0 -> w
// x = x*w;
console.log("X", x);
if(down !== null){
// global
y = (ps.distance(offset, down, modulus) / (0.25*modulus));
// 0 -> 1
y = (y/2)+0.5;
// 0 -> h
// y = y*h;
console.log("Y", y);
localStorage.setItem('calibration', [x, y, offset].join(','))
// debug.append('circle')
// .attr('r',10)
// .attr('cx',x)
// .attr('cy',y)
// .attr('fill','rgba(255,0,0,0.3)')
}
}
}
// the most recent averaged point
function point(timestamps){
// are the last two close enough to be a match?
var last = timestamps[timestamps.length - 1],
penultimate = timestamps[timestamps.length - 2];
if(last && penultimate){
var d = ps.distance(last, penultimate, modulus);
if(Math.abs(last - penultimate) < modulus/2){
console.log("pre-modulation skip") //found points, but too close together (pre modulation)
return null;
}
if(Math.abs(d) < modulus*0.1){//within a second?
var avg = ps.average([last, penultimate], modulus)
return avg;
} else {
console.log("post-modulation skip") // found points, but not close enough
}
}
return null;
}
bean.on(calibrations, 'modified', process)
// Calibration visuals
var w = 150,
h = 150;
var svg = d3.select("#calibration-status").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.append('g')
.attr('transform', 'translate(' + w/2 + ', ' + h/2 + ') scale('+Math.min(w,h)/2.5+')');
g
.append('circle')
.attr('r',1)
.attr('fill','none')
.attr('stroke','#eee')
.attr('stroke-width',0.02)
var points = g.selectAll('.point').data(calibrations);
var x_line = svg.append('line').attr({
x1:0, y1:0, x2: 0, y1: h,
stroke: '#eee','stroke-width':1,
transform: 'translate(-10, 0)'
})
var y_line = svg.append('line').attr({
x1:0, y1:0, x2:w, y1:0,
stroke: '#eee','stroke-width':1,
transform: 'translate(0, -10)'
})
var xy_circle = svg.append('circle').attr({
cx:-10,
cy:-10,
r:10,
fill: 'rgba(0, 255, 0, 0.6)'
})
var modRad = (Math.PI*2) / modulus;
function cx(d){
return d3.round(Math.sin(d.time*modRad),4)
}
function cy(d){
return d3.round(-Math.cos(d.time*modRad),4)
}
function render(){
points = points.data(calibrations)
points
.enter()
.append('circle')
.attr('class', 'point')
.attr('r', 0.1)
.attr('cx', cx)
.attr('cy', cy)
.attr('fill', function(d){
switch(d.type){
case 'left' : return '#ff7f0e'
case 'right': return '#08f'
case 'down' : return '#f08'
case 'ignore' : return 'rgba(0,0,0,0.2)'
}
return '#000'
});
points
.exit()
.remove();
if(x){
x_line.attr('transform','translate('+ (d3.round(x*w)+0.5) + ',0)')
xy_circle.attr('cx', (d3.round(x*w)+0.5))
}
if(y){
y_line.attr('transform','translate(0, '+ (d3.round(y*h)+0.5) + ')')
xy_circle.attr('cy', (d3.round(y*h)+0.5))
}
if(x && y){
document.getElementsByTagName('html')[0].className = 'calibrated'
}
calibrateRight.innerHTML = calibrated.right ? 'ok' : 'right';
calibrateLeft.innerHTML = calibrated.left ? 'ok' : 'left';
calibrateDown.innerHTML = calibrated.down ? 'ok' : 'down';
}
bean.on(calibrations, 'modified', render)
function reset_calibration(){
x=null; y=null, offset=null;
calibrated = {left:null,right:null,down:null};
calibrations.forEach(function(f){f.type = 'ignore'});
y_line.attr('transform','translate(0, -10)');
x_line.attr('transform','translate(-10, 0)');
xy_circle.attr('cx', -10);
xy_circle.attr('cy', -10);
points.attr('fill', 'rgba(0,0,0,0.1)');
calibrateReset.style.display = 'none'
document.getElementsByTagName('html')[0].className = '';
localStorage.removeItem('calibration')
render();
}
// DEMOS!!!
// ensure demo background has been loaded
(new Image).src = 'demo-bg.jpg';
var demoPanel = el('demo'),
demoClose = el('demo-close'),
demoTitle = el('demo-title'),
demoStart1 = el('demo-start-1'),
demoStart2 = el('demo-start-2'),
demoStart3 = el('demo-start-3'),
bodyElement = document.getElementsByTagName('body')[0];
var demoReset, totally;
bean.on(demoClose, 'click', function(e){
e.preventDefault();
if(totally || confirm('are you, like, totally sure you want to stop this?')){
demoPanel.style.display = 'none';
bodyElement.style.overflow = 'auto';
if(demoReset){
demoReset(); demoReset = null;
}
}
})
bean.on(window, 'keyup',function(e){
if (e.keyCode == 27){
if(totally || confirm('are you, like, totally sure you want to stop this?')){
demoPanel.style.display = 'none';
bodyElement.style.overflow = 'auto';
if(demoReset){
demoReset(); demoReset = null;
}
}
}
})
/*
bean.on(demoStart1, 'click', function(e){
e.preventDefault();
demoPanel.style.display = 'block';
bodyElement.style.overflow = 'hidden';
demoTitle.innerHTML = 'Demo #1';
demoPanel.className = 'bg-position';
demoPanel.style.backgroundPosition = [
Math.round((x||0)*100) + '%',
Math.round((y||0)*100) + '%'
].join(' ')
demoReset = function(){
demoPanel.className = '';
demoPanel.style.backgroundPosition = '';
}
})
bean.on(demoStart2, 'click', function(e){
e.preventDefault();
demoPanel.style.display = 'block';
bodyElement.style.overflow = 'hidden';
demoTitle.innerHTML = 'Demo #2'
demoPanel.style.backgroundColor = fillHSL(x||0, y||0);
demoReset = function(){
demoPanel.className = '';
demoPanel.style.backgroundColor = '';
}
})
*/
bean.on(demoStart3, 'click', function(e){
e.preventDefault();
demoPanel.style.display = 'block';
bodyElement.style.overflow = 'hidden';
// demoTitle.innerHTML = 'Demo #3';
// play a blank sound for iOS to start audio
if(window.play){
window.play();
}
// create a canvas
var canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
demoPanel.appendChild(canvas);
// var off;
// if(offset === undefined){
// console.warn("using 0 offset");
// off = (+ new Date) % modulus;
// } else {
// off = ((+ new Date)+offset) % modulus;
// }
// var drift = ((+new Date()) + offset) % modulus;
var drift = ((+new Date()) - offset) % modulus;
var syncDemo = new SyncDemo({
canvas:canvas
}, drift, x, y)
syncDemo.start();
demoReset = function(){
demoPanel.removeChild(canvas)
syncDemo.cancel();
}
})
//
var stored = localStorage.getItem('calibration');
if(stored){
var parts = stored.split(',').map(parseFloat);
x = parts[0];
y = parts[1];
offset = parts[2];
render();
console.log("used stored calibration", parts);
bean.fire(calibrations, 'modified')
}
// debugging
if(window.parent !== window){
console.log("listening")
window.addEventListener('message', function(e){
var data = e.data || {};
if(data.setup){
x = data.setup.x;
y = data.setup.y;
offset = modulus/4;// ((+ new Date) + (modulus/4)) % modulus;
render();
} else if(data.demo){
totally = true;
demoClose.click();
// if(data.demo === 1){
// demoStart1.click()
// }
// if(data.demo === 2){
// demoStart2.click()
// }
if(data.demo === 3){
demoStart3.click()
}
} else {
console.log("unhandled message ", data)
}
}, false)
}
function paintHSL(){
g.remove()
for (var x = 0.95; x >= 0; x-=0.05) {
for (var y = 0.95; y >= 0; y-=0.05) {
// var xr = (x-.5)*2,
// yr = (y-.5)*2;
svg
.append('circle')
.attr({
r:2,
cx: x*w,
cy: y*h,
fill: fillHSL(x,y)
})
}
};
}
// paintHSL()
// return an hsl fill based on the position of the client
function fillHSL(_x,_y){
var x = (_x*2)-1,
y = (_y*2)-1,
a = Math.atan2(x, y),
d = Math.sqrt((x*x)+(y*y));
// convert / constrain
a = (a + Math.PI*2) % (Math.PI*2);
// 0..2PI -> 0..360
a = (a/Math.PI) * 180
if(d > 1) d = 1;
return 'hsl('+d3.round(a)+', '+d3.round(d*100)+'%, 50%)'
}