Skip to content

Commit b95fb12

Browse files
committed
docs: border plugin
1 parent 4e2a002 commit b95fb12

File tree

10 files changed

+59
-12
lines changed

10 files changed

+59
-12
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,48 @@ const buffer = await new Promise((resolve) => {
125125
await writeFile('qr.pdf', buffer)
126126
```
127127

128+
### Plugins
129+
130+
Version `5.0.0` of the library introduces plugin support, enabling you to further customize the shape of dots and corners. Additionally, plugins can apply effects, such as adding borders, to enhance the final QR code output.
131+
132+
<p float="left">
133+
<img style="display:inline-block" src="https://raw.githubusercontent.com/Liquid-JS/qr-code-styling/master/src/assets/border.png" width="240" />
134+
</p>
135+
136+
#### Plugin development
137+
138+
A simple plugin example to customize QR code dots:
139+
140+
```ts
141+
import { svgPath, DrawArgs } from '@liquid-js/qr-code-styling/plugin-utils'
142+
143+
const qrCode = new QRCodeStyling({
144+
plugins: [
145+
{
146+
drawDot: (args: DrawArgs): SVGElement | undefined => {
147+
const { size, x, y, document } = args
148+
149+
const element = document.createElementNS('http://www.w3.org/2000/svg', 'path')
150+
// Insert your own SVG path definition, or implement neighbour-aware logic through args.getNeighbor()
151+
element.setAttribute(
152+
'd',
153+
svgPath`M ${x} ${y + (size - size) / 2}
154+
v ${size}
155+
h ${size / 2}
156+
a ${size / 2} ${size / 2} 0 0 0 0 ${-size}
157+
z`
158+
)
159+
160+
return element
161+
}
162+
},
163+
]
164+
// ...other options
165+
})
166+
```
167+
168+
See [Border plugin](https://github.com/Liquid-JS/qr-code-styling/tree/master/src/plugins/border.js) for further reference.
169+
128170
### Kanji support
129171

130172
For Kanji mode to work, import `stringToBytesFuncs` from `@liquid-js/qr-code-styling/kanji` and inclue it with config.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@
115115
"logo",
116116
"design",
117117
"node",
118-
"nodejs"
118+
"nodejs",
119+
"plugin",
120+
"plugins"
119121
],
120122
"author": "HitkoDev <development@hitko.si>",
121123
"license": "MIT",

src/assets/border.png

207 KB
Loading

src/core/qr-svg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export class QRSVG {
566566
const draw = getQrCornerDotFigure(options.cornersDotOptions.type)
567567

568568
if (cornersDotMaskGroup) {
569-
cornersDotMaskGroup.appendChild(draw(squareArgs))
569+
cornersDotMaskGroup.appendChild(draw(dotArgs))
570570
}
571571
} else {
572572
const draw = getQrDotFigure(options.cornersDotOptions?.type || options.dotsOptions.type, options.plugins)

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export { FileExtension } from './tools/browser-utils.js'
44
export { type RecursivePartial } from './types/helper.js'
55
export { type CanvasOptions } from './utils/canvas-options.js'
66
export { GradientType, type Gradient } from './utils/gradient.js'
7-
export { CornerDotType, CornerSquareType, DotType, ImageMode, ShapeType, type Options } from './utils/options.js'
7+
export { CornerDotType, CornerSquareType, DotType, ImageMode, ShapeType, type Options, type Plugin } from './utils/options.js'
88
export { ErrorCorrectionPercents } from './utils/qrcode.js'
99
import * as _browserUtils from './tools/browser-utils.js'
1010

src/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { FileExtension } from './tools/browser-utils.js'
1111
export { type RecursivePartial } from './types/helper.js'
1212
export { type CanvasOptions } from './utils/canvas-options.js'
1313
export { GradientType, type Gradient } from './utils/gradient.js'
14-
export { CornerDotType, CornerSquareType, DotType, ImageMode, ShapeType, type Options } from './utils/options.js'
14+
export { CornerDotType, CornerSquareType, DotType, ImageMode, ShapeType, type Options, type Plugin } from './utils/options.js'
1515
export { ErrorCorrectionPercents } from './utils/qrcode.js'
1616

1717
export const browserUtils: typeof _browserUtils | undefined = undefined

src/plugins/border.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export class BorderPlugin implements Plugin {
4646
+ (this.pluginOptions.innerBorder?.size || 0) + (this.pluginOptions.innerBorder?.margin || 0)
4747
+ (this.pluginOptions.outerBorder?.size || 0) + (this.pluginOptions.outerBorder?.margin || 0)
4848
)
49-
const vb = extendSVG(svg, thickness)
50-
if (!vb)
49+
const drawArea = extendSVG(svg, thickness)
50+
if (!drawArea)
5151
return
5252

53-
const cx = (vb.left * 2 + vb.right) / 2
54-
const cy = (vb.top * 2 + vb.bottom) / 2
53+
const cx = (drawArea.left + drawArea.right) / 2
54+
const cy = (drawArea.top + drawArea.bottom) / 2
5555
const lineSize = Math.min(options.width, options.height) / 2 * (1 - (this.pluginOptions.round || 0))
56-
let r = (Math.min(vb.right - 2 * thickness, vb.bottom - 2 * thickness) / 2) - lineSize
56+
let r = (Math.min(drawArea.right - drawArea.left - 2 * thickness, drawArea.bottom - drawArea.top - 2 * thickness) / 2) - lineSize
5757

5858
const drawBorder = (cfg: BorderConfig) => {
5959
const t = cfg.size

src/plugins/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { type DrawArgs } from '../types/helper.js'
12
export * from '../utils/svg.js'

src/utils/svg.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export function extendSVG(svg: SVGSVGElement, padding: number) {
6363
return undefined
6464
return {
6565
left: vb[0],
66-
right: vb[2],
66+
right: vb[2] + vb[0],
6767
top: vb[1],
68-
bottom: vb[3]
68+
bottom: vb[3] + vb[1]
6969
}
7070
}

typedoc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"entryPoints": [
44
"./src/index.ts",
55
"./src/tools/browser-utils.ts",
6-
"./src/tools/browser-image-tools.ts"
6+
"./src/tools/browser-image-tools.ts",
7+
"./src/plugins/utils.ts",
8+
"./src/plugins/border.ts"
79
],
810
"out": "docs",
911
"sort": [

0 commit comments

Comments
 (0)