-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.js
68 lines (62 loc) · 1.7 KB
/
main.js
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
import './style.css';
// DOM Elements
const video = document.querySelector('video');
const backCamera = document.querySelector('.back');
const frontCamera = document.querySelector('.front');
const fullscreen = document.querySelector('.fullscreen');
/**
*
* @param {MediaProvider} stream The video stream to render
*/
function render(stream) {
video.srcObject = stream;
}
/**
* Gets the corresponding device camera based on a direction.
*
* @param {string} camera The device camera to use
* @returns {ConstrainDOMString} The cross-platform camera `facingMode`
*/
function getFacingMode(camera) {
switch (camera) {
case 'front':
return 'user';
case 'back':
return 'environment';
default:
return 'environment';
}
}
/**
* Captures output of a specified device camera.
*
* @param {string} camera The device camera to use
*/
function capture(camera) {
navigator.mediaDevices
.getUserMedia({
video: { facingMode: getFacingMode(camera) },
})
.then(render)
.catch(console.error);
}
/**
* Cross-platform way to make a video enter fullscreen.
*/
function enterFullScreen() {
if (video.webkitEnterFullScreen) {
// Mobile Safari
video.webkitEnterFullScreen();
} else if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.webkitRequestFullscreen) {
// Regular Safari
video.webkitRequestFullscreen();
} else if (video.msRequestFullscreen) {
// IE11 — I sincerely hope you don't own a phone running IE11...
video.msRequestFullscreen();
}
}
frontCamera.addEventListener('click', () => capture('front'));
backCamera.addEventListener('click', () => capture('back'));
fullscreen.addEventListener('click', enterFullScreen);