forked from potatowagon/silly-app-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (122 loc) · 3.46 KB
/
index.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
// define global var here
var app = {
usingGenericSensor: true,
max: {
x: 0,
y: 0,
z: 0
},
busy: false,
//media paths collection
audio: [
"audio/cat.mp3",
"audio/nonono.mp3"
]
};
/*INSERT ACCELEROMETER HERE*/
try {
let sensor = new LinearAccelerationSensor({ frequency: 60 });
sensor.start();
sensor.onreading = () => {
var event = new CustomEvent('devicemotion', {
detail: {
acceleration: {
x: sensor.x,
y: sensor.y,
z: sensor.z
}
}
});
window.dispatchEvent(event);
}
sensor.onerror = event => console.log(event.error.name, event.error.message);
}
catch (e) {
console.log(e);
app.usingGenericSensor = false;
}
if (window.DeviceMotionEvent || 'LinearAccelerationSensor' in window) {
/*ADD AN EVENT LISTENER TO WINDOW*/
window.addEventListener('devicemotion', deviceMotionHandler, false);
}
else {
console.log("Sensors not supported");
}
function deviceMotionHandler(eventData) {
var SCALE = 1000;
if (app.usingGenericSensor) {
var acc = eventData.detail.acceleration;
}
else {
if (eventData.acceleration.x) {
var acc = eventData.acceleration;
}
else {
//deep clone
var acc = {};
acc.x = eventData.accelerationIncludingGravity.x;
acc.y = eventData.accelerationIncludingGravity.y;
acc.z = eventData.accelerationIncludingGravity.z;
if (acc.x > 8) {
acc.x = acc.x - 8;
}
if (acc.y > 8) {
acc.y = acc.y - 8;
}
if (acc.z > 8) {
acc.z = acc.z - 8;
}
}
}
var mAcc = {};
//scale acc values
mAcc.x = acc.x * SCALE;
mAcc.y = acc.y * SCALE;
mAcc.z = acc.z * SCALE;
app.max.x = updateMaxValue(mAcc.x, app.max.x);
app.max.y = updateMaxValue(mAcc.y, app.max.y);
app.max.z = updateMaxValue(mAcc.z, app.max.z);
//display acc readings
var accReading = "<p>x: " + mAcc.x + "</br>y: " + mAcc.y + "</br>z: " + mAcc.z + "</br> max: ( " + app.max.x + ", " + app.max.y + ", " + app.max.z + " )";
document.getElementById('acc-readings').innerHTML = accReading;
//adjust shake threshold here
var shakyThreshold = 5000;
//do silly stuff
var shakyThreshold = 11000;
console.log(app.busy);
if (!app.busy) {
if (Math.abs(mAcc.x) > shakyThreshold || Math.abs(mAcc.y) > shakyThreshold || Math.abs(mAcc.z) > shakyThreshold) {
app.busy = true;
/*DO SILLY STUFF*/
var scream = new Audio(randomPicker(app.audio));
scream.play();
sleep(2000);
app.busy = false;
}
}
}
/*UTILS*/
function updateMaxValue(val, field) {
if (Math.abs(val) > Math.abs(field)) {
field = val;
}
return field;
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
function randomPicker(array) {
let i = Math.floor(Math.random() * array.length);
return array[i];
}
/* INSERT SCREAM FOR BUTTON CLICK FUNCTION HERE*/
function scream() {
var scream = new Audio(randomPicker(app.audio));
scream.play();
console.log("is pushed, is screaming");
}