-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
81 lines (76 loc) · 2.85 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display Webcam Stream</title>
<style>
#container{
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement{
width: 502px;
height: 375px;
background-color: #666;
}
</style>
</head>
<body>
<p><span id="errorMsg"></span></p>
<div class="video.wrap">
<video autoplay="true" id="video"></video>
</div>
<div class="controller">
<button id="snap">Capture</button>
</div>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
const canvas = document.getElementById('canvas');
const snap = document.getElementById("snap");
const video = document.getElementById('video');
const errorMsgElement = document.querySelector('span#errorMsg');
//var video = document.querySelector("#videoElement");
const constraints = {
audio: false,
video: {
width: 640, height: 480
}
};
async function init(){
try{
const stream = await navigator.mediaDevices.getUserMedia(constraints);
handleSuccsess(stream);
} catch(e){
errorMsgElement.innerHTML = 'navigator.getUserMedia error: ${e.toString()}';
}
}
//Success
function handleSuccsess(stream){
window.stream = stream;
video.srcObject = stream;
}
//load init
init();
// Draw Image
var context = canvas.getContext('2d');
snap.addEventListener("click", param => {
context.drawImage(video, 0, 0, 640, 480);
var d = canvas.toDataURL("image/jpg");
console.log(d);
//var w = window.open('about:blank','image from canvas');
//w.document.write("<img src='"+d+"' alt = 'from canvas'\>");
});
/* if(navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({video : true})
.then(function (stream){
video.srcObject = stream;
})
.catch(function (err0r){
console.log("Something went wrong!");
});
}*/
</script>
</body>
</html>