-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
457 lines (424 loc) · 15.4 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
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
<html>
<head>
<title>SoundAir</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="audiosynth.js"></script>
<script src="http://js.leapmotion.com/leap-0.6.3.min.js"></script>
<style>
html, body{
margin: 0;
padding-top: 0;
background-color: #364854;
font-family: 'Open Sans', sans-serif;
color: #fff;
font-weight: 300;
text-align: center;
}
#container {
padding-top: 2em;
}
h1 {
font-weight: 300;
font-size: 2.5em;
}
#canvasContainer{
width: 800px;
height: 500px;
margin-left: auto;
margin-right: auto;
display: none;
position: relative;
user-select: none;
}
#mycanvas {
position: absolute;
left: 0;
top: 0;
z-index: 10;
pointer-events:none;
}
#caption{
font-size: 1.5em;
margin-top: 0.1em;
}
#toolbar {
padding-top:1em;
}
.button {
border-bottom: 1px solid #fff;
color: #fff;
font-size: 1.5em;
display: inline-block;
padding-left: 0.1em;
padding-right: 0.1em;
cursor: pointer;
transition: all 0.1s ease;
}
.button:hover {
padding-left: 0.2em;
padding-right: 0.2em;
border-bottom: 2px solid #fff;
}
.button:active {
padding-left: 0.1em;
padding-right: 0.1em;
border-bottom: 1px solid #fff;
}
.card {
width: 76px;
height: 127px;
background-color: rgba(255, 255, 255, 0.9);
border: 1px solid #aaa;
position: absolute;
z-index: 1;
top: 0;
left: 0;
color: #333;
}
.card span{
font-size: 1.2em;
border: 1px solid #333;
padding: 0.15em;
cursor: pointer;
margin:0.1em;
}
.card input{
display: none;
width: 116px;
position: relative;
left: -21px;
margin-top: 0.2em;
border-radius: 0.25em;
border: 1px solid #aaa;
text-align: center;
font-size: 1.25em;
}
.cardSound {
display: none;
font-size: 1.4em;
}
.cardTitle {
font-size: 1.5em;
padding-bottom: 0.1em;
padding-top: 0.2em;
}
.editPanel {
width: 20em;
position:relative;
left: -6.5em;
display: none;
}
.editButton {
border:1px solid #000;
font-size: 1.2em;
display:inline-block;
background-color: rgba(255, 255, 255, 0.9);
margin:0.2em;
cursor: pointer;
padding-left: 0.4em;
padding-right: 0.4em;
padding-top: 0.1em;
padding-bottom: 0.1em;
transition: all 0.1s ease;
}
.editButton:hover {
background-color: rgba(255, 255, 255, 1);
}
.editButton:active {
background-color: rgba(255, 255, 255, 0.9);
}
#instrumentLabel {
font-size: 1.5em;
}
</style>
</head>
<body>
<div id="container">
<h1 id="heading" style="display: none;">Welcome to SoundAir</h1>
<div id="canvasContainer">
<canvas id="mycanvas" width="800" height="500"></canvas>
</div>
<div id="toolbar" style="display: none;"><div class="button" id="addCard">Add Card</div></div>
<p id="caption" style="display: none;">Let's begin...</p>
<p id="instrumentLabel" style="display: none;">Piano</p>
</div>
<script>
$(document).ready(function() {
$("#heading").fadeIn("slow", function(){
$("#caption").fadeIn("slow", function(){
$("#canvasContainer").slideDown("slow");
$("#caption").fadeOut("slow", function(){
$("#toolbar").fadeIn("slow");
});
});
});
});
var cardWidth = 76;
var cardHeight = 127;
function createCardDiv(id){
var div = document.createElement("DIV");
var title = document.createElement("DIV");
var move = document.createElement("SPAN");
var resize = document.createElement("SPAN");
var music = document.createElement("SPAN");
var edit = document.createElement("SPAN");
var soundText = document.createElement("INPUT");
var soundTextDisplay = document.createElement("DIV");
soundTextDisplay.className = "cardSound";
soundTextDisplay.id = "soundTextDisplay" + id;
div.className = "card";
title.className = "cardTitle";
move.className = "glyphicon glyphicon-move";
resize.className = "glyphicon glyphicon-resize-small";
music.className = "glyphicon glyphicon-music";
edit.className = "glyphicon glyphicon-cog";
move.setAttribute("data-toggle", "tooltip");
resize.setAttribute("data-toggle", "tooltip");
music.setAttribute("data-toggle", "tooltip");
edit.setAttribute("data-toggle", "tooltip");
move.setAttribute("data-placement", "bottom");
resize.setAttribute("data-placement", "bottom");
music.setAttribute("data-placement", "bottom");
edit.setAttribute("data-placement", "bottom");
move.setAttribute("title", "Move card");
resize.setAttribute("title", "Half the size of the card");
music.setAttribute("title", "Attach a sound to the card");
edit.setAttribute("title", "Attach an action to the card");
move.setAttribute("onclick", "move("+id+")");
resize.setAttribute("onclick", "resize("+id+")");
resize.style.display = "none";
music.setAttribute("onclick", "music("+id+")");
edit.setAttribute("onclick", "edit("+id+")");
soundText.setAttribute("type", "text");
soundText.setAttribute("onkeypress", "keyup(event, " + id + ")");
soundText.id="soundText"+id;
title.appendChild(document.createTextNode("#" + id));
div.id = "card"+id;
div.style.left = Math.floor(cx - cardWidth / 2)+"px";
div.style.top = Math.floor(cz - cardHeight / 2)+"px";
// Create the edit panel
editPanel = document.createElement("DIV");
editPanel.id = "edit"+id;
editPanel.className = "editPanel";
editPanel.innerHTML = "<div class='editButton' onclick='showVolumePanel("+id+")'>Change<br/>volume</div><div class='editButton' onclick='showInstrumentPanel("+id+")'>Change<br/>instrument</div>";
// Create the volume set panel
volumePanel = document.createElement("DIV");
volumePanel.id = "volumePanel"+id;
volumePanel.className = "editPanel";
volumePanel.innerHTML = "<div class='editButton' onclick='setAction("+id+", \"vol+\")'>+</div><div class='editButton' onclick='setAction("+id+", \"vol-\")'>-</div>";
volumePanel.style.left = "-1.1em";
// Create the change instrument panel
instrumentPanel = document.createElement("DIV");
instrumentPanel.id = "instrumentPanel"+id;
instrumentPanel.className = "editPanel";
instrumentPanel.innerHTML = "<div class='editButton' onclick='setAction("+id+", \"Piano\")'>Piano</div><div class='editButton' onclick='setAction("+id+", \"Organ\")'>Organ</div><div class='editButton' onclick='setAction("+id+", \"Guitar\")'>Guitar</div>";
instrumentPanel.style.left = "-1.1em";
div.appendChild(title);
div.appendChild(move);
div.appendChild(resize);
div.appendChild(music);
div.appendChild(edit);
div.appendChild(soundText);
div.appendChild(soundTextDisplay);
div.appendChild(editPanel);
editPanel.appendChild(volumePanel);
editPanel.appendChild(instrumentPanel);
return div;
}
var cardCount = 0;
var cards = {};
function addCard(){
cardCount += 1;
document.getElementById("canvasContainer").appendChild(createCardDiv(cardCount));
cards[cardCount] = {'x': -1*(cx - canvas.width / 2), 'z': cz - canvas.height / 2, 'sound': null, 'pressed': false};
$('[data-toggle="tooltip"]').tooltip();
}
document.getElementById("addCard").onclick = function(){
addCard();
}
function move(id){
$("#card" + id).css("left", Math.floor(cx - cardWidth / 2) + "px");
$("#card" + id).css("top", Math.floor(cz - cardHeight / 2) + "px");
cards[id]['x'] = -1*(cx - canvas.width / 2);
cards[id]['z'] = cz - canvas.height / 2;
}
function resize(id){
}
function music(id){
document.getElementById("soundText"+id).style.display = "block";
document.getElementById("soundText"+id).focus();
}
function keyup(e, id){
if(e.keyCode == 13){
var text = document.getElementById("soundText"+id).value;
document.getElementById("soundText"+id).style.display = "none";
document.getElementById("soundTextDisplay"+id).innerHTML = text;
cards[id]["sound"] = text;
document.getElementById("soundTextDisplay"+id).style.display = "block";
}
}
function edit(id){
$("#edit"+id).toggle();
}
function showVolumePanel(id){
$("#volumePanel"+id).toggle();
}
function showInstrumentPanel(id){
$("#instrumentPanel"+id).toggle();
}
function setAction(id, action){
console.log(action);
document.getElementById("edit"+id).style.display = "none";
document.getElementById("soundTextDisplay"+id).innerHTML = action;
cards[id]["sound"] = action;
document.getElementById("soundTextDisplay"+id).style.display = "block";
}
// LeapMotion integration
var canvas = document.getElementById('mycanvas');
var context = canvas.getContext('2d');
// Events to enable mouse mode when the LeapMotion is not connected
var mode = "leap";
var mouseDown = false
var mouseX = 0;
var mouseY = 0;
document.getElementById("canvasContainer").addEventListener('mousemove', function(e) {
if(mode == "mouse"){
var rect = canvas.getBoundingClientRect();
mouseX = e.clientX - rect.left;
mouseY = e.clientY - rect.top;
}
}, false);
document.getElementById("canvasContainer").addEventListener('mousedown', function(e) {
mouseDown = true;
}, false);
document.getElementById("canvasContainer").addEventListener('mouseup', function(e) {
mouseDown = false;
}, false);
document.getElementById("canvasContainer").addEventListener('dblclick', function(e) {
if(mode == "mouse"){
addCard();
}
}, false);
var previousFrame = null;
var controllerOptions = {optimizeHMD: true};
var pressY = 300;
// Helper functions
function distance(x1, x2, y1, y2){
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
function isOnCard(leftX, rightX, topZ, bottomZ, fingerX, fingerZ){
return fingerX < leftX && fingerX > rightX && fingerZ > topZ && fingerZ < bottomZ;
}
var piano = Synth.createInstrument('piano');
var guitar = Synth.createInstrument('acoustic');
var organ = Synth.createInstrument('organ');
var instruments = [piano, guitar, organ];
var selected = 0;
var playing = false;
var volume = 1;
function mainLoop(fingerX, fingerZ, palmY){
if(palmY >= pressY){
for (var key in cards) {
if (!cards.hasOwnProperty(key)) continue;
var x = cards[key]["x"];
var z = cards[key]["z"];
console.log(x, z, fingerX, fingerZ);
if(isOnCard(x + cardWidth / 2, x - cardWidth / 2, z - cardHeight / 2, z + cardHeight / 2, fingerX, fingerZ)){
if(cards[key]["sound"] && cards[key]["pressed"] == false){
if(cards[key]["sound"] == "vol+"){
volume += 0.1;
if(volume > 1){
volume = 1;
}
document.getElementById("caption").innerHTML = "Volume: " + Math.floor(volume*100) + "%";
document.getElementById("caption").style.display = "block";
Synth.setVolume(volume);
}else if(cards[key]["sound"] == "vol-"){
volume -= 0.1;
if(volume < 0){
volume = 0;
}
document.getElementById("caption").innerHTML = "Volume: " + Math.floor(volume*100) + "%";
document.getElementById("caption").style.display = "block";
Synth.setVolume(volume);
}else if(cards[key]["sound"] == "Piano"){
selected = 0;
document.getElementById("instrumentLabel").innerHTML = "Instrument: Piano";
document.getElementById("instrumentLabel").style.display = "block";
}else if(cards[key]["sound"] == "Guitar"){
selected = 1;
document.getElementById("instrumentLabel").innerHTML = "Instrument: Guitar";
document.getElementById("instrumentLabel").style.display = "block";
}else if(cards[key]["sound"] == "Organ"){
selected = 2;
document.getElementById("instrumentLabel").innerHTML = "Instrument: Organ";
document.getElementById("instrumentLabel").style.display = "block";
}else{
var s = cards[key]["sound"];
instruments[selected].play(s.substring(0, s.length - 1).toUpperCase(), s.substring(s.length - 1), 2);
}
cards[key]["pressed"] = true;
playing = true;
}
}else{
cards[key]["pressed"] = false;
}
}
}else if(playing){
for (var key in cards) {
if (!cards.hasOwnProperty(key)) continue;
cards[key]["pressed"] = false;
}
playing = false;
}
// Draw circle on canvas at fingerX/Z
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
cx = Math.round(-fingerX + canvas.width / 2);
cz = Math.round(fingerZ + canvas.height / 2);
context.arc(cx, cz, 10, 0, 2 * Math.PI, false);
context.fillStyle = '#0000ff';
context.fill();
// How close are we to pressing?
var distance = pressY - palmY;
var lineWidth;
if(distance > 0){
if(distance < 10){
lineWidth = 1;
}else{
lineWidth = Math.floor(Math.log(distance));
}
context.lineWidth = lineWidth;
context.strokeStyle = '#fff';
context.stroke();
}
}
// Enable a loop for mouse support
function loopMouse() {
requestAnimationFrame(loopMouse);
if(mode != "mouse"){
return;
}
var fingerX = Math.round(-mouseX + canvas.width / 2);
var fingerZ = Math.round(mouseY - canvas.height / 2);
var palmY = pressY - 1 + mouseDown;
mainLoop(fingerX, fingerZ, palmY);
}
loopMouse();
Leap.loop(controllerOptions, function(frame) {
var data = document.getElementById("data");
if (frame.pointables.length > 0 || mode != "mouse") {
var indexFinger = frame.pointables[1];
var fingerX = Math.round(indexFinger.tipPosition[0]);
var fingerZ = Math.round(indexFinger.tipPosition[2]);
var palmY = frame.hands[0].palmPosition[1];
mainLoop(fingerX, fingerZ, palmY);
}
previousFrame = frame;
})
</script>
</body>
</html>