-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (61 loc) · 2.35 KB
/
Copy pathapp.js
File metadata and controls
73 lines (61 loc) · 2.35 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
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
const input = document.getElementById("qr-input");
const generateBtn = document.getElementById("generate-btn");
const qrOutput = document.getElementById("qr-output");
const qrContainer = document.getElementById("qr-container");
const emptyState = document.getElementById("empty-state");
const copyBtn = document.getElementById("copy-btn");
const downloadBtn = document.getElementById("download-btn");
const scanLine = document.querySelector(".scan-line");
let currentSVG = null;
function generate() {
const text = input.value.trim();
if (!text) return;
const qr = qrcode(0, "H");
qr.addData(text);
qr.make();
const svgStr = qr.createSvgTag({ scalable: true });
qrContainer.innerHTML = svgStr;
currentSVG = svgStr;
qrOutput.style.display = "flex";
requestAnimationFrame(() => {
qrOutput.classList.add("visible");
});
emptyState.classList.add("hidden");
scanLine.classList.add("flash");
setTimeout(() => scanLine.classList.remove("flash"), 500);
resetCopyBtn();
}
function downloadSVG() {
if (!currentSVG) return;
const blob = new Blob([currentSVG], { type: "image/svg+xml" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "qrcode.svg";
a.click();
URL.revokeObjectURL(url);
}
function copySVG() {
if (!currentSVG) return;
navigator.clipboard.writeText(currentSVG).then(() => {
copyBtn.classList.add("success");
copyBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
Copied!
`;
setTimeout(resetCopyBtn, 2000);
});
}
function resetCopyBtn() {
copyBtn.classList.remove("success");
copyBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
Copy SVG
`;
}
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") generate();
});
generateBtn.addEventListener("click", generate);
downloadBtn.addEventListener("click", downloadSVG);
copyBtn.addEventListener("click", copySVG);