-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
32 lines (23 loc) · 859 Bytes
/
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
const video = document.querySelector('#video')
const btn = document.querySelector('#btn')
const canvas = document.querySelector('#canvas')
if(navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({video: true })
.then(stream => {
video.srcObject = stream
})
.catch(error => {
console.log('An error occured while accessing webcam.')
})
}
btn.addEventListener('click', () => {
// get intrinsic width and height of the video element
const width = video.videoWidth, height = video.videoHeight
const context = canvas.getContext('2d')
canvas.width = width
canvas.height = height
context.drawImage(video, 0, 0, width, height)
const imgURL = canvas.toDataURL('image/png')
document.querySelector('#dl-btn').href = imgURL
document.querySelector('#dl-btn').click()
})