-
Notifications
You must be signed in to change notification settings - Fork 14
/
webcam.html
158 lines (151 loc) · 4.71 KB
/
webcam.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<html lang=”en”>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Impulcifer</title>
<style>
html, body{
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
body {
display: flex;
flex-direction: row;
}
#camera {
position: relative;
display: flex;
flex-direction: column;
overflow: hidden;
width: calc(100vw - 19vh);
margin: 8px 0 8px 8px;
height: calc(100vh - 16px);
box-sizing: border-box;
overflow: hidden;
border-radius: 16px;
}
#shutter-wrapper {
width: 100%;
position: relative;
}
#shutter, #ghost {
/*transform: scaleX(-1);*/
position: absolute;
width: 100%;
box-sizing: border-box;
border-radius: 16px;
}
#ghost {
opacity: 0.5;
}
#trigger{
width: 200px;
background-color: black;
color: white;
font-size: 16px;
border-radius: 30px;
border: none;
padding: 15px 20px;
text-align: center;
box-shadow: 0 5px 10px 0 rgba(0,0,0,0.2);
position: absolute;
bottom: 30px;
left: calc(50% - 100px);
}
#snaps {
width: 19vh;
position: relative;
display: flex;
flex-direction: column;
}
#snaps .snap {
/*transform: scaleX(-1);*/
box-sizing: border-box;
border-radius: 16px;
margin: 8px 8px 0 8px;
background-color: lightgray;
background-size: cover;
flex-grow: 1;
}
#snaps .snap img {
display: none;
}
#snaps .snap:last-child {
margin-bottom: 8px;
}
#snaps .snap.selected {
box-shadow: 0 0 3px 3px lightblue;
}
</style>
</head>
<body>
<!-- Camera -->
<div id="camera">
<div id="shutter-wrapper">
<video id="shutter" autoplay playsinline></video>
<canvas id="ghost"></canvas>
</div>
<button id="trigger">Take a picture</button>
</div>
<div id="snaps">
<div class="snap selected"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
<div class="snap"><img src="//:0" /></div>
</div>
<!-- Reference to your JavaScript file -->
<script>
// Set constraints for the video stream
let constraints = { video: { facingMode: "user", width: {min: 1270}, height: {min: 720} }, audio: false };
// Define constants
const shutter = document.querySelector("#shutter");
const ghost = document.querySelector("#ghost");
const trigger = document.querySelector("#trigger");
const snapImages = document.querySelectorAll("#snaps .snap");
function removeSelection (skipEl) {
snapImages.forEach((el) => {
if (el !== skipEl) {
el.classList.remove("selected");
}
});
}
snapImages.forEach((el) => {
el.onclick = () => {
removeSelection();
el.classList.add("selected");
ghost.width = shutter.videoWidth;
ghost.height = shutter.videoHeight;
ghost.getContext("2d").drawImage(el.getElementsByTagName("img")[0], 0, 0);
};
});
// Access the device camera and stream to camera
function cameraStart() {
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
track = stream.getTracks()[0];
shutter.srcObject = stream;
})
.catch(function(error) {
console.error("Oops. Something is broken.", error);
});
}
// Take a picture when trigger is tapped
trigger.onclick = function() {
ghost.width = shutter.videoWidth;
ghost.height = shutter.videoHeight;
ghost.getContext("2d").drawImage(shutter, 0, 0);
let dataUrl = ghost.toDataURL("image/webp");
let snap = document.querySelector("#snaps .snap.selected");
snap.style.backgroundImage = 'url("' + dataUrl + '")';
snap.getElementsByTagName("img")[0].src = dataUrl;
};
// Start the video stream when the window loads
window.addEventListener("load", cameraStart, false);
</script>
</body>
</html>