-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (119 loc) · 4.51 KB
/
script.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Semua KOnstanta urusan Ambil Gambar
feather.replace();
const controls = document.querySelector('.controls');
const video = document.querySelector('video');
const canvas = document.querySelector('canvas');
const buttons = [...controls.querySelectorAll('button')];
const ketcam = document.getElementById('keteranganCam');
let streamStarted = false;
const [play, pause] = buttons;
// KONS inisialisasi penggunaan kamera
var constraints = {
video: {
width: {
min: 1280,
ideal: 1920,
max: 2560 },
height: {
min: 720,
ideal: 1080,
max: 1440 },
facingMode: 'environment' // Menentukan depan/belakang = "user" (depan) / "environment" (belakang) ==> https://www-webdevdrops-com.translate.goog/en/how-to-access-device-cameras-with-javascript/?_x_tr_sl=en&_x_tr_tl=id&_x_tr_hl=id&_x_tr_pto=tc&_x_tr_hist=true
} };
// Semua KOnstanta urusan Upload Gambar
const form = document.querySelector('form');
const statusMessage = document.getElementById('statusMessage');
//const fileInput = document.querySelector('input');
const progressBar = document.querySelector('progress');
const tbKirim = document.querySelector('#kirim');
const gambar64 = document.querySelector('#gambar64');
// ############################# Semua Rutin Urusan AMBIL Gambar KAMERA
// EVEN ketika tombol PLAY di klik
const VidPlay = () => {
if (streamStarted) {
video.play();
play.classList.add('d-none');
pause.classList.remove('d-none');
return;
}
if ('mediaDevices' in navigator && navigator.mediaDevices.getUserMedia) {
constraints.video.facingMode = 'environment';
const updatedConstraints = constraints;
startStream(updatedConstraints);
}
setTimeout(ketCamera, 3000);
tbKirim.classList.add('d-none');
};
// EVEN untuk rutin PAUSE
const pauseStream = () => {
video.pause();
play.classList.remove('d-none');
pause.classList.add('d-none');
};
// EVEN untuk rutin AMBIL Foto
const doScreenshot = () => {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
gambar64.value = canvas.toDataURL('image/jpeg', 1.0); //==>('image/webp');
pauseStream();
tbKirim.classList.remove('d-none');
};
play.onclick = VidPlay;
pause.onclick = doScreenshot;
//RUTIN steam GAMBAR
const startStream = async constraints => {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
handleStream(stream);
};
//RUTIN Even saat mulai video steam
const handleStream = stream => {
video.srcObject = stream;
play.classList.add('d-none');
pause.classList.remove('d-none');
};
const ketCamera = () => {
ketcam.innerHTML = 'L Asli = ' +video.videoWidth+' T Asli = '+video.videoHeight+'<br/>';
ketcam.innerHTML = ketcam.innerHTML + 'L Offset = ' +video.offsetWidth+' T Offset = '+video.offsetHeight;
}
VidPlay();
// ############################# Semua Rutin Urusan Upload FORM
function updateStatusMessage(text) {
statusMessage.innerHTML = text;
}
function showPendingState() {
tbKirim.disabled = true;
updateStatusMessage('-->> Menunggu ...')
}
function updateProgressBar(value) {
const percent = value * 100;
progressBar.value = Math.round(percent);
}
tbKirim.onclick = (event) => {
event.preventDefault();
showPendingState();
uploadFiles();
}
function uploadFiles() {
const url = 'upload.php';
const method = 'post';
const xhr = new XMLHttpRequest();
const data = new FormData(form);
xhr.addEventListener('loadend', () => {
if (xhr.status === 200) {
updateStatusMessage('Success');
updateProgressBar(0);
ketcam.innerHTML = xhr.responseText;
tbKirim.disabled = false;
} else {
updateStatusMessage('Error');
tbKirim.disabled = false;
}
});
xhr.upload.addEventListener('progress', event => {
updateStatusMessage(`<br/>==> ${event.loaded} of ${event.total} byte`);
updateProgressBar(event.loaded / event.total);
});
xhr.open(method, url);
xhr.send(data);
}