-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.html
148 lines (140 loc) · 5.37 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
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
<html>
<body>
<video autoplay></video>
<img>
<div>
<input type="range" id="panRange" name="pan" hidden>
<label for="pan">Pan</label>
</div>
<div>
<input type="range" id="tiltRange" name="tilt" hidden>
<label for="tilt">Tilt</label>
</div>
<div>
<input type="range" id="zoomRange" name="zoom " hidden>
<label for="zoom">Zoom</label>
</div>
<div class="select">
<label for="videoSource">Video source: </label>
<select id="videoSource" disabled></select>
</div>
<p>See <a href="README.md">the README</a> for a list of supported cameras.</p>
<script>
var imageCapture;
const supports = navigator.mediaDevices.getSupportedConstraints();
console.log("supports = ", JSON.parse(JSON.stringify(supports)));
navigator.mediaDevices.getUserMedia({
video: { pan: true, tilt: true, zoom: true }
}).then((mediaStream) => {
gotStream(mediaStream);
return navigator.mediaDevices.enumerateDevices()
.then(gotDevices);
})
.catch(error => console.log('Argh!', error.name || error));
async function gotStream(mediaStream) {
const video = document.querySelector('video');
if (video.srcObject) {
video.srcObject.getTracks().forEach(track => track.stop());
}
document.querySelector('video').srcObject = mediaStream;
const track = mediaStream.getVideoTracks()[0];
const capabilities = track.getCapabilities();
console.log("capabilities == ", JSON.parse(JSON.stringify(capabilities)));
const settings = track.getSettings();
console.log("settings == ", JSON.parse(JSON.stringify(settings)));
const inputPanRange = document.getElementById('panRange');
const inputTiltRange = document.getElementById('tiltRange');
const inputZoomRange = document.getElementById('zoomRange');
// Check whether pan is supported or not.
if (!('pan' in settings)) {
return Promise.reject('pan is not supported by ' + track.label);
}
// Check whether tilt is supported or not.
if (!('tilt' in settings)) {
return Promise.reject('tilt is not supported by ' + track.label);
}
// Check whether zoom is supported or not.
if (!('zoom' in settings)) {
return Promise.reject('zoom is not supported by ' + track.label);
}
// Map pan to a slider element.
inputPanRange.min = capabilities.pan.min + 0.00001;
inputPanRange.max = capabilities.pan.max;
console.log("inputPanRange.min = ", inputPanRange.min);
console.log("inputPanRange.max = ", inputPanRange.max);
inputPanRange.step = capabilities.pan.step;
console.log("inputPanRange.max = ", inputPanRange.step);
inputPanRange.value = settings.pan;
// Map tilt to a slider element.
inputTiltRange.min = capabilities.tilt.min + 0.00001;
inputTiltRange.max = capabilities.tilt.max;
console.log("inputTiltRange.min = ", inputTiltRange.min);
console.log("inputTiltRange.max = ", inputTiltRange.max);
inputTiltRange.step = capabilities.tilt.step;
console.log("inputTiltRange.max = ", inputTiltRange.step);
inputTiltRange.value = settings.pan;
inputPanRange.oninput = function(event) {
console.log(" inputPanRange.value = ", inputPanRange.value);
track.applyConstraints({
advanced: [{
pan: event.target.value
}]
});
}
inputPanRange.hidden = false;
inputTiltRange.oninput = function(event) {
console.log(" inputTiltRange.value = ", inputTiltRange.value);
track.applyConstraints({
advanced: [{
tilt: event.target.value
}]
});
}
inputTiltRange.hidden = false;
// Map zoom to a slider element.
inputZoomRange.min = capabilities.zoom.min + 0.00001;
inputZoomRange.max = capabilities.zoom.max;
console.log("inputZoomRange.min = ", inputZoomRange.min);
console.log("inputZoomRange.max = ", inputZoomRange.max);
inputZoomRange.step = capabilities.zoom.step;
console.log("inputZoomRange.max = ", inputZoomRange.step);
inputZoomRange.value = settings.zoom;
inputZoomRange.oninput = function(event) {
console.log(" inputZoomRange.value = ", inputZoomRange.value);
track.applyConstraints({
advanced: [{
zoom: event.target.value
}]
});
}
inputZoomRange.hidden = false;
}
function gotDevices(devices) {
const select = document.getElementById('videoSource');
devices.forEach(device => {
if (device.kind !== 'videoinput') {
return;
}
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label;
select.appendChild(option);
});
select.disabled = false;
select.onchange = () => {
const deviceId = select.value;
navigator.mediaDevices.getUserMedia({video: {deviceId}})
.then(gotStream)
.catch(error => console.log('Argh!', error.name || error));
}
}
function takePhoto() {
imageCapture.takePhoto().then(blob => {
console.log('Photo taken: ' + blob.type + ', ' + blob.size + 'B');
const image = document.querySelector('img');
image.src = URL.createObjectURL(blob);
}).catch(err => console.error('takePhoto() failed: ', err));
}
</script>
</body>
</html>