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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/core.js +33 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "somdigi-qr-generator",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
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 = 1,
10
- dotRadius = 0.48,
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}" cy="${y + margin + 0.5}" r="${dotRadius}" fill="${fill}" />`
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" viewBox="0 0 ${viewSize} ${viewSize}">
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(viewSize, margin, fill)}
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 (SOLID + OUTLINE)
59
+ // FINDER PATTERN (DINAMIS)
55
60
  // =============================
56
- static #finder(_, margin, fill) {
57
- const p = [
61
+ static #finder(size, margin, fill) {
62
+ const pos = [
58
63
  { x: 0, y: 0 },
59
- { x: 21, y: 0 },
60
- { x: 0, y: 21 }
64
+ { x: size - 7, y: 0 },
65
+ { x: 0, y: size - 7 }
61
66
  ];
62
67
 
63
- return p.map(v => `
64
- <rect x="${v.x + margin}" y="${v.y + margin}" width="7" height="7" rx="2" fill="${fill}"/>
65
- <rect x="${v.x + margin + 1}" y="${v.y + margin + 1}" width="5" height="5" rx="1.5" fill="white"/>
66
- <rect x="${v.x + margin + 2}" y="${v.y + margin + 2}" width="3" height="3" rx="1" fill="${fill}"/>
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" x1="0%" y1="0%" x2="100%" y2="100%">
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>