Skip to content

Commit ea26552

Browse files
committed
first commit
0 parents  commit ea26552

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

index.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Native Camera</title>
9+
<link rel="stylesheet" href="style.css">
10+
</head>
11+
12+
<body>
13+
<div style="margin-bottom: 1rem;">
14+
<button id="iniciar" onclick="initCamera()">Iniciar</button>
15+
<button id="parar" onclick="pararCamera()">Parar</button>
16+
<select id="cameras"></select>
17+
</div>
18+
19+
<div>
20+
<video autoplay="true" id="webcam" hidden></video>
21+
</div>
22+
23+
<script src="index.js"></script>
24+
</body>
25+
26+
</html>

index.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var stream;
2+
var videoContainer = document.querySelector('#webcam');
3+
var videoSelect = document.querySelector('#cameras');
4+
var botoes = {
5+
iniciar: document.querySelector('#iniciar'),
6+
parar: document.querySelector('#parar'),
7+
};
8+
9+
// Ao carregar o DOM, as funções serão inicializadas
10+
document.addEventListener('DOMContentLoaded', (ev) => carregarCameras());
11+
12+
videoSelect.onchange = (ev) => initCamera(ev.value);
13+
// videoSelect.addEventListener('change', (ev) => initCamera(ev.value)); // Dá pra fazer dessa forma também
14+
15+
async function carregarCameras() {
16+
alternarExibicaoVideo(false);
17+
18+
const devices = await navigator.mediaDevices.enumerateDevices();
19+
for (const d of devices) {
20+
if (d.kind === 'videoinput') {
21+
setNomeCamera(d);
22+
}
23+
}
24+
25+
// initCamera(); // comentado para não iniciar as câmeras logo de inicio
26+
}
27+
28+
async function setNomeCamera(camera) {
29+
const option = document.createElement('option');
30+
option.value = camera.deviceId;
31+
option.text = camera.label;
32+
if (!camera.label) {
33+
await solicitarPermissao();
34+
await carregarCameras();
35+
return;
36+
}
37+
videoSelect.appendChild(option);
38+
}
39+
40+
async function solicitarPermissao() {
41+
const s = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
42+
s.getTracks().forEach(t => t.stop());
43+
}
44+
45+
async function initCamera(idCamera = videoSelect.value) {
46+
pararCamera();
47+
48+
const constrains = {
49+
video: {
50+
deviceId: idCamera ? { exact: idCamera } : undefined
51+
}
52+
};
53+
stream = await navigator.mediaDevices.getUserMedia(constrains);
54+
55+
videoContainer.srcObject = stream;
56+
alternarExibicaoVideo(true);
57+
}
58+
59+
function pararCamera() {
60+
stream?.getTracks().forEach(t => {
61+
// console.log('Parando o video', t, t.getCapabilities());
62+
t.stop();
63+
});
64+
65+
alternarExibicaoVideo(false);
66+
}
67+
68+
function alternarExibicaoVideo(exibir) {
69+
if (exibir) {
70+
videoContainer.removeAttribute('hidden');
71+
botoes.iniciar.disabled = true;
72+
botoes.parar.disabled = false;
73+
} else {
74+
videoContainer.setAttribute('hidden', true);
75+
botoes.iniciar.disabled = false;
76+
botoes.parar.disabled = true;
77+
}
78+
}

style.css

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
button {
2+
border: none;
3+
padding: 1rem;
4+
border-radius: 8px;
5+
color: #fff;
6+
font-size: 14px;
7+
transition: all 300ms;
8+
cursor: pointer;
9+
}
10+
11+
button[disabled] {
12+
cursor: not-allowed;
13+
background-color: gray !important;
14+
}
15+
16+
button#iniciar {
17+
background-color: #30f03f;
18+
}
19+
20+
button#iniciar:hover {
21+
background-color: #009500;
22+
}
23+
24+
button#parar {
25+
background-color: #f0303f;
26+
}
27+
28+
button#parar:hover {
29+
background-color: #950000;
30+
}
31+
32+
select {
33+
border: none;
34+
padding: 1rem;
35+
background-color: lightgray;
36+
border-radius: 8px;
37+
cursor: pointer;
38+
}
39+
40+
video {
41+
width: 50vw;
42+
/* Para espelhar a câmera */
43+
/* transform: scaleX(-1); */
44+
}
45+
46+
footer {
47+
position: fixed;
48+
bottom: 0px;
49+
left: 0px;
50+
width: 100%;
51+
background-color: lightgray;
52+
text-align: center;
53+
padding: 0.5rem 0;
54+
color: white;
55+
}
56+
57+
@media (max-width: 760px) {
58+
video {
59+
width: 100%;
60+
max-height: 100%;
61+
}
62+
}

0 commit comments

Comments
 (0)