-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
943 lines (823 loc) · 34.4 KB
/
index.html
File metadata and controls
943 lines (823 loc) · 34.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Generator - HarishDevLab</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="icon" href="https://raw.githubusercontent.com/HarishDevLab/HarishDevLab/main/logo.jpg" type="image/jpeg">
<!-- qrcode.js Library via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
:root {
--primary: #5a67d8;
--primary-dark: #4c51bf;
--secondary: #7e3af2;
--accent: #0ea5e9;
--success: #10b981;
--glass-bg: rgba(255, 255, 255, 0.08);
--glass-border: rgba(255, 255, 255, 0.18);
--glass-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
--text-light: rgba(255, 255, 255, 0.95);
--text-lighter: rgba(255, 255, 255, 0.7);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
color: #333;
}
.container {
width: 100%;
max-width: 1200px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
padding: 30px;
}
@media (max-width: 992px) {
.container {
grid-template-columns: 1fr;
max-width: 700px;
}
}
.glass-panel {
background: var(--glass-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-radius: 24px;
border: 1px solid var(--glass-border);
box-shadow: var(--glass-shadow);
padding: 35px;
transition: all 0.3s ease;
}
.glass-panel:hover {
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: white;
font-size: 2.8rem;
margin-bottom: 10px;
background: linear-gradient(to right, #ffffff, #d1d5f0);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.header p {
color: var(--text-lighter);
font-size: 1.1rem;
max-width: 90%;
margin: 0 auto;
}
label {
display: block;
margin-bottom: 10px;
color: var(--text-light);
font-weight: 600;
font-size: 1.05rem;
}
#qrInput {
width: 100%;
padding: 20px;
border-radius: 16px;
border: 2px solid transparent;
background: rgba(255, 255, 255, 0.9);
font-size: 1rem;
resize: vertical;
min-height: 120px;
transition: all 0.3s;
outline: none;
margin-bottom: 10px;
}
#qrInput:focus {
border-color: var(--accent);
background: white;
box-shadow: 0 0 0 4px rgba(14, 165, 233, 0.15);
}
/* Upload Area Styling */
.upload-section {
margin: 25px 0;
}
.upload-container {
position: relative;
border: 2.5px dashed var(--glass-border);
border-radius: 18px;
padding: 30px;
text-align: center;
background: rgba(255, 255, 255, 0.03);
cursor: pointer;
transition: all 0.3s;
min-height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.upload-container:hover, .upload-container.dragover {
border-color: var(--accent);
background: rgba(14, 165, 233, 0.05);
}
.upload-container i {
font-size: 3rem;
color: var(--accent);
margin-bottom: 15px;
opacity: 0.9;
}
.upload-text {
color: var(--text-light);
margin-bottom: 8px;
font-weight: 500;
}
.upload-subtext {
color: var(--text-lighter);
font-size: 0.9rem;
display: block;
margin-bottom: 15px;
}
.browse-btn {
background: linear-gradient(135deg, var(--primary), var(--secondary));
color: white;
border: none;
padding: 10px 25px;
border-radius: 50px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 10px;
box-shadow: 0 4px 10px rgba(90, 103, 216, 0.3);
}
.browse-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 15px rgba(90, 103, 216, 0.4);
}
#logoPreview {
margin-top: 20px;
display: none;
width: 100px;
height: 100px;
object-fit: cover;
border-radius: 50%;
border: 3px solid white;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
/* Dropdown Styling with Arrow */
.select-wrapper {
position: relative;
display: flex;
flex-direction: column;
}
.select-wrapper::after {
content: '\f078';
font-family: 'Font Awesome 6 Free';
font-weight: 900;
position: absolute;
right: 15px;
top: 45px;
color: var(--primary);
pointer-events: none;
font-size: 0.9rem;
}
.select-wrapper select {
padding: 16px;
padding-right: 40px; /* Space for arrow */
border-radius: 12px;
border: 1px solid var(--glass-border);
background: rgba(255, 255, 255, 0.9);
font-size: 1rem;
font-weight: 500;
outline: none;
transition: all 0.3s;
appearance: none;
cursor: pointer;
}
.select-wrapper select:focus {
border-color: var(--accent);
box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.2);
}
.controls-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 25px 0;
}
@media (max-width: 576px) {
.controls-grid {
grid-template-columns: 1fr;
}
}
.action-buttons {
display: flex;
gap: 15px;
margin-top: 10px;
}
.btn {
padding: 17px 30px;
border-radius: 12px;
border: none;
font-size: 1.05rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
flex: 1;
}
.btn-primary {
background: linear-gradient(135deg, var(--primary), var(--secondary));
color: white;
box-shadow: 0 4px 14px rgba(90, 103, 216, 0.35);
}
.btn-primary:hover {
transform: translateY(-3px);
box-shadow: 0 7px 20px rgba(90, 103, 216, 0.4);
}
.btn-secondary {
background: rgba(255, 255, 255, 0.95);
color: #2d3748;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.btn-secondary:hover {
background: white;
transform: translateY(-3px);
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none !important;
}
.qr-display-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 450px;
}
.qr-card {
background: white;
border-radius: 22px;
padding: 35px;
margin-bottom: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
position: relative;
width: 100%;
max-width: 400px;
}
#qrcodeCanvasContainer {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#qrcodeCanvas {
border-radius: 12px;
max-width: 100%;
height: auto;
display: none;
}
.client-badge {
position: absolute;
top: -12px;
right: 20px;
background: var(--primary);
color: white;
padding: 8px 18px;
border-radius: 20px;
font-size: 0.8rem;
font-weight: 700;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
.features-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-top: 30px;
}
.feature {
display: flex;
align-items: center;
gap: 12px;
color: var(--text-lighter);
}
.feature i {
color: var(--accent);
font-size: 1.1rem;
}
.footer {
margin-top: 30px;
text-align: center;
color: var(--text-lighter);
font-size: 0.9rem;
padding-top: 20px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.hidden {
display: none;
}
.loading {
display: none;
color: #666;
text-align: center;
padding: 20px;
}
.status-message {
background: rgba(255, 255, 255, 0.9);
border-radius: 12px;
padding: 12px 20px;
margin-top: 20px;
text-align: center;
font-size: 0.95rem;
color: #2d3748;
display: none;
border-left: 4px solid var(--success);
}
.status-message i {
color: var(--success);
margin-right: 8px;
}
.scan-test {
background: rgba(255, 255, 255, 0.9);
border-radius: 12px;
padding: 15px;
margin-top: 15px;
text-align: center;
display: none;
}
.scan-test h4 {
color: var(--primary);
margin-bottom: 8px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
</style>
</head>
<body>
<div class="container">
<!-- Control Panel -->
<div class="glass-panel">
<div class="header">
<h1><i class="fas fa-qrcode"></i> QR GENERATOR</h1>
<p>Generate QR codes with logo's — 100% in your browser.</p>
</div>
<div>
<label for="qrInput"><i class="fas fa-edit"></i> Content to encode (URL, text, UPI, etc.)</label>
<textarea id="qrInput" placeholder="https://example.com OR upi://pay?pa=user@okbank OR Any text...">https://github.com/HarishDevLab</textarea>
<small style="color: var(--text-lighter); display: block; text-align: right;">Press Generate or type to update in real-time</small>
</div>
<div class="upload-section">
<label><i class="fas fa-image"></i> Center Logo (Optional)</label>
<div class="upload-container" id="uploadContainer">
<i class="fas fa-cloud-upload-alt"></i>
<p class="upload-text">Drag & drop your logo here</p>
<span class="upload-subtext">or</span>
<button class="browse-btn" id="browseBtn">
<i class="fas fa-folder-open"></i> Browse Files
</button>
<img id="logoPreview" alt="Logo Preview">
<input type="file" id="logoUpload" accept="image/*" class="hidden">
</div>
<small style="color: var(--text-lighter); display: block; text-align: center; margin-top: 10px;">
JPG, PNG or SVG • Max size: 100MB
</small>
</div>
<div class="controls-grid">
<div class="select-wrapper">
<label for="logoShape"><i class="fas fa-crop-alt"></i> Logo Shape</label>
<select id="logoShape">
<option value="circle">Circle</option>
<option value="square">Square</option>
</select>
</div>
<div class="select-wrapper">
<label for="qrStyle"><i class="fas fa-palette"></i> QR Code Style</label>
<select id="qrStyle">
<option value="standard">Standard Black</option>
<option value="gradient">Professional Gradient</option>
</select>
</div>
</div>
<div class="action-buttons">
<button class="btn btn-primary" id="generateBtn">
<i class="fas fa-bolt"></i> Generate QR Code
</button>
<!--<button class="btn btn-secondary" id="downloadBtn" disabled>
<i class="fas fa-download"></i> Download PNG
</button>-->
</div>
<div class="footer">
<p>HarishDevLab © 2026 | Powered by qrcode.js | All processing happens securely in your browser.</p>
</div>
</div>
<!-- Display Panel -->
<div class="glass-panel">
<div class="qr-display-container">
<div class="client-badge"><i class="fas fa-lock"></i> QR CODE</div>
<div class="qr-card">
<div id="qrcodeCanvasContainer">
<p id="placeholderText" style="color: #718096; text-align: center; padding: 40px 20px;">
<i class="fas fa-qrcode" style="font-size: 3rem; opacity: 0.3; display: block; margin-bottom: 15px;"></i>
Your QR code will appear here.
</p>
<canvas id="qrcodeCanvas"></canvas>
</div>
</div>
<div class="loading" id="loading"><i class="fas fa-spinner fa-spin"></i> Finalizing high-quality QR code...</div>
<div class="status-message" id="statusMessage">
<i class="fas fa-check-circle"></i>
<span>QR code ready! You can scan it or download.</span>
</div>
<div class="scan-test" id="scanTest">
<h4><i class="fas fa-qrcode"></i></h4>
<p style="color: #4a5568; font-size: 0.9rem; margin-bottom: 10px;">
Test this QR before downloading or sharing.
</p>
<div style="display: flex; gap: 10px; justify-content: center;">
<span style="background: #e6fffa; color: #065f46; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem;">
<i class="fas fa-link"></i> URLs
</span>
<span style="background: #f0f9ff; color: #075985; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem;">
<i class="fas fa-wallet"></i> UPI
</span>
<span style="background: #fef3c7; color: #92400e; padding: 4px 12px; border-radius: 20px; font-size: 0.8rem;">
<i class="fas fa-font"></i> Text
</span>
</div>
</div>
<div class="action-buttons"></div>
<button class="btn btn-secondary" id="downloadBtn" disabled>
<i class="fas fa-download"></i> Download PNG
</button>
</div>
</div>
</div>
</div>
<script>
// ==================== DOM & State ====================
const qrInput = document.getElementById('qrInput');
const logoUpload = document.getElementById('logoUpload');
const uploadContainer = document.getElementById('uploadContainer');
const browseBtn = document.getElementById('browseBtn');
const logoPreview = document.getElementById('logoPreview');
const logoShape = document.getElementById('logoShape');
const qrStyle = document.getElementById('qrStyle');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn');
const qrCanvas = document.getElementById('qrcodeCanvas');
const placeholderText = document.getElementById('placeholderText');
const loadingDiv = document.getElementById('loading');
const statusMessage = document.getElementById('statusMessage');
const scanTest = document.getElementById('scanTest');
const ctx = qrCanvas.getContext('2d');
let qrcodeLib = null; // Will hold the QRCode.js instance
let logoImage = null;
let currentQRCodeDataURL = null;
let generationTimeout = null;
// ==================== Initialize QRCode.js ====================
function initQRCode() {
// Create a hidden container for the library to work with
const hiddenContainer = document.createElement('div');
hiddenContainer.id = 'hiddenQrContainer';
hiddenContainer.style.display = 'none';
document.body.appendChild(hiddenContainer);
// Initialize the QRCode.js instance with default options
qrcodeLib = new QRCode(hiddenContainer, {
text: 'https://github.com/HarishDevLab',
width: 256,
height: 256,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H, // High error correction (30%) for logos
margin: 2 // Quiet zone
});
console.log('QRCode.js library initialized.');
}
// ==================== Event Listeners ====================
window.addEventListener('DOMContentLoaded', () => {
initQRCode();
setupDragAndDrop();
generateQRCode(); // Generate initial code
// Real-time generation with debouncing
qrInput.addEventListener('input', () => {
clearTimeout(generationTimeout);
generationTimeout = setTimeout(generateQRCode, 500);
});
generateBtn.addEventListener('click', generateQRCode);
downloadBtn.addEventListener('click', downloadQRCode);
logoUpload.addEventListener('change', handleLogoUpload);
browseBtn.addEventListener('click', () => logoUpload.click());
logoShape.addEventListener('change', generateQRCode);
qrStyle.addEventListener('change', generateQRCode);
});
// ==================== Logo Handling ====================
function handleLogoUpload(event) {
const file = event.target.files[0];
if (!file) return;
// Check file size (max 100MB)
if (file.size > 100 * 1024 * 1024) {
showStatus('File size should be less than 100MB', 'error');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
// Clear previous logo image
logoImage = null;
// Create new image
logoImage = new Image();
logoImage.onload = function() {
// Update preview
logoPreview.src = e.target.result;
logoPreview.style.display = 'block';
// Update upload container text
uploadContainer.querySelector('.upload-text').innerHTML =
'<i class="fas fa-check-circle" style="color: #10b981; margin-right: 5px;"></i> Logo Uploaded';
uploadContainer.querySelector('.upload-subtext').textContent = 'Click "Browse Files" to replace';
// Generate QR code with new logo
generateQRCode();
};
logoImage.onerror = function() {
showStatus('Error loading image. Please try another file.', 'error');
logoImage = null;
};
logoImage.src = e.target.result;
};
reader.onerror = function() {
showStatus('Error reading file', 'error');
};
reader.readAsDataURL(file);
}
function setupDragAndDrop() {
// Prevent default drag behaviors
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
uploadContainer.addEventListener(eventName, preventDefaults, false);
document.body.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Highlight drop zone when dragging over it
['dragenter', 'dragover'].forEach(eventName => {
uploadContainer.addEventListener(eventName, () => {
uploadContainer.classList.add('dragover');
}, false);
});
['dragleave', 'drop'].forEach(eventName => {
uploadContainer.addEventListener(eventName, () => {
uploadContainer.classList.remove('dragover');
}, false);
});
// Handle dropped files
uploadContainer.addEventListener('drop', (e) => {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length) {
// Replace the file input with new file
logoUpload.files = files;
handleLogoUpload({ target: { files: files } });
}
});
}
// ==================== Core QR Generation ====================
function generateQRCode() {
const text = qrInput.value.trim();
if (!text) {
showStatus('Please enter some text or a URL first.', 'warning');
scanTest.style.display = 'none';
return;
}
// Show loading state
generateBtn.disabled = true;
generateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...';
loadingDiv.style.display = 'block';
placeholderText.style.display = 'none';
// Use the library's makeCode method to generate the QR code
try {
qrcodeLib.makeCode(text);
} catch (error) {
console.error('QRCode.js error:', error);
showStatus('Error generating QR code. Content may be too long.', 'error');
resetUI();
return;
}
// Give the library a moment to render
setTimeout(() => {
try {
// The library renders to the hidden container, get its canvas
const hiddenCanvas = document.querySelector('#hiddenQrContainer canvas');
if (!hiddenCanvas) throw new Error("QR code canvas not found");
// Prepare our display canvas
const qrSize = 300;
const quietZone = 40;
const finalSize = qrSize + (quietZone * 2);
qrCanvas.width = finalSize;
qrCanvas.height = finalSize;
// Clear canvas
ctx.clearRect(0, 0, finalSize, finalSize);
// Draw quiet zone background (subtle gradient)
const bgGrad = ctx.createLinearGradient(0, 0, finalSize, finalSize);
bgGrad.addColorStop(0, '#f8fafc');
bgGrad.addColorStop(1, '#f1f5f9');
ctx.fillStyle = bgGrad;
ctx.fillRect(0, 0, finalSize, finalSize);
// Draw QR code with rounded corners
const qrX = quietZone;
const qrY = quietZone;
const cornerRadius = 14;
// Save context state
ctx.save();
// Create clipping path for rounded QR code
ctx.beginPath();
roundRect(ctx, qrX, qrY, qrSize, qrSize, cornerRadius);
ctx.clip();
// Draw the generated QR code from the library
ctx.drawImage(hiddenCanvas, qrX, qrY, qrSize, qrSize);
// Apply professional gradient if selected
if (qrStyle.value === 'gradient') {
applyGradientToQR(ctx, qrX, qrY, qrSize);
}
// Add logo if uploaded (PROPERLY using library's error correction)
if (logoImage) {
// Use smaller logo size (15% of QR code) to preserve scannability
const logoSize = qrSize * 0.15;
const logoX = qrX + (qrSize - logoSize) / 2;
const logoY = qrY + (qrSize - logoSize) / 2;
addLogoToQR(ctx, logoX, logoY, logoSize);
}
// Restore context
ctx.restore();
// Draw a subtle border around the quiet zone
ctx.strokeStyle = 'rgba(0, 0, 0, 0.05)';
ctx.lineWidth = 1;
ctx.strokeRect(0.5, 0.5, finalSize - 1, finalSize - 1);
// Finalize
qrCanvas.style.display = 'block';
currentQRCodeDataURL = qrCanvas.toDataURL('image/png');
downloadBtn.disabled = false;
// Test the QR code content type and show appropriate message
const contentMsg = testContentType(text);
showStatus(`QR code generated! ${contentMsg}`, 'success');
scanTest.style.display = 'block';
} catch (error) {
console.error('QR generation error:', error);
showStatus('Error rendering QR code. Please try again.', 'error');
placeholderText.style.display = 'block';
qrCanvas.style.display = 'none';
scanTest.style.display = 'none';
} finally {
resetUI();
}
}, 50);
}
function testContentType(text) {
if (text.startsWith('http://') || text.startsWith('https://')) {
return 'Contains a website link.';
} else if (text.toLowerCase().includes('upi://') || text.includes('@')) {
return 'Contains UPI payment info.';
} else if (text.includes('\n') || text.length > 100) {
return 'Contains text content.';
} else {
return 'Ready to scan!';
}
}
// ==================== Visual Effects ====================
function applyGradientToQR(ctx, x, y, size) {
// 1. Get image data of the QR code area
const imageData = ctx.getImageData(x, y, size, size);
const data = imageData.data;
const gradient = ctx.createLinearGradient(x, y, x + size, y + size);
gradient.addColorStop(0, '#4f46e5'); // Indigo
gradient.addColorStop(0.5, '#7c3aed'); // Violet
gradient.addColorStop(1, '#0ea5e9'); // Sky
// 2. Create a temporary canvas for the gradient
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = size;
tempCanvas.height = size;
// 3. Draw the gradient on the temporary canvas
tempCtx.fillStyle = gradient;
tempCtx.fillRect(0, 0, size, size);
const gradientData = tempCtx.getImageData(0, 0, size, size).data;
// 4. Apply gradient ONLY to dark pixels
for (let i = 0; i < data.length; i += 4) {
// If pixel is dark (part of QR code)
// Check if it's not white (white is usually 255,255,255)
if (data[i] < 200 || data[i+1] < 200 || data[i+2] < 200) {
// Replace dark pixel color with gradient color
data[i] = gradientData[i]; // Red
data[i+1] = gradientData[i+1]; // Green
data[i+2] = gradientData[i+2]; // Blue
// Keep the same alpha value
}
}
// 5. Put the modified image data back
ctx.putImageData(imageData, x, y);
}
function addLogoToQR(ctx, logoX, logoY, logoSize) {
// Create white background for logo (helps with contrast)
ctx.fillStyle = '#FFFFFF';
if (logoShape.value === 'circle') {
// Draw circle background
ctx.beginPath();
ctx.arc(logoX + logoSize / 2, logoY + logoSize / 2, logoSize / 2, 0, Math.PI * 2);
ctx.fill();
// Save context for clipping
ctx.save();
// Create circular clipping path
ctx.beginPath();
ctx.arc(logoX + logoSize / 2, logoY + logoSize / 2, logoSize / 2, 0, Math.PI * 2);
ctx.clip();
// Draw the logo image (centered and scaled)
ctx.drawImage(logoImage, logoX, logoY, logoSize, logoSize);
// Restore context
ctx.restore();
} else {
// Square with rounded corners
const logoBgRadius = 6;
// Draw square background
ctx.beginPath();
roundRect(ctx, logoX, logoY, logoSize, logoSize, logoBgRadius);
ctx.fill();
// Save context for clipping
ctx.save();
// Create square clipping path
ctx.beginPath();
roundRect(ctx, logoX, logoY, logoSize, logoSize, logoBgRadius);
ctx.clip();
// Draw the logo image
ctx.drawImage(logoImage, logoX, logoY, logoSize, logoSize);
// Restore context
ctx.restore();
}
}
function roundRect(ctx, x, y, width, height, radius) {
if (radius > width / 2) radius = width / 2;
if (radius > height / 2) radius = height / 2;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
// ==================== Download & UI Helpers ====================
function downloadQRCode() {
if (!currentQRCodeDataURL) return;
const link = document.createElement('a');
const timestamp = new Date().toISOString().slice(0, 19).replace(/[:]/g, '-');
link.download = `qr-studio-pro-${timestamp}.png`;
link.href = currentQRCodeDataURL;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Show feedback
const originalText = downloadBtn.innerHTML;
downloadBtn.innerHTML = '<i class="fas fa-check"></i> Downloaded!';
downloadBtn.style.background = 'linear-gradient(135deg, #10b981, #059669)';
setTimeout(() => {
downloadBtn.innerHTML = originalText;
downloadBtn.style.background = '';
}, 2000);
}
function showStatus(message, type) {
statusMessage.querySelector('span').textContent = message;
statusMessage.style.borderLeftColor = type === 'success' ? '#10b981' :
type === 'warning' ? '#f59e0b' : '#ef4444';
statusMessage.querySelector('i').className = type === 'success' ? 'fas fa-check-circle' :
type === 'warning' ? 'fas fa-exclamation-triangle' :
'fas fa-exclamation-circle';
statusMessage.style.display = 'block';
setTimeout(() => {
if (statusMessage.style.display === 'block') statusMessage.style.display = 'none';
}, 5000);
}
function resetUI() {
generateBtn.disabled = false;
generateBtn.innerHTML = '<i class="fas fa-bolt"></i> Generate QR Code';
loadingDiv.style.display = 'none';
}
</script>
</body>
</html>