-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
195 lines (149 loc) · 5.29 KB
/
index.html
File metadata and controls
195 lines (149 loc) · 5.29 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Seattle CoderDojo's Touchless Soundboard</title>
<link rel="stylesheet" href="css/index.css"/>
</head>
<body>
<center><canvas id="displayArea" style="background:#dddddd;"></canvas></center>
</body>
<script type="text/javascript" src="scripts/imagelist.js"></script>
<script type="text/javascript" src="scripts/leap-0.6.4.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>
// TO DO NOTES
// 1: HOW BIG IS THE ACTUAL DOT ON THE SCREEN?
// DOESN'T MATTER - WE CALCULATE A CENTERPOINT AND DETERMINE IF THAT CENTERPOINT IS WITHIN
// THE PLANE OF THE PIANO KEY OR SOUND OBJECT
//Set up our Canvas - optimized for full-screen
var canvasElement = document.getElementById("displayArea");
var displayArea = canvasElement.getContext("2d");
// set up the scaling
var canvX = 960;
var canvY = 540;
var objscale = Math.floor((960/canvX)*100)/100; //rounds to two decimal places
//initialize images & sounds
var sounds = [];
var images = [];
loadData();
//Create a leap controller object
var controller = new Leap.Controller({});
// the "good" fingers are the left index (1), and right index (6)
// other fingers can be enabled by adding *strings* of the numbers
//from 0-9 to the goodfingers array
var goodfingers = ['1','6'];
// Let's set up fingertip trackers. When the fingertip's Z coordinate
// is past the "plane of contact", allfingers[i].tip is set to true. If it's not,
// allfingers[i].tip is set to false and allfingers[i].play is set to true.
//
// if tip and play are both true for a finger, the collision tracker
// is invoked. If there's a collision, the appropriate sound is played
// and play.side is set to false.
// This keeps the fingertip from invoking another sound until it's been pulled
// back across the plane.
var allfingers = [];
// set up the fuzzy yellow dots we'll use to show the
// fingertip positions on screen
var fingertip = new Image();
fingertip.src = "images/fingertip.png";
// controller logic
controller.on("frame", function(frame){
if(frame.pointables.length > 0)
{
canvasElement.width = canvX;
canvasElement.height= canvY; //clear
drawNoisies();
//Get a pointable and normalize the tip position
for(var i in goodfingers){
if(!frame.pointables[goodfingers[i]]){
continue;
}
/* console.log("working with " + i) */
var pointable = frame.pointables[goodfingers[i]];
var interactionBox = frame.interactionBox;
var normalizedPosition = interactionBox.normalizePoint(pointable.tipPosition, true);
// Convert the normalized coordinates to span the canvas
var canvasX = canvasElement.width * normalizedPosition[0];
var canvasY = canvasElement.height * (1 - normalizedPosition[1]);
/* var canvasX = (canvasElement.width * normalizedPosition[0]) - ((fingertip.width / 2) * normalizedPosition[0]);
var canvasY = (canvasElement.height * (1 - normalizedPosition[1])) + ((fingertip.height / 2) * (1 - normalizedPosition[1])); */
//we can ignore z for a 2D context
displayArea.drawImage(fingertip,canvasX,canvasY);
if(pointable.tipPosition[2] < 0){checkHit(canvasX, canvasY)}
}
}
});
controller.connect();
function loadData(){
// populate images array with {img object, square object (topy, bottomy, leftx, rightx)}
// since this is a start-up func, we'll iterate through the images and sounds separately
// simply to help ensure the images have more time to preload.
for(var i in imagelist){
var data = imagelist[i];
var img = new Image();
img.src = data.imagePath;
img.onload = function(){
canvasElement.width = canvX;
canvasElement.height= canvY; //clear
drawNoisies();
}
images.push({"image": img, "square":{ "leftx" : data.topx, "topy": data.topy, "rightx": (data.topx + data.width), "bottomy": (data.topy + data.height) }})
}
for(var i in imagelist){
var data = imagelist[i];
var sound = {};
sound.played = false;
if(data.isfunc){
sound.type = "function";
sound.function = window[data.soundfunction];
sound.params = data.params;
} else {
var audio = document.createElement("audio");
audio.src = data.soundpath;
sound.type = "audio";
sound.holder = audio;
document.body.appendChild(audio);
}
sounds.push(sound);
}
return true;
}
function drawNoisies(){
for(var i in images){
var data = images[i];
displayArea.drawImage(data.image, data.square.leftx, data.square.topy);
}
}
function checkHit(x, y){
for(var i in images){
var data = images[i];
if (isHit({"x": x, "y": y}, data.square)) playSound(i);
}
}
function isHit(point, square){
if( between(square.leftx, square.rightx, point.x) && between(square.topy, square.bottomy, point.y) ){
return true;
} else {
return false;
}
}
function between(a,b,c){
// if c is between or equal to a and b
if((c >= a) && (c <= b)) {
return true;
} else {
return false;
}
}
function playSound(num){
//if we're on a delay until the sound can be played again
if(sounds[num].played) return;
if(sounds[num].type == 'audio'){
sounds[num].played = true;
sounds[num].holder.play();
}
window.setTimeout(function(){
sounds[num].played = false;
}, 150);
}
</script>
</html>