-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (117 loc) · 3.46 KB
/
script.js
File metadata and controls
141 lines (117 loc) · 3.46 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
const userInput = document.getElementById('user-input');
const canvas = document.getElementById('canvas');
const heads = document.querySelectorAll('.editor-head');
const imageInput = document.getElementById('user-image');
const preferWidth = document.getElementById('width');
const preferHeight = document.getElementById('height');
const downloadBtn = document.getElementById('downloadBtn');
const downloadFormat = document.getElementById('downloadFormat');
const dotStyle = document.getElementById('dot-style');
const dotColor = document.getElementById('dot-color');
dotColor.addEventListener('input', () => qrCode.update({
dotsOptions: {
color: dotColor.value,
}
}));
let selectedImage = null;
let selectedImageDataUrl = null;
heads.forEach(head => {
head.addEventListener('click', () => {
const targetId = head.getAttribute('data-target');
const targetDiv = document.getElementById(targetId);
if (targetDiv) {
targetDiv.classList.toggle('hidden');
}
});
});
const qrCode = new QRCodeStyling({
width: 300,
height: 300,
type: "canvas",
data: userInput.value,
image: selectedImageDataUrl,
dotsOptions: {
color: "#001014",
type: dotStyle.value,
},
backgroundOptions: {
color: "#ffffff",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 20
},
qrOptions: {
typeNumber: 0,
mode: '',
errorCorrectionLevel: 'Q'
}
});
qrCode.append(canvas);
dotStyle.addEventListener('input', () => qrCode.update({
dotsOptions: {
type: dotStyle.value,
}
}));
userInput.addEventListener('input', () => qrCode.update({
data: userInput.value,
}));
preferWidth.addEventListener('input', () => qrCode.update({
width: preferWidth.value,
}));
preferHeight.addEventListener('input', () => qrCode.update({
height: preferHeight.value,
}));
imageInput.addEventListener('change', validateFile);
document.getElementById('remove-image').addEventListener('click', () => {
console.log('button click')
imageInput.value = '';
selectedImage = null;
selectedImageDataUrl = null;
qrCode.update({
image: selectedImageDataUrl
});
})
downloadBtn.addEventListener('click', () => {
const format = downloadFormat.value;
qrCode.download({ name: `QR Code Generator ${Math.floor(Math.random() * 10) + 1}`, extension: format });
})
function validateFile() {
const maxSizeMB = 5;
const maxSizeBytes = maxSizeMB * 1024 * 1024;
if (imageInput.files.length === 0) {
alert('Please select an image file.');
return;
}
if (imageInput.files.length > 1) {
alert('Only one file can be uploaded.');
imageInput.value = '';
return;
}
const file = imageInput.files[0];
if (!file.type.startsWith('image/')) {
alert('Please select a valid image file (e.g., JPG, PNG, GIF).');
imageInput.value = '';
return;
}
if (file.size > maxSizeBytes) {
alert(`File size exceeds ${maxSizeMB}MB limit. Please select a smaller file.`);
imageInput.value = '';
return;
}
selectedImage = file;
const reader = new FileReader();
reader.onload = function (event) {
const selectedImageDataUrl = event.target.result;
qrCode.update({
image: selectedImageDataUrl
});
};
reader.onerror = function () {
alert('Error reading file. Please try again.');
imageInput.value = '';
selectedImage = null;
selectedImageDataUrl = null;
};
reader.readAsDataURL(file);
}