-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
166 lines (146 loc) · 6.98 KB
/
Copy pathindex.html
File metadata and controls
166 lines (146 loc) · 6.98 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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mobile Sensor</title>
<!-- Pico.css -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
</head>
<body>
<main class="container">
<header>
<hgroup>
<h1>Mobile Sensor</h1>
<p>シンプルでクリーンなセンサーデバッガー</p>
</hgroup>
</header>
<div id="sensor-area" style="opacity: 0.4; filter: grayscale(1); pointer-events: none;">
<div class="grid">
<article>
<header>📍 位置情報 (GPS)</header>
<div id="gps-display">緯度: -, 経度: -</div>
</article>
<article>
<header>📱 傾き (Orientation)</header>
<div id="ori-display">beta: -, gamma: -</div>
</article>
</div>
<div class="grid">
<article>
<header>🚀 加速度 (Motion)</header>
<div id="acc-display">x: -, y: -, z: -</div>
</article>
<article>
<header>🎤 ボリューム (Mic)</header>
<progress id="volume-progress" value="0" max="100"></progress>
</article>
</div>
</div>
<section>
<button id="start-btn" class="contrast">すべてのセンサーを起動</button>
<article id="log-console" style="display:none;">
<header>Console Log</header>
<div id="log-content" style="max-height: 200px; overflow-y: auto; font-family: monospace; font-size: 0.8rem;"></div>
</article>
</section>
</main>
<script>
const startBtn = document.getElementById('start-btn');
const sensorArea = document.getElementById('sensor-area');
const logConsole = document.getElementById('log-console');
const logContent = document.getElementById('log-content');
// シンプルなコンソールフック
(function() {
const oldLog = console.log;
const oldError = console.error;
console.log = function(...args) {
oldLog.apply(console, args);
addLog('LOG', args);
};
console.error = function(...args) {
oldError.apply(console, args);
addLog('ERR', args, true);
};
function addLog(type, args, isError = false) {
const line = document.createElement('div');
if (isError) line.style.color = 'red';
const time = new Date().toLocaleTimeString([], { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' });
line.textContent = `[${time}] [${type}] ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`;
logContent.appendChild(line);
logContent.scrollTop = logContent.scrollHeight;
}
})();
const initGPS = () => {
console.log("GPS初期化開始...");
if ("geolocation" in navigator) {
navigator.geolocation.watchPosition(pos => {
document.getElementById('gps-display').innerText =
`緯度: ${pos.coords.latitude.toFixed(4)}, 経度: ${pos.coords.longitude.toFixed(4)}`;
}, err => console.error("GPS Error:", err), { enableHighAccuracy: true });
} else {
console.error("GPS非対応のブラウザです");
document.getElementById('gps-display').innerText = "GPS非対応です";
}
};
const initMotionSensors = async () => {
console.log("モーションセンサー初期化開始...");
const handleOrientation = (e) => {
document.getElementById('ori-display').innerText = `beta: ${Math.round(e.beta)}, gamma: ${Math.round(e.gamma)}`;
};
const handleMotion = (e) => {
const { x, y, z } = e.acceleration || {};
document.getElementById('acc-display').innerText = `x: ${x?.toFixed(2) ?? '-'}, y: ${y?.toFixed(2) ?? '-'}, z: ${z?.toFixed(2) ?? '-'}`;
};
if (typeof DeviceOrientationEvent !== 'undefined' && typeof DeviceOrientationEvent.requestPermission === 'function') {
try {
const res = await DeviceOrientationEvent.requestPermission();
console.log("モーションセンサー許可状態:", res);
if (res === 'granted') {
window.addEventListener('deviceorientation', handleOrientation);
window.addEventListener('devicemotion', handleMotion);
}
} catch (err) {
console.error("モーションセンサー許可エラー:", err);
alert("許可に失敗しました");
}
} else {
console.log("DeviceOrientationEvent.requestPermission 非対応ブラウザ");
window.addEventListener('deviceorientation', handleOrientation);
window.addEventListener('devicemotion', handleMotion);
}
};
const initMicrophone = async () => {
console.log("マイク初期化開始...");
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
console.log("マイク許可取得成功");
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const source = audioContext.createMediaStreamSource(stream);
const analyser = audioContext.createAnalyser();
source.connect(analyser);
const dataArray = new Uint8Array(analyser.frequencyBinCount);
const updateVolume = () => {
analyser.getByteFrequencyData(dataArray);
const average = dataArray.reduce((a, b) => a + b) / dataArray.length;
document.getElementById('volume-progress').value = average * 2;
requestAnimationFrame(updateVolume);
};
updateVolume();
} catch (err) {
console.error("マイク許可エラー:", err);
}
};
startBtn.addEventListener('click', async () => {
console.log("起動ボタンクリック");
startBtn.style.display = 'none';
sensorArea.style = ""; // グレーアウトを解除
logConsole.style.display = "block"; // ログコンソールを表示
initGPS();
await initMotionSensors();
await initMicrophone();
});
</script>
</body>
</html>