-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (41 loc) · 1.28 KB
/
script.js
File metadata and controls
48 lines (41 loc) · 1.28 KB
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
const qrText = document.getElementById('qrTxt');
const sizes = document.getElementById('sizes');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn')
const qrContainer = document.querySelector('.qr-body');
let size = sizes.value;
generateBtn.addEventListener('click', (e)=>{
e.preventDefault();
isEmptyInput();
});
sizes.addEventListener('change', (e)=>{
size = e.target.value;
generateQRCode();
isEmptyInput();
});
downloadBtn.addEventListener('click', ()=>{
let img = document.querySelector('.qr-body img');
if(img !== null){
let imgAtr = img.getAttribute('src')
downloadBtn.setAttribute("href", imgAtr);
}
else{
downloadBtn.setAttribute("href", `${document.querySelector('canvas').toDataURL()}`);
}
});
function isEmptyInput(){
qrText.value.length > 0 ? generateQRCode() : qrText.classList.add('error');
setTimeout(()=>{
qrText.classList.remove('error');
},1000)
}
function generateQRCode(){
qrContainer.innerHTML ="";
new QRCode(qrContainer,{
text:qrText.value,
height: size,
width: size,
colorLight:'#fff',
colorDark:'#000',
});
}