Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
442 lines (388 loc) · 14.9 KB
/
Copy pathscript.js
File metadata and controls
442 lines (388 loc) · 14.9 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
let defaultWidth = 2;
let defaultHeight = 55;
let DisplayValue = true;
let color = '#000000';
let fontSize = 20;
let scale = 1;
let Format = document.getElementById("format").value;
let pos = document.getElementById("position").value;
let align = document.getElementById("align").value;
let barcodeInput = document.getElementById("value").value;
function generateBarcode() {
barcodeInput = document.getElementById("value").value;
defaultWidth = parseInt(document.getElementById("widthSlider").value);
defaultHeight = parseInt(document.getElementById("heightSlider").value);
fontSize = parseInt(document.getElementById("fontSlider").value);
JsBarcode("#barcode", barcodeInput, {
format: Format,
width: defaultWidth,
height: defaultHeight,
displayValue: DisplayValue,
lineColor: color,
fontSize: fontSize,
textPosition: pos,
textAlign: align,
text: barcodeInput,
margin: 10,
marginTop: 40,
marginBottom: 10
});
updateSize();
// console.log("Barcode generated");
document.getElementById("width").innerHTML = defaultWidth;
document.getElementById("height").innerHTML = defaultHeight;
document.getElementById("fontSize").innerHTML = fontSize;
document.getElementById("barcode").style.transform = `scale(${scale})`;
const svg = document.getElementById("barcode");
svg.setAttribute("height", parseInt(svg.getAttribute("height")) + 30);
let text = document.createElementNS("http://www.w3.org/2000/svg", "text");
let text1 = document.createElementNS("http://www.w3.org/2000/svg", "text");
let PersonalText = document.getElementById("personalText").value;
let Price = document.getElementById("Price").value;
text.setAttribute("x", "50%");
text.setAttribute("y", "30");
text.setAttribute("text-anchor", "middle");
text.setAttribute("font-family", "Arial, sans-serif");
text.setAttribute("font-size", fontSize + "px");
text.setAttribute("fill", color);
text.setAttribute("style", "z-index: 1000;");
text.classList.add("svgText");
text.textContent = PersonalText;
text1.setAttribute("x", "80%");
text1.setAttribute("y", "92.5%");
text1.setAttribute("text1-anchor", "middle");
text1.setAttribute("font-family", "Arial, sans-serif");
text1.setAttribute("font-size", fontSize + "px");
text1.setAttribute("fill", color);
text1.setAttribute("style", "z-index: 1000;");
text1.classList.add("svgText");
text1.textContent = Price;
// console.log(text);
svg.appendChild(text);
svg.appendChild(text1);
let isDragging = false;
let currentX;
let currentY;
let initialX;
let initialY;
let xOffset = 0;
let yOffset = 0;
let isDragging1 = false;
let currentX1;
let currentY1;
let initialX1;
let initialY1;
let xOffset1 = 0;
let yOffset1 = 0;
text.addEventListener("mousedown", dragStart);
svg.addEventListener("mousemove", drag);
svg.addEventListener("mouseup", dragEnd);
svg.addEventListener("mouseleave", dragEnd);
text1.addEventListener("mousedown", dragStart1);
svg.addEventListener("mousemove", drag1);
svg.addEventListener("mouseup", dragEnd1);
svg.addEventListener("mouseleave", dragEnd1);
function dragStart(e) {
initialX = e.clientX - xOffset;
initialY = e.clientY - yOffset;
isDragging = true;
}
function dragStart1(e) {
initialX1 = e.clientX - xOffset1;
initialY1 = e.clientY - yOffset1;
isDragging1 = true;
}
function drag(e) {
if (isDragging) {
e.preventDefault();
currentX = e.clientX - initialX;
currentY = e.clientY - initialY;
xOffset = currentX;
yOffset = currentY;
setTranslate(currentX, currentY, text);
}
}
function drag1(e) {
if (isDragging1) {
e.preventDefault();
currentX1 = e.clientX - initialX1;
currentY1 = e.clientY - initialY1;
xOffset1 = currentX1;
yOffset1 = currentY1;
setTranslate1(currentX1, currentY1, text1);
}
}
function dragEnd(e) {
initialX = currentX;
initialY = currentY;
isDragging = false;
}
function dragEnd1(e) {
initialX1 = currentX1;
initialY1 = currentY1;
isDragging1 = false;
}
function setTranslate(xPos, yPos, el) {
el.setAttribute("transform", `translate(${xPos}, ${yPos})`);
}
function setTranslate1(xPos1, yPos1, el) {
el.setAttribute("transform", `translate(${xPos1}, ${yPos1})`);
}
}
function updateSize() {
let widthInput = document.getElementById("widthInput").value;
let heightInput = document.getElementById("heightInput").value;
if (widthInput != "" && heightInput != "") {
document.getElementById("barcode").setAttribute("width", widthInput + "px");
document.getElementById("barcode").setAttribute("height", heightInput + "px");
} else if (widthInput != "" && heightInput == "") {
document.getElementById("barcode").setAttribute("width", widthInput + "px");
} else if (heightInput != "" && widthInput == "") {
document.getElementById("barcode").setAttribute("height", heightInput + "px");
}
// console.log("Size updated");
// console.log(document.getElementById("barcode"))
}
function value() {
if(document.getElementById("check").checked) {
DisplayValue = true;
} else {
DisplayValue = false;
}
generateBarcode();
}
function dWidth() {
if (defaultWidth > 1) {
let widthSlider = document.getElementById("widthSlider");
widthSlider.value = parseInt(widthSlider.value) - 1;
widthSlider.dispatchEvent(new Event('input'));
}
generateBarcode();
}
function iWidth() {
let widthSlider = document.getElementById("widthSlider");
widthSlider.value = parseInt(widthSlider.value) + 1;
widthSlider.dispatchEvent(new Event('input'));
generateBarcode();
}
function dHeight() {
if(defaultHeight > 1) {
let heightSlider = document.getElementById("heightSlider");
heightSlider.value = parseInt(heightSlider.value) - 1;
heightSlider.dispatchEvent(new Event('input'));
}
generateBarcode();
}
function iHeight() {
let heightSlider = document.getElementById('heightSlider');
heightSlider.value = parseInt(heightSlider.value) + 1;
heightSlider.dispatchEvent(new Event('input'));
generateBarcode();
}
function updateColor() {
let value = document.getElementById("color").value;
color = value;
generateBarcode();
}
function dFont() {
if(fontSize > 1) {
let fontSlider = document.getElementById("fontSlider");
fontSlider.value = parseInt(fontSlider.value) - 1;
fontSlider.dispatchEvent(new Event('input'));
}
generateBarcode();
}
function iFont() {
let fontSlider = document.getElementById('fontSlider');
fontSlider.value = parseInt(fontSlider.value) + 1;
fontSlider.dispatchEvent(new Event('input'));
generateBarcode();
}
function updateFormat() {
Format = document.getElementById("format").value;
generateBarcode();
}
function info() {
var modal = document.getElementById('myModal');
var modalText = document.getElementById('modalText');
modalText.innerHTML = `
<ul>
<li><strong>CODE128</strong>: High-density, alphanumeric barcode. Ex: <code>1234567890AB</code></li>
<li><strong>CODE39</strong>: Alphanumeric barcode with a limited character set. Ex: <code>BOOK1234</code></li>
<li><strong>ITF-14</strong>: Used for packaging and logistics, numeric-only. Ex: <code>01234567890123</code></li>
<li><strong>EAN-13</strong>: Standard 13-digit barcode for retail products. Ex: <code>9781234567890</code></li>
<li><strong>EAN-8</strong>: Shortened 8-digit version of EAN-13. Ex: <code>12345678</code></li>
<li><strong>EAN-5</strong>: Used as an add-on to EAN-13 for additional information. Ex: <code>9781234567890-12</code></li>
<li><strong>EAN-2</strong>: Used as an add-on to EAN-13 for supplementary information. Ex: <code>9781234567890-01</code></li>
<li><strong>UPC-A</strong>: 12-digit barcode used in retail, similar to EAN-13. Ex: <code>012345678912</code></li>
<li><strong>MSI</strong>: Numeric-only barcode, used in industrial and retail environments. Ex: <code>123456789</code></li>
<li><strong>MSI10</strong>: Variant of MSI with a 10-digit length. Ex: <code>1234567890</code></li>
<li><strong>MSI11</strong>: Variant of MSI with an 11-digit length. Ex: <code>12345678901</code></li>
<li><strong>MSI1010</strong>: MSI with a 10-digit length and check digit. Ex: <code>1234567890-1</code></li>
<li><strong>MSI1110</strong>: MSI with an 11-digit length and check digit. Ex: <code>12345678901-2</code></li>
<li><strong>Pharmacode</strong>: Used in pharmaceuticals for product identification. Ex: <code>12345</code></li>
</ul>
`;
modal.style.display = "block";
}
function closeModal() {
var modal = document.getElementById('myModal');
modal.style.display = "none";
}
// Close the modal when clicking outside of it
window.onclick = function(event) {
var modal = document.getElementById('myModal');
if (event.target === modal) {
modal.style.display = "none";
}
}
function changeSize() {
const svg = document.getElementById("barcode");
const slider = document.getElementById("size");
scale = 1 + (slider.value / 100);
svg.style.transform = `scale(${scale})`;
}
function updatePosition(option) {
document.getElementById("position").value = option;
const formattedOption = option.substring(0,1).toUpperCase() + option.substring(1).toLowerCase();
pos = document.getElementById("position").value;
// pos.dispatchEvent(new Event('input'));
document.querySelectorAll('.list1 li').forEach(
li => li.classList.remove('active')
);
const element = Array.from(document.querySelectorAll('.list1 li')).find(li => li.textContent.trim() === formattedOption);
if (element){
console.log('working.')
element.classList.add('active');
} else {
console.log('not working.')
}
generateBarcode();
}
function textAlign(option) {
document.getElementById('align').value = option;
const formattedOption = option.substring(0,1).toUpperCase() + option.substring(1).toLowerCase();
document.querySelectorAll('.list2 li').forEach(
li => li.classList.remove('active')
);
const element = Array.from(document.querySelectorAll('.list2 li')).find(li => li.textContent.trim() === formattedOption)
element.classList.add('active');
align = document.getElementById("align").value;
generateBarcode();
}
function printSVG() {
var svg = document.getElementById("barcode");
// svg.style.width="188px";
// svg.style.height="94px";
var width = 1000;
var height = 676;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var printWindow = window.open("", "_blank", "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
printWindow.document.write(`
<html>
<head>
<title>Print Barcode</title>
<style>
@page {margin: 0; }
body { margin: 0; display: flex; justify-content: center; align-items: center;}
svg { width: 100%; height: 100%; }
</style>
</head>
<body>
${svg.outerHTML}
</body>
</html>
`);
printWindow.document.close();
// printWindow.onload = function() {
// printWindow.focus();
// printWindow.print();
// setTimeout(function() {
// printWindow.close();
// }, 100);
// };
}
// function printSVG() {
// var svg = document.getElementById("barcode");
// // Calculate the aspect ratio of the SVG
// var svgWidth = svg.viewBox.baseVal.width || svg.clientWidth;
// var svgHeight = svg.viewBox.baseVal.height || svg.clientHeight;
// // Calculate scaling factors to fit within 2x4 inches
// var maxWidthInches = 4;
// var maxHeightInches = 2;
// var maxWidthPixels = maxWidthInches * 96; // 96 DPI is standard
// var maxHeightPixels = maxHeightInches * 96;
// var scaleX = maxWidthPixels / svgWidth;
// var scaleY = maxHeightPixels / svgHeight;
// var scale = Math.min(scaleX, scaleY);
// // Apply scaling to SVG
// svg.setAttribute("width", svgWidth * scale + "px");
// svg.setAttribute("height", svgHeight * scale + "px");
// // Create a new window for printing
// var printWindow = window.open('', '', 'width=200,height=400');
// // Write the SVG content into the new window with CSS for correct print size
// printWindow.document.write(`
// <html>
// <head>
// <style>
// @page { size: 4in 2in; margin: 0; }
// body { margin: 0; }
// svg { width: 2in; height: auto; max-height: 4in; }
// </style>
// </head>
// <body>
// ${svg.outerHTML}
// </body>
// </html>
// `);
// printWindow.document.close();
// printWindow.onload = function() {
// printWindow.focus();
// printWindow.print();
// setTimeout(function() {
// printWindow.close();
// }, 100);
// };
// }
function advance() {
document.getElementById("advanceButton").style.display="none";
document.getElementById("b").style.display="block";
document.getElementById("c").style.display="block";
const childElements = document.querySelectorAll('#simple > *');
childElements.forEach(element => {
element.style.margin = "0";
});
document.getElementById("barcodeData").style.display="block";
}
function simple() {
document.getElementById("advanceButton").style.display="flex";
document.getElementById("b").style.display="none";
document.getElementById("c").style.display="none";
const childElements = document.querySelectorAll('#simple > *');
childElements.forEach(element => {
element.style.margin = "0 20px";
});
document.getElementById("barcodeData").style.display="none";
}
document.addEventListener("DOMContentLoaded", function() {
const sliders = document.querySelectorAll(".sliderInput");
sliders.forEach(element => {
element.addEventListener("input", () => {
console.log(element.value);
generateBarcode();
});
});
});
function download() {
const svg = document.getElementById("barcode");
const svgData = new XMLSerializer().serializeToString(svg);
const svgBlob = new Blob([svgData], {type: "image/svg+xml;charset=utf-8"});
const svgUrl = URL.createObjectURL(svgBlob);
const downloadLink = document.createElement("a");
downloadLink.href = svgUrl;
downloadLink.download = "barcode.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}