-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
328 lines (265 loc) · 8.6 KB
/
sketch.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
// This is the URL of our message-passing server
var SOCKET_URL = 'wss://fierce-plains-17880.herokuapp.com/';
// This is a special code word unique to your project.
// You must get this from an instructor!
var CODE_WORD = 'kale';
// The socket is how you send/receive messages.
var socket;
// --------------------------------------------------------------------
var resources = {};
var res_names = ['v_bg','h_bg','lf1','lf2','lf3','t1','t2','t3','t4','ft1','ft2','ft3','ft4','snow','m1','m2','m3'];
var deviceMode; // horizontal or vertinal
var num_leafs; // number of leafs that fall when shaked
var leafs_fall_toggle = false;
var leafs = [];
var snows = [];
var turtle_move_toggle = false;
var turtle;
var turtle_dir = "l";
var moon_toggle = false;
var moon;
var siri;
class Leaf {
constructor(){
this.typeID = Math.floor((Math.random() * 3) + 1); // randomly choose from three types of leaf
this.typeName = "lf"+this.typeID;
this.x = Math.floor((Math.random() * windowWidth) + 1);
this.y = Math.floor((Math.random() * windowHeight) + 1);
this.degree = (Math.random() * 360) + 1; // init rotation degree
this.width = (Math.random() * 35) + 25;
this.height = (Math.random() * 35) + 25;
this.timeCounter = 0; // this is the frame counter for the animation
this.speed = (Math.random() * 3) + 1; // the speed of falling
}
reset(){
// used to reset the position and rotation of the leaf
this.x = Math.floor((Math.random() * windowWidth) + 1);
this.y = Math.floor((Math.random() * windowHeight) + 1);
this.degree = (Math.random() * 360) + 1; // init rotation degree
this.timeCounter = 0; // this is the frame counter for the animation
}
display(canvas_h){
rotate(2*PI/this.degree);
var shifty = this.speed*this.timeCounter; // distance the leaf falls in y axis
var newy = (this.y+shifty)%canvas_h;
image(resources[this.typeName],this.x,newy,this.width,this.height);
this.timeCounter++;
}
}
class Snow {
constructor(){
this.typeName = "snow";
this.x = Math.floor((Math.random() * windowWidth) + 1);
this.y = Math.floor((Math.random() * windowHeight) + 1);
this.degree = (Math.random() * 360) + 1; // init rotation degree
this.width = (Math.random() * 15) + 10;
this.height = (Math.random() * 15) + 10;
this.timeCounter = 0; // this is the frame counter for the animation
this.speed = (Math.random() * 3) + 1; // the speed of falling
}
reset(){
// used to reset the position and rotation of the leaf
this.x = Math.floor((Math.random() * windowWidth) + 1);
this.y = Math.floor((Math.random() * windowHeight) + 1);
this.degree = (Math.random() * 360) + 1; // init rotation degree
this.timeCounter = 0; // this is the frame counter for the animation
}
display(canvas_h){
rotate(2*PI/this.degree);
var shifty = this.speed*this.timeCounter; // distance the leaf falls in y axis
var newy = (this.y+shifty)%canvas_h;
image(resources[this.typeName],this.x,newy,this.width,this.height);
this.timeCounter++;
}
}
class Moon {
constructor(x,y){
this.x = x;
this.y = y;
this.timeCounter = 0;
}
display(){
var id = 1+Math.floor(this.timeCounter/30)%3;
image(resources['m'+id],this.x,this.y,110,110);
if (moon_toggle){
this.timeCounter++;
}
}
}
class Turtle {
constructor(x,y){
this.timeCounter = 0; // animation frame
this.x = x;
this.y = y;
}
display(max_x_distance){
var animiIndex = 1+Math.floor(this.timeCounter/10)%4;
var shiftx_raw = (this.timeCounter%max_x_distance-max_x_distance/2);
var shiftx = abs(shiftx_raw);// movement along x axis
resetMatrix();
var pref;
var current_dir;
if (shiftx_raw>0){
pref = "t"; // right
current_dir = "right";
}else{
pref = "ft"; // left
current_dir = "left";
}
image(resources[pref+animiIndex],this.x+shiftx ,this.y,200,200);
if (turtle_move_toggle && turtle_dir===current_dir){
this.timeCounter++;
}
}
}
// --------------------------------------------------------------------
function setup() {
setShakeThreshold(15);
createCanvas(windowWidth, windowHeight);
socket = io(SOCKET_URL + CODE_WORD);
// To perform accelerometer sensing, you need to emit a message named "sense"
// with an object payload. The keys of this object should correspond to which
// acceleration events you wish to listen for.
// The supported event types are deviceMoved, deviceShaken, deviceTurned.
// For example:
socket.emit('sense', {deviceShaken: true, deviceMoved: true});
// Next, register corresponding event handlers.
socket.on('deviceShaken', deviceShaken);
// Finally, if you wish to set the move or shake threshold, simply emit it
// as a message. The defaults correspond to p5js' defaults:
// a shakeThreshold of 30, and moveThreshold of 0.5
socket.emit('shakeThreshold', 100);
socket.emit('moveThreshold', 0.75);
// ---------------------------------------------------------------------
// load resources
for (i=0;i<res_names.length;i++){
res_name = res_names[i];
res_path = "assets/"+res_name+".png";
resources[res_name] = loadImage(res_path);
console.log("load resource ",res_path);
}
resources['sound'] = loadSound('assets/sound.mp4');
// init global variables
deviceMode = "h";
num_leafs = 150;
siri = new p5.SpeechRec();
siri.continuous = true;
// siri.interimResults = true;
siri.onResult = parseResult;
siri.start();
for (i=0;i<num_leafs;i++){
var lf = new Leaf();
leafs.push(lf);
var sn = new Snow();
snows.push(sn);
}
turtle = new Turtle(windowWidth/2-100,windowHeight-200);
moon = new Moon(windowWidth-100,50);
}
function draw() {
if (deviceMode=="h"){
image(resources["h_bg"],0,0,windowWidth,windowHeight);
}else if (deviceMode=="v"){
image(resources["v_bg"],0,0,windowWidth,windowHeight);
}
if (leafs_fall_toggle){
if (deviceMode==="v"){
for (i=0;i<leafs.length;i++){
leafs[i].display(windowHeight);
}
}else{
for (i=0;i<leafs.length;i++){
snows[i].display(windowHeight);
}
}
}
turtle.display(windowWidth);
moon.display();
}
// The acceleration data is then passed to your socket handlers. This data
// corresponds to p5js' acceleration data, so you can continue to use that
// as a reference. For example, data.deviceOrientation or data.acceleration.x
// are the same as the deviceOrientation and accelerationX variables in p5.
// function deviceShaken(data) {
// console.log(data);
// }
// in dev mode, mouseClicked is an alias of deviceShaken()
function mouseClicked(){
console.log("leafs will fall");
if (!leafs_fall_toggle){
for (i=0;i<leafs.length;i++){
leafs[i].reset();
}
}
leafs_fall_toggle = !leafs_fall_toggle;
resources['sound'].setVolume(1);
resources['sound'].play();
}
// in dev mode, keyPressed is to simulate deviceTurned()
// deviceMoved()
function keyPressed() {
if (keyCode === LEFT_ARROW) {
deviceMode = "h";
} else if (keyCode === RIGHT_ARROW) {
deviceMode = "v";
}
if (keyCode === DOWN_ARROW){
turtle_move_toggle = true;
}
}
function keyReleased() {
turtle_move_toggle = false;
}
function deviceTurned(data){
if (deviceMode==="v"){
deviceMode = "h";
}else{
deviceMode = "v";
}
leafs_fall_toggle = false;
}
function deviceShaken(){
if (!leafs_fall_toggle){
for (i=0;i<leafs.length;i++){
leafs[i].reset();
}
}
leafs_fall_toggle = true;
resources['sound'].setVolume(1);
resources['sound'].play();
}
function parseResult()
//https://github.com/IDMNYU/p5.js-speech/blob/master/examples/05continuousrecognition.html
{
// recognition system will often append words into phrases.
// so hack here is to only use the last word:
var mostrecentword = siri.resultString.split(' ').pop();
if(mostrecentword.indexOf("left")!==-1) {
turtle_move_toggle = true;
turtle_dir = "left";
}
else if(mostrecentword.indexOf("right")!==-1) {
turtle_move_toggle = true;
turtle_dir = "right";
}
//
else if(mostrecentword.indexOf("moon")!==-1) {
moon_toggle = true;
}
else if(mostrecentword.indexOf("Moen")!==-1) {
moon_toggle = true;
}
else if(mostrecentword.indexOf("ruin")!==-1) {
moon_toggle = true;
}
//
else if(mostrecentword.indexOf("Stop")!==-1) {
turtle_move_toggle = false;
moon_toggle = false;
turtle_dir = "right";
}
// else if(mostrecentword.indexOf("up")!==-1) { dx=0;dy=-1; }
// else if(mostrecentword.indexOf("down")!==-1) { dx=0;dy=1; }
// else if(mostrecentword.indexOf("clear")!==-1) { background(255); }
console.log(mostrecentword);
}