-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 1.99 KB
/
index.js
File metadata and controls
69 lines (59 loc) · 1.99 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
// スマホから加速度を取得するイベントハンドラの追加
window.addEventListener('devicemotion', devicemotionHandler);
window.addEventListener('deviceorientation', deviceOrientationHandler);
var url = window.document.location.host.replace(/:.*/, '');
var wsport = 3333; // WebSocketのポート
const debug = false;
var host = debug?'localhost':'';
// WebSocketの設定
var wsosc = {};
wsosc.ws = new WebSocket('ws://' + host + ':' + wsport);
wsosc.status = false;
wsosc.ws.onopen = function(){wsosc.status = true;};
wsosc.ws.onclose = function(){wsosc.status = false;};
wsosc.send = function(path,type,data){
var jsonobj = {"osc":"WsoscSend","path":path,"type":type,"data":data};
var json = JSON.stringify(jsonobj);
// 送信可になったら
if(wsosc.status){ wsosc.ws.send(json); }
};
// JQuery
var toggle = 0;
$(function() {
// ボタンが押されたらsend
$('#btn1').on('click', function() {
wsosc.send('/osc/button1','i',[toggle]);
if(!toggle){ toggle = true; $("#btn1").val("stop"); }
else{ toggle = false; $("#btn1").val("start"); }
$('#icebox')[0].load();
});
});
// 加速度を取得するイベントハンドラ
function devicemotionHandler(e){
if(!toggle) return;
var acx = e.acceleration.x;
var acy = e.acceleration.y;
var acz = e.acceleration.z;
var data = [acx,acy,acz];
wsosc.send('/osc/accel', 'S', data);
$("#accel_x").text("accel_x : " + acx);
$("#accel_y").text("accel_y : " + acy);
$("#accel_z").text("accel_z : " + acz);
if(toggle){
if(acy > 10){
$('#icebox')[0].currentTime = 0;
$('#icebox')[0].play();
}
}
}
function deviceOrientationHandler(e){
if(!toggle) return;
var gx = e.beta;
var gy = e.gamma;
var gz = e.alpha;
var data = [gx,gy,gz];
wsosc.send('/osc/gyro', 'S', data);
$("#gyro_x").text("gyro_x : " + gx);
$("#gyro_y").text("gyro_y : " + gy);
$("#gyro_z").text("gyro_z : " + gz);
}