-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (112 loc) · 3.68 KB
/
app.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
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
// Define constants
const cameraView = document.querySelector("#camera--view"),
cameraOutput = document.querySelector("#camera--output"),
cameraSensor = document.querySelector("#camera--sensor"),
cameraTrigger = document.querySelector("#camera--trigger"),
cameraStartButton = document.querySelector("#camera--start"),
cameraStopButton = document.querySelector("#camera--stop");
//let there be light!
const btnFlash = document.querySelector('.switch');
// Set constraints for the video stream
var constraints = { video: { facingMode: "user" }, audio: false };
var track = null;
//have a console on mobile
const consoleOutput = document.getElementById("camDiv");
const log = function(msg){
consoleOutput.innerText = `${consoleOutput.innerText}\n${JSON.stringify(msg)}`;
console.log(msg);
}
btnFlash.onclick = function(){
log('Flash found on this device. starting');
startFlash();
};
//Test browser support
const SUPPORTS_MEDIA_DEVICES = 'mediaDevices' in navigator;
function startFlash(){
if (SUPPORTS_MEDIA_DEVICES) {
//Get the environment camera (usually the second one)
navigator.mediaDevices.enumerateDevices().then(devices => {
const cameras = devices.filter((device) => device.kind === 'videoinput');
if (cameras.length === 0) {
log('No camera found on this device.');
}
else
{
log('Camera found success');
}
const camera = cameras[cameras.length - 1];
// Create stream and get video track
navigator.mediaDevices.getUserMedia({
video: {
deviceId: camera.deviceId,
facingMode: ['environment', 'user'],
height: {ideal: 1080},
width: {ideal: 1920}
}
}).then(stream => {
const track = stream.getVideoTracks()[0];
cameraView.srcObject = stream;
//Create image capture object and get camera capabilities
const imageCapture = new ImageCapture(track)
imageCapture.getPhotoCapabilities().then(capabilities => {
log('Photo capabilities found success');
if (capabilities.torch){
btnFlash.addEventListener('click', function(){
try{
track.applyConstraints({
advanced: [{torch: true}]
});
} catch(err){
log(err);
}
});
}else{
log("No torch found");
btnFlash.addEventListener('click', function(){
try{
track.applyConstraints({
advanced: [{torch: false}]
});
} catch(err){
log(err);
}
});
}
});
}).catch(log);
}).catch(log);
//The light will be on as long the track exists
}
}
// Access the device camera and stream to cameraView
function cameraStart() {
navigator.mediaDevices
.getUserMedia(constraints)
.then(function(stream) {
track = stream.getTracks()[0];
cameraView.srcObject = stream;
})
.catch(function(error) {
console.error("Oops. Something is broken.", error);
});
}
// Take a picture when cameraTrigger is tapped
cameraStartButton.onclick = function()
{
log(' camera started on this device.');
cameraStart();
}
// Take a picture when cameraTrigger is tapped
cameraStopButton.onclick = function() {
track.stop();
log(' camera stopped on this device.');
}
// Take a picture when cameraTrigger is tapped
cameraTrigger.onclick = function() {
cameraSensor.width = cameraView.videoWidth;
cameraSensor.height = cameraView.videoHeight;
cameraSensor.getContext("2d").drawImage(cameraView, 0, 0);
cameraOutput.src = cameraSensor.toDataURL("image/webp");
cameraOutput.classList.add("taken");
// track.stop();
};