-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQRCode.svelte
More file actions
182 lines (164 loc) · 4.23 KB
/
QRCode.svelte
File metadata and controls
182 lines (164 loc) · 4.23 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<script>
import QRCodeStyling from 'qr-code-styling';
import ECSESSLogo from '../assets/ECSESS.png';
let { data = '', downloadSize = 1000, size = 300 } = $props();
let qrCodeContainer = $state(/** @type {HTMLDivElement | null} */ (null));
let qrCodeInstance = $state(/** @type {QRCodeStyling | null} */ (null));
let debounceTimer = $state(/** @type {any} */ (null));
let lastData = $state('');
function updateQRCode() {
if (!qrCodeContainer) return;
const trimmedData = data.trim();
// Skip if data hasn't actually changed
if (trimmedData === lastData) return;
lastData = trimmedData;
// Clean up previous instance
if (qrCodeInstance) {
qrCodeContainer.innerHTML = '';
qrCodeInstance = null;
}
// Only create QR code if data is provided
if (trimmedData) {
// Create new QR code instance
const instance = new QRCodeStyling({
width: size,
height: size,
type: 'svg',
data: trimmedData,
margin: 0,
qrOptions: {
typeNumber: 0,
mode: 'Byte',
errorCorrectionLevel: 'M'
},
imageOptions: {
hideBackgroundDots: true,
imageSize: 0.4,
margin: 8,
crossOrigin: 'anonymous'
},
dotsOptions: {
color: '#3f6a3f', // ecsess-600
type: 'rounded',
gradient: {
type: 'radial',
colorStops: [
{
offset: 0,
color: '#8fb98a' // ecsess-300
},
{
offset: 1,
color: '#2d5a2d' // ecsess-700
}
]
}
},
backgroundOptions: {
color: '#ffffff' // ecsess-white
},
cornersSquareOptions: {
color: '#3f6a3f', // ecsess-600
type: 'extra-rounded'
},
cornersDotOptions: {
color: '#3f6a3f', // ecsess-600
type: 'dot'
},
image: ECSESSLogo
});
qrCodeInstance = instance;
instance.append(qrCodeContainer);
}
}
$effect(() => {
// Clear any pending debounce
if (debounceTimer !== null) {
clearTimeout(debounceTimer);
}
// Only update if container is available
if (qrCodeContainer) {
// Debounce updates for smooth typing experience
debounceTimer = setTimeout(() => {
updateQRCode();
debounceTimer = null;
}, 300);
}
// Cleanup function
return () => {
if (debounceTimer !== null) {
clearTimeout(debounceTimer);
}
};
});
export function download(format = 'png') {
if (!lastData || !lastData.trim() || !qrCodeInstance) return;
// Create a high-resolution version with extra margin for download
const marginSize = Math.floor(downloadSize * 0.04); // 4% margin
const downloadInstance = new QRCodeStyling({
width: downloadSize,
height: downloadSize,
type: 'svg',
data: lastData,
margin: marginSize, // Add extra padding/margin
qrOptions: {
typeNumber: 0,
mode: 'Byte',
errorCorrectionLevel: 'M'
},
imageOptions: {
hideBackgroundDots: true,
imageSize: 0.4,
margin: 8,
crossOrigin: 'anonymous'
},
dotsOptions: {
color: '#3f6a3f', // ecsess-600
type: 'rounded',
gradient: {
type: 'radial',
colorStops: [
{
offset: 0,
color: '#8fb98a' // ecsess-300
},
{
offset: 1,
color: '#2d5a2d' // ecsess-700
}
]
}
},
backgroundOptions: {
color: '#ffffff' // ecsess-white
},
cornersSquareOptions: {
color: '#3f6a3f', // ecsess-600
type: 'extra-rounded'
},
cornersDotOptions: {
color: '#3f6a3f', // ecsess-600
type: 'dot'
},
image: ECSESSLogo
});
// Create a temporary container for the high-res QR code
const tempContainer = document.createElement('div');
tempContainer.style.width = `${downloadSize + marginSize}px`;
tempContainer.style.height = `${downloadSize + marginSize}px`;
tempContainer.style.position = 'absolute';
tempContainer.style.left = '-9999px';
document.body.appendChild(tempContainer);
downloadInstance.append(tempContainer);
// Wait for the QR code to render, then download
setTimeout(() => {
downloadInstance.download({
name: 'qrcode',
extension: /** @type {'png' | 'svg' | 'jpeg' | 'webp'} */ (format)
});
// Clean up temporary container
document.body.removeChild(tempContainer);
}, 100);
}
</script>
<div bind:this={qrCodeContainer} class="max-h-sm flex max-w-sm items-center justify-center"></div>