somdigi-qr-generator 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/core.js +33 -17
package/package.json
CHANGED
package/src/core.js
CHANGED
|
@@ -6,17 +6,19 @@ class QRGenerator {
|
|
|
6
6
|
static async generate({
|
|
7
7
|
data,
|
|
8
8
|
size = 1024,
|
|
9
|
-
margin =
|
|
10
|
-
dotRadius = 0.
|
|
9
|
+
margin = 2,
|
|
10
|
+
dotRadius = 0.45,
|
|
11
11
|
gradient = null,
|
|
12
12
|
errorCorrectionLevel = "M"
|
|
13
13
|
}) {
|
|
14
14
|
if (!data) throw new Error("data required");
|
|
15
15
|
|
|
16
|
+
// Generate QR matrix
|
|
16
17
|
const qr = QRCode.create(data, { errorCorrectionLevel });
|
|
17
18
|
const cells = qr.modules;
|
|
18
19
|
const count = cells.size;
|
|
19
20
|
|
|
21
|
+
// REAL viewbox size (margin kiri + kanan)
|
|
20
22
|
const viewSize = count + margin * 2;
|
|
21
23
|
const fill = gradient ? "url(#g)" : "#000";
|
|
22
24
|
|
|
@@ -28,21 +30,24 @@ class QRGenerator {
|
|
|
28
30
|
if (this.#isFinder(x, y, count)) continue;
|
|
29
31
|
|
|
30
32
|
dots.push(
|
|
31
|
-
`<circle cx="${x + margin + 0.5}"
|
|
33
|
+
`<circle cx="${x + margin + 0.5}"
|
|
34
|
+
cy="${y + margin + 0.5}"
|
|
35
|
+
r="${dotRadius}"
|
|
36
|
+
fill="${fill}" />`
|
|
32
37
|
);
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
const svg = `
|
|
37
|
-
<svg xmlns="http://www.w3.org/2000/svg"
|
|
42
|
+
<svg xmlns="http://www.w3.org/2000/svg"
|
|
43
|
+
viewBox="0 0 ${viewSize} ${viewSize}">
|
|
38
44
|
${gradient ? this.#gradientDef(gradient) : ""}
|
|
39
45
|
<rect width="100%" height="100%" fill="white"/>
|
|
40
46
|
|
|
41
|
-
${this.#finder(
|
|
47
|
+
${this.#finder(count, margin, fill)}
|
|
42
48
|
|
|
43
49
|
${dots.join("")}
|
|
44
|
-
</svg
|
|
45
|
-
`;
|
|
50
|
+
</svg>`;
|
|
46
51
|
|
|
47
52
|
return sharp(Buffer.from(svg))
|
|
48
53
|
.resize(size, size)
|
|
@@ -51,22 +56,28 @@ class QRGenerator {
|
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
// =============================
|
|
54
|
-
// FINDER (
|
|
59
|
+
// FINDER PATTERN (DINAMIS)
|
|
55
60
|
// =============================
|
|
56
|
-
static #finder(
|
|
57
|
-
const
|
|
61
|
+
static #finder(size, margin, fill) {
|
|
62
|
+
const pos = [
|
|
58
63
|
{ x: 0, y: 0 },
|
|
59
|
-
{ x:
|
|
60
|
-
{ x: 0, y:
|
|
64
|
+
{ x: size - 7, y: 0 },
|
|
65
|
+
{ x: 0, y: size - 7 }
|
|
61
66
|
];
|
|
62
67
|
|
|
63
|
-
return
|
|
64
|
-
<rect x="${
|
|
65
|
-
|
|
66
|
-
<rect x="${
|
|
68
|
+
return pos.map(p => `
|
|
69
|
+
<rect x="${p.x + margin}" y="${p.y + margin}"
|
|
70
|
+
width="7" height="7" rx="2" fill="${fill}"/>
|
|
71
|
+
<rect x="${p.x + margin + 1}" y="${p.y + margin + 1}"
|
|
72
|
+
width="5" height="5" rx="1.5" fill="white"/>
|
|
73
|
+
<rect x="${p.x + margin + 2}" y="${p.y + margin + 2}"
|
|
74
|
+
width="3" height="3" rx="1" fill="${fill}"/>
|
|
67
75
|
`).join("");
|
|
68
76
|
}
|
|
69
77
|
|
|
78
|
+
// =============================
|
|
79
|
+
// EXCLUDE FINDER AREA
|
|
80
|
+
// =============================
|
|
70
81
|
static #isFinder(x, y, size) {
|
|
71
82
|
return (
|
|
72
83
|
(x < 7 && y < 7) ||
|
|
@@ -75,10 +86,15 @@ class QRGenerator {
|
|
|
75
86
|
);
|
|
76
87
|
}
|
|
77
88
|
|
|
89
|
+
// =============================
|
|
90
|
+
// GRADIENT SUPPORT
|
|
91
|
+
// =============================
|
|
78
92
|
static #gradientDef([from, to]) {
|
|
79
93
|
return `
|
|
80
94
|
<defs>
|
|
81
|
-
<linearGradient id="g"
|
|
95
|
+
<linearGradient id="g"
|
|
96
|
+
x1="0%" y1="0%"
|
|
97
|
+
x2="100%" y2="100%">
|
|
82
98
|
<stop offset="0%" stop-color="${from}" />
|
|
83
99
|
<stop offset="100%" stop-color="${to}" />
|
|
84
100
|
</linearGradient>
|