-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (55 loc) · 2.15 KB
/
Copy pathscript.js
File metadata and controls
68 lines (55 loc) · 2.15 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
const qrText = document.getElementById('qr-text');
const generateBtn = document.getElementById('generate-btn');
const qrCodeDiv = document.getElementById('qr-code');
const downloadBtn = document.getElementById('download-btn');
const qrSize = document.getElementById('qr-size');
let qrcode = null;
generateBtn.addEventListener('click', () => {
const text = qrText.value.trim();
const size = parseInt(qrSize.value);
if (text) {
qrCodeDiv.innerHTML = '';
downloadBtn.style.display = 'none';
qrcode = new QRCode(qrCodeDiv, {
text: text,
width: size,
height: size,
});
setTimeout(() => {
downloadBtn.style.display = 'block';
}, 100);
} else {
alert('Por favor, digite um texto ou URL para gerar o QR Code.');
qrCodeDiv.innerHTML = '<p>Seu QR code aparecerá aqui</p>';
downloadBtn.style.display = 'none';
}
});
downloadBtn.addEventListener('click', () => {
const qrCanvas = qrCodeDiv.querySelector('canvas');
if (qrCanvas) {
const originalSize = qrCanvas.width;
const borderSize = 20; // Tamanho da borda em pixels
const newSize = originalSize + 2 * borderSize;
// Cria um novo canvas maior com fundo branco
const borderedCanvas = document.createElement('canvas');
borderedCanvas.width = newSize;
borderedCanvas.height = newSize;
const ctx = borderedCanvas.getContext('2d');
// Preenche o fundo com branco
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, newSize, newSize);
// Desenha o QR code original no centro do novo canvas
const offsetX = borderSize;
const offsetY = borderSize;
ctx.drawImage(qrCanvas, offsetX, offsetY);
// Obtém a URL da imagem com a borda
const imageUrl = borderedCanvas.toDataURL('image/png');
// Cria e simula o clique no link para download
const link = document.createElement('a');
link.href = imageUrl;
link.download = 'qr-code.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
});