-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (50 loc) · 1.67 KB
/
script.js
File metadata and controls
58 lines (50 loc) · 1.67 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
const qrText = document.getElementById("qr-text");
const sizes = document.getElementById("sizes");
const generateBtn = document.getElementById("generateBtn");
const downloadBtn = document.getElementById("downloadBtn");
const qrContainer = document.querySelector(".qr-body");
let qr;
// Clear QR before generating new one
function clearQRCode() {
qrContainer.innerHTML = "";
}
// Generate QR Code
function generateQRCode() {
clearQRCode();
const size = sizes.value;
if (qrText.value.trim().length === 0) {
alert("Please enter text or URL");
return;
}
// Create new QR code
qr = new QRCode(qrContainer, {
text: qrText.value,
width: size,
height: size,
colorDark: "#000",
colorLight: "#fff",
correctLevel: QRCode.CorrectLevel.H,
render: "image" // Ensures output is <img> for download
});
}
// Download QR Code
downloadBtn.addEventListener("click", () => {
const img = document.querySelector(".qr-body img");
const canvas = document.querySelector(".qr-body canvas");
if (img) {
const imgAttr = img.getAttribute("src");
downloadBtn.setAttribute("href", imgAttr);
downloadBtn.setAttribute("download", "QR_Code.png");
} else if (canvas) {
const imgData = canvas.toDataURL("image/png");
downloadBtn.setAttribute("href", imgData);
downloadBtn.setAttribute("download", "QR_Code.png");
} else {
alert("Please generate a QR code first.");
}
});
// Generate QR when clicking generate button
generateBtn.addEventListener("click", (e) => {
e.preventDefault(); // prevent page reload if <a href="">
generateQRCode();
});