-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (32 loc) · 961 Bytes
/
index.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
import { showQRCode } from './qr.js';
const saveBlob = (function() {
const a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
return function saveData(blob, fileName) {
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
};
}());
const textElem = document.querySelector('#text');
const canvasElem = document.querySelector('canvas');
const downloadElem = document.querySelector('#download');
const errorElem = document.querySelector('#error');
const ctx = canvasElem.getContext('2d');
textElem.addEventListener('input', () => {
updateQRCode(textElem.value);
});
downloadElem.addEventListener('click', () => {
canvasElem.toBlob(blob => saveBlob(blob, 'qr-code'));
});
function updateQRCode(s) {
downloadElem.disabled = s ? '' : true;
canvasElem.height = 1;
errorElem.textContent = '';
if (!s) {
return;
}
showQRCode(ctx, s);
}