forked from tony-xlh/getUserMedia-multiple-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcomponent.html
More file actions
executable file
·76 lines (72 loc) · 2.71 KB
/
webcomponent.html
File metadata and controls
executable file
·76 lines (72 loc) · 2.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Multiple Camera Simple Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script type="module">
import { defineCustomElements } from 'https://cdn.jsdelivr.net/npm/camera-preview-component/dist/esm/loader.js';
defineCustomElements();
</script>
<style>
camera-preview {
max-width: 100%;
}
</style>
</head>
<body>
<label>
Camera 1:
<select id="select-camera-1"></select>
</label>
<label>
Camera 2:
<select id="select-camera-2"></select>
</label>
<camera-preview id="camera1"></camera-preview>
<camera-preview id="camera2"></camera-preview>
<button id="btn-open-camera">Open Cameras</button>
<script>
var devices;
var camSelect1 = document.getElementById("select-camera-1");
var camSelect2 = document.getElementById("select-camera-2");
window.onload = async function(){
await listDevices();
}
async function listDevices(){
devices = await document.getElementById("camera1").getAllCameras();
for (let index = 0; index < devices.length; index++) {
const device = devices[index];
camSelect1.appendChild(new Option(device.label ?? "Camera "+index,device.deviceId));
camSelect2.appendChild(new Option(device.label ?? "Camera "+index,device.deviceId));
}
camSelect2.selectedIndex = 1;
}
function getComponent(index) {
return document.getElementById("camera"+(index+1));
}
document.getElementById("btn-open-camera").addEventListener("click",function(){
for (let index = 0; index < 2; index++) {
var camComponent = document.getElementById("camera"+(index+1));
const onOpened = async () => {
var video = await getComponent(index).getVideoElement();
getComponent(index).style.width = video.videoWidth + "px";
getComponent(index).style.height = video.videoHeight + "px";
}
const onClosed = () => {
console.log("closed");
}
camComponent.addEventListener("opened",onOpened);
camComponent.addEventListener("closed",onClosed);
if (index === 0) {
camComponent.desiredCamera = camSelect1.selectedOptions[0].label;
}else{
camComponent.desiredCamera = camSelect2.selectedOptions[0].label;
}
camComponent.active = true;
}
});
</script>
</body>
</html>