Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 2, 2024
1 parent 35100a7 commit 3f9d1c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:root {
color-scheme: light dark;
--bg-color: #eee;
--error-color: red;
}
@media (prefers-color-scheme: dark) {
:root {
Expand All @@ -17,10 +18,14 @@
}
html, body {
font-family: monospace;
background-color: var(--bg-color);
}
#text {
width: 100%;
}
#error {
color: var(--error-color)
}
button {
display: block;
margin: 0.5em 0 0.5em;
Expand All @@ -35,5 +40,6 @@ <h1>QR Code Generator</h1>
<textarea id="text" placeholder="enter or paste url (etc...) here"></textarea>
<button id="download" type="button" disabled>download qr code</button>
<canvas></canvas>
<p id="error"></p>
</body>
</html>
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const saveBlob = (function() {
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', () => {
Expand All @@ -30,11 +31,17 @@ downloadElem.addEventListener('click', () => {

function updateQRCode(s) {
downloadElem.disabled = s ? '' : true;
canvasElem.height = 1;
errorElem.textContent = '';
if (!s) {
canvasElem.width = 1;
return;
}
const qr = QrCode.encodeText(s, Ecc.MEDIUM);
let qr;
try {
qr = QrCode.encodeText(s, Ecc.MEDIUM);
} catch (e) {
errorElem.textContent = e.toString();
}
const scale = 4;
const padding = 3;
const size = qr.size + padding * 2;
Expand All @@ -47,14 +54,6 @@ function updateQRCode(s) {
ctx.fillRect(x, y, 1, 1);
}
}
//const img = new Image();
//img.src = ctx.canvas.toDataURL();
//qrCodesElem.appendChild(el('div', {
// className: 'qrcode',
//}, [
// el('div', {textContent: s}),
// img,
//]));
}

console.log('here');

0 comments on commit 3f9d1c2

Please sign in to comment.