-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
62 lines (56 loc) · 2.18 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Webcams</title>
</head>
<body>
<select id="webcamSelect">
<option disabled selected>Select a webcam...</option>
</select>
<button id="startButton">Start Camera</button>
<video id="videoElement" controls autoplay></video>
<script>
const videoElement = document.getElementById('videoElement');
async function populateWebcams() {
const webcamSelect = document.getElementById('webcamSelect');
const devices = await navigator.mediaDevices.enumerateDevices();
devices.forEach(device => {
if (device.kind === 'videoinput') {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${webcamSelect.options.length + 1}`;
webcamSelect.appendChild(option);
}
});
}
async function startCamera() {
const selectedDeviceId = document.getElementById('webcamSelect').value;
if (!selectedDeviceId) {
alert('Please select a webcam first.');
return;
}
const constraints = {
video: { deviceId: selectedDeviceId },
};
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
} catch (error) {
console.error('Error accessing webcam:', error);
}
}
// Toggle fullscreen on video click
videoElement.addEventListener('click', () => {
if (videoElement.requestFullscreen) {
videoElement.requestFullscreen();
} else if (videoElement.webkitRequestFullscreen) { /* Safari */
videoElement.webkitRequestFullscreen();
} else if (videoElement.msRequestFullscreen) { /* IE11 */
videoElement.msRequestFullscreen();
}
});
populateWebcams();
document.getElementById('startButton').addEventListener('click', startCamera);
</script>
</body>
</html>