-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfie.js
60 lines (46 loc) · 1.79 KB
/
selfie.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
const capture = document.getElementById("capture-btn");
const resetBtn = document.getElementById('reset');
const shareBtn = document.getElementById('share');
const shareBlock = document.querySelector('.share-and-reset-block')
const video = document.querySelector("video");
const canvas2 = document.createElement("canvas");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (err) {
alert("Error occurred: " + err);
});
}
capture.onclick = function () {
const screenshotTarget = document.getElementById("selfie-container");
canvas2.width = video.videoWidth;
canvas2.height = video.videoHeight;
canvas2.getContext("2d").drawImage(video, 0, 0);
html2canvas(screenshotTarget).then((canvas) => {
const capturedImage = canvas.toDataURL("image/png");
var anchor = document.createElement("a");
// anchor.setAttribute("href", capturedImage);
// anchor.setAttribute("download", "yourScreenshot.png");
anchor.href = capturedImage;
anchor.download = "yourScreenshot.png";
anchor.click();
anchor.remove();
});
// Switch buttons
capture.style.display = 'none'
shareBlock.style.display = 'flex'
};
// on reset
function resetBlock () {
shareBlock.style.display = 'none'
capture.style.display = 'flex'
}
// on share
function shareOnLinkedIn () {
window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(document.URL) + '&title=' + encodeURIComponent(document.title), '', 'width=600,height=800'); return false
}
shareBtn.addEventListener('click', shareOnLinkedIn)
resetBtn.addEventListener('click', resetBlock)