@liquid-js/qr-code-styling 2.0.3 → 2.0.5

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/README.md CHANGED
@@ -31,52 +31,33 @@ If you have issues / suggestions / notes / questions, please open an issue or co
31
31
  </head>
32
32
  <body>
33
33
  <div id="canvas"></div>
34
+ <button type="button" id="dl">Download</button>
34
35
  <script type="module">
35
- import { QRCodeStyling } from "https://unpkg.com/@liquid-js/qr-code-styling/lib/qr-code-styling.js";
36
+ import { QRCodeStyling, browserUtils } from "./index.ts";
36
37
 
37
- const options = {
38
- shape: "circle",
38
+ const qrCode = new QRCodeStyling({
39
+ width: 300,
40
+ height: 300,
39
41
  type: "svg",
40
- data: "h",
42
+ data: "https://www.facebook.com/",
41
43
  image: "https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg",
42
44
  dotsOptions: {
43
- type: "extra-rounded",
44
- gradient: {
45
- type: "linear",
46
- rotation: Math.PI / 2,
47
- colorStops: [
48
- { offset: 0, color: "blue" },
49
- { offset: 0.5, color: "red" },
50
- { offset: 1, color: "green" }
51
- ]
52
- }
45
+ color: "#4267b2",
46
+ type: "rounded"
53
47
  },
54
- cornersSquareOptions: {
55
- type: "rounded",
56
- gradient: {
57
- type: "linear",
58
- rotation: Math.PI * 0.2,
59
- colorStops: [
60
- {
61
- offset: 0,
62
- color: "blue"
63
- },
64
- {
65
- offset: 1,
66
- color: "red"
67
- }
68
- ]
69
- }
48
+ backgroundOptions: {
49
+ color: "#e9ebee"
70
50
  },
71
51
  imageOptions: {
72
52
  crossOrigin: "anonymous",
73
- imageSize: 1,
74
- margin: 1
53
+ margin: 20
75
54
  }
76
- };
77
- const qrCode = new QRCodeStyling(options);
55
+ });
78
56
 
79
57
  qrCode.append(document.getElementById("canvas"));
58
+ document.getElementById("dl").addEventListener("click", () => {
59
+ browserUtils.download(qrCode, { extension: "png" }, { width: 1200, height: 1200 });
60
+ });
80
61
  </script>
81
62
  </body>
82
63
  </html>
@@ -107,7 +88,6 @@ new QRCodeStyling(options) => QRCodeStyling
107
88
  | cornersSquareOptions | object | | Square in the corners styling options |
108
89
  | cornersDotOptions | object | | Dots in the corners styling options |
109
90
  | backgroundOptions | object \| boolean | `false` | QR background styling options, false to disable background |
110
- | margin | number | `0` | Margin (in blocks) to add to the code |
111
91
 
112
92
  `options.qrOptions` structure
113
93
 
@@ -153,11 +133,12 @@ new QRCodeStyling(options) => QRCodeStyling
153
133
 
154
134
  `options.backgroundOptions` structure
155
135
 
156
- | Property | Type | Default Value | Description |
157
- | -------- | ---------------- | ------------- | ------------------- |
158
- | round | number (`0 - 1`) | `0` | Background roundnes |
159
- | color | string | | |
160
- | gradient | object | | |
136
+ | Property | Type | Default Value | Description |
137
+ | -------- | ---------------- | ------------- | ----------------------------------------------------- |
138
+ | round | number (`0 - 1`) | `0` | Background roundnes |
139
+ | color | string | | |
140
+ | gradient | object | | |
141
+ | margin | number | `0` | Margin (in blocks) between background and the QR code |
161
142
 
162
143
  Gradient structure
163
144
 
@@ -228,16 +209,16 @@ const extension = (svg, options) => {
228
209
  const size = Math.min(width, height);
229
210
  const border = document.createElementNS("http://www.w3.org/2000/svg", "rect");
230
211
  const borderAttributes = {
231
- "fill": "none",
232
- "x": (width - size + 40) / 2,
233
- "y": (height - size + 40) / 2,
234
- "width": size - 40,
235
- "height": size - 40,
236
- "stroke": 'black',
212
+ fill: "none",
213
+ x: (width - size + 40) / 2,
214
+ y: (height - size + 40) / 2,
215
+ width: size - 40,
216
+ height: size - 40,
217
+ stroke: "black",
237
218
  "stroke-width": 40,
238
- "rx": 100,
219
+ rx: 100
239
220
  };
240
- Object.keys(borderAttributes).forEach(attribute => {
221
+ Object.keys(borderAttributes).forEach((attribute) => {
241
222
  border.setAttribute(attribute, borderAttributes[attribute]);
242
223
  });
243
224
  svg.appendChild(border);
@@ -4,7 +4,6 @@ export interface RequiredOptions extends Options {
4
4
  shape: ShapeType;
5
5
  width: number;
6
6
  height: number;
7
- margin: number;
8
7
  data: string;
9
8
  qrOptions: {
10
9
  typeNumber: TypeNumber;
@@ -131,6 +131,8 @@ function sanitizeOptions(options) {
131
131
  if (newOptions.dotsOptions.gradient) {
132
132
  newOptions.dotsOptions.gradient = sanitizeGradient(newOptions.dotsOptions.gradient);
133
133
  }
134
+ // Ensure integer dot size
135
+ newOptions.dotsOptions.size = Math.round(Math.max(0, newOptions.dotsOptions.size) || 10);
134
136
  if (newOptions.cornersSquareOptions) {
135
137
  newOptions.cornersSquareOptions = {
136
138
  ...newOptions.cornersSquareOptions
@@ -165,7 +167,6 @@ const defaultOptions = {
165
167
  shape: ShapeType.square,
166
168
  width: undefined,
167
169
  height: undefined,
168
- margin: 0,
169
170
  data: "",
170
171
  qrOptions: {
171
172
  typeNumber: TypeNumber[0],
@@ -905,7 +906,8 @@ class QRSVG {
905
906
  }
906
907
  }
907
908
  if (options.shape === ShapeType.circle) {
908
- const additionalDots = Math.floor((minSize / dotSize - count - 2 * this._options.margin) / 2);
909
+ const margin = (this._options.backgroundOptions && this._options.backgroundOptions.margin) || 0;
910
+ const additionalDots = Math.floor((minSize / dotSize - count - 2 * margin) / 2);
909
911
  const fakeCount = count + additionalDots * 2;
910
912
  const xFakeBeginning = xBeginning - additionalDots * dotSize;
911
913
  const yFakeBeginning = yBeginning - additionalDots * dotSize;
@@ -1182,7 +1184,8 @@ let QRCodeStyling$1 = class QRCodeStyling {
1182
1184
  return;
1183
1185
  }
1184
1186
  const count = Math.ceil((this._options.shape == ShapeType.circle ? Math.sqrt(2) : 1) * this._qr.getModuleCount());
1185
- const size = this._options.dotsOptions.size * (count + 2 * this._options.margin);
1187
+ const margin = (this._options.backgroundOptions && this._options.backgroundOptions.margin) || 0;
1188
+ const size = Math.ceil(this._options.dotsOptions.size * (count + 2 * margin));
1186
1189
  const qrSVG = new QRSVG({
1187
1190
  ...this._options,
1188
1191
  width: size,