qr-code-styling-node 1.12.0 → 1.13.0

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
@@ -231,6 +231,38 @@ const result = await qrCode.share({ name: "wifi", extension: "png", title: "Wi-F
231
231
  // "shared", "cancelled" or "downloaded"
232
232
  ```
233
233
 
234
+ ### Plugins
235
+
236
+ A plugin is a plain object with two optional hooks. `configure(options)` returns option changes to merge in before
237
+ drawing. `afterDraw(draw)` draws on top of the finished code, with the canvas context or the SVG element. It gets
238
+ the code's area in pixels (`x`, `y`, `size`, `dotSize`, `count`), and that area accounts for frames.
239
+
240
+ ```js
241
+ const ring = {
242
+ name: "ring",
243
+ configure: (options) => (options.margin < 24 ? { margin: 24 } : undefined),
244
+ afterDraw: ({ type, area, context, svg, document }) => {
245
+ const cx = area.x + area.size / 2, cy = area.y + area.size / 2, r = area.size * 0.75;
246
+ if (type === "canvas") {
247
+ context.beginPath();
248
+ context.arc(cx, cy, r, 0, Math.PI * 2);
249
+ context.strokeStyle = "#e11d48";
250
+ context.stroke();
251
+ } else {
252
+ const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
253
+ Object.entries({ cx, cy, r, fill: "none", stroke: "#e11d48" }).forEach(([k, v]) => circle.setAttribute(k, v));
254
+ svg.appendChild(circle);
255
+ }
256
+ }
257
+ };
258
+
259
+ new QRCodeStyling({ data: "https://example.com", plugins: [ring] });
260
+ ```
261
+
262
+ Plugins run in order, and async `afterDraw` hooks are awaited. `configure` runs again on every `update()`, so it
263
+ should return the same changes for the same options. Check the result with `verify()` when a plugin draws near
264
+ the code.
265
+
234
266
  ### Checking that a code scans
235
267
 
236
268
  `verify()` renders the code and decodes it with a QR decoder you pass in, so the library itself stays dependency-free.
@@ -268,6 +300,7 @@ height |number |`300` |Size of canvas
268
300
  type |string (`'canvas' 'svg'`)|`canvas` |The type of the element that will be rendered
269
301
  shape |string (`'square' 'circle'`)|`'square'` |`'circle'` draws a round code with decorative dots around the real one
270
302
  svgOptions |object `{ mergePaths }` | |SVG only: `mergePaths: true` draws all dots as one path (smaller files)
303
+ plugins |array | |Plugins with `configure` and `afterDraw` hooks, see [Plugins](#plugins)
271
304
  data |string | |The date will be encoded to the QR code
272
305
  image |string | |The image will be copied to the center of the QR code
273
306
  margin |number |`0` |Margin around canvas
@@ -18,6 +18,7 @@ export default class QRCanvas {
18
18
  clear(): void;
19
19
  _drawFramed(qr: QRCode): Promise<void>;
20
20
  drawQR(qr: QRCode): Promise<void>;
21
+ _drawCode(qr: QRCode): Promise<void>;
21
22
  drawBackground(): void;
22
23
  drawDots(filter?: FilterFunction): void;
23
24
  _drawArtRects(rects: ArtRect[]): void;
@@ -27,6 +27,7 @@ export default class QRSVG {
27
27
  _svgElement(name: string, attributes: Record<string, string | number>): SVGElement;
28
28
  _drawFramed(qr: QRCode): Promise<void>;
29
29
  drawQR(qr: QRCode): Promise<void>;
30
+ _drawCode(qr: QRCode): Promise<void>;
30
31
  drawBackground(): void;
31
32
  drawDots(filter?: FilterFunction): void;
32
33
  drawCorners(): void;