الملفات
qr-generator/script.js
muath-ye 257b7e03fd fix: qr
2024-09-16 09:25:55 +03:00

64 أسطر
2.2 KiB
JavaScript

document.getElementById('generate-btn').addEventListener('click', () => {
const text = document.getElementById('text-input').value;
const canvas = document.getElementById('qr-code');
const downloadButtons = document.getElementById('download-buttons');
if (text) {
QRCode.toCanvas(canvas, text, function (error) {
if (error) {
console.error(error);
return;
}
console.log('QR code generated!');
downloadButtons.classList.remove('hidden'); // Show download buttons
});
}
});
// Download QR Code as SVG
document.getElementById('download-svg').addEventListener('click', () => {
const text = document.getElementById('text-input').value;
if (text) {
QRCode.toString(text, { type: 'svg' }, function (error, svg) {
if (error) {
console.error(error);
return;
}
const blob = new Blob([svg], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'qrcode.svg';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
});
}
});
// Download QR Code as PNG
document.getElementById('download-png').addEventListener('click', () => {
const text = document.getElementById('text-input').value;
const canvas = document.getElementById('qr-code');
if (text) {
QRCode.toCanvas(canvas, text, function (error) {
if (error) {
console.error(error);
return;
}
canvas.toBlob(function (blob) {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'qrcode.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}, 'image/png');
});
}
});