@pjaudiomv/qrcode-svg 1.0.0 → 1.0.2
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/LICENSE +1 -0
- package/README.md +186 -280
- package/dist/qrcode.d.ts +49 -0
- package/dist/qrcode.js +2107 -0
- package/dist/qrcode.js.map +1 -0
- package/package.json +56 -29
- package/src/qrcode.d.ts +49 -0
- package/src/qrcode.js +1679 -0
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/qrcode-svg.iml +0 -9
- package/.idea/vcs.xml +0 -6
- package/app.js +0 -156
- package/bin/qrcode-svg.js +0 -2
- package/dist/qrcode.min.js +0 -2
- package/examples/ascii.js +0 -15
- package/examples/ascii.txt +0 -26
- package/examples/convert.js +0 -25
- package/examples/index.html +0 -12
- package/examples/output/ascii.txt +0 -26
- package/examples/output/convert.pdf +0 -0
- package/examples/output/convert.png +0 -0
- package/examples/output/sample-1.svg +0 -306
- package/examples/output/sample-2.svg +0 -324
- package/examples/output/sample-3.svg +0 -408
- package/examples/samples.js +0 -38
- package/lib/qrcode.js +0 -1672
- package/minify.js +0 -17
- package/pjaudiomv-qrcode-svg-1.0.0.tgz +0 -0
- package/test/test.js +0 -277
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,280 +1,186 @@
|
|
|
1
|
-
## Introduction
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Getting Started
|
|
10
|
-
|
|
11
|
-
Install the package:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### ASCII modules
|
|
190
|
-
|
|
191
|
-
QR Code in ASCII to output in a shell
|
|
192
|
-
```javascript
|
|
193
|
-
var QRCode = require('@lemaik/qrcode-svg');
|
|
194
|
-
|
|
195
|
-
var hello = new QRCode("Hello World!");
|
|
196
|
-
var modules = hello.qrcode.modules;
|
|
197
|
-
|
|
198
|
-
var ascii = '';
|
|
199
|
-
var length = modules.length;
|
|
200
|
-
for (var y = 0; y < length; y++) {
|
|
201
|
-
for (var x = 0; x < length; x++) {
|
|
202
|
-
var module = modules[x][y];
|
|
203
|
-
ascii += (module ? 'x' : ' ');
|
|
204
|
-
}
|
|
205
|
-
ascii += '\r\n';
|
|
206
|
-
}
|
|
207
|
-
console.log(ascii);
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
xxxxxxx xx x x xxxxxxx
|
|
214
|
-
x x xxxx x x x x
|
|
215
|
-
x xxx x xx xx x x xxx x
|
|
216
|
-
x xxx x xx x xxx x
|
|
217
|
-
x xxx x x x x x xxx x
|
|
218
|
-
x x x xx xx x x
|
|
219
|
-
xxxxxxx x x x x x xxxxxxx
|
|
220
|
-
xx xx
|
|
221
|
-
x x xx x x xx x x
|
|
222
|
-
x x xx x xx x xx x
|
|
223
|
-
x x xx x x x xx xx
|
|
224
|
-
x xx xxx xx x x x x x
|
|
225
|
-
xx xxxx xxxx x
|
|
226
|
-
x x x xx x xx xx x xx xx
|
|
227
|
-
x xx xxxx xxxx
|
|
228
|
-
xx xx x x x x xx x
|
|
229
|
-
xxxx xxxx xxxxxx x
|
|
230
|
-
x x x
|
|
231
|
-
xxxxxxx x xxx x x x x
|
|
232
|
-
x x xxx x xx x x
|
|
233
|
-
x xxx x xxxxxxxxxx
|
|
234
|
-
x xxx x xxxxxxxxx x xx
|
|
235
|
-
x xxx x xxx xx x x x
|
|
236
|
-
x x x x x
|
|
237
|
-
xxxxxxx xxx xxx x x x
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
### Web browser
|
|
243
|
-
|
|
244
|
-
Use on a HTML page with JavaScript
|
|
245
|
-
```html
|
|
246
|
-
<!DOCTYPE html>
|
|
247
|
-
<html>
|
|
248
|
-
<body>
|
|
249
|
-
<div id="container"></div>
|
|
250
|
-
<script src="dist/qrcode.min.js"></script>
|
|
251
|
-
<script>
|
|
252
|
-
var qrcode = new QRCode({
|
|
253
|
-
content: "Hello World!",
|
|
254
|
-
container: "svg-viewbox", //Responsive use
|
|
255
|
-
join: true //Crisp rendering and 4-5x reduced file size
|
|
256
|
-
});
|
|
257
|
-
var svg = qrcode.svg();
|
|
258
|
-
document.getElementById("container").innerHTML = svg;
|
|
259
|
-
</script>
|
|
260
|
-
</body>
|
|
261
|
-
</html>
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
## Thanks
|
|
265
|
-
|
|
266
|
-
Thanks to [davidshimjs](https://github.com/davidshimjs/qrcodejs) for the base library.
|
|
267
|
-
|
|
268
|
-
Thanks to [Kazuhiko Arase](http://www.d-project.com/) for the original QR Code in JavaScript algorithm.
|
|
269
|
-
|
|
270
|
-
Thanks to all contributors on the GitHub.
|
|
271
|
-
|
|
272
|
-
## Legal notice
|
|
273
|
-
|
|
274
|
-
```
|
|
275
|
-
Licensed under the MIT license:
|
|
276
|
-
http://www.opensource.org/licenses/mit-license.php
|
|
277
|
-
|
|
278
|
-
The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
|
|
279
|
-
http://www.denso-wave.com/qrcode/faqpatent-e.html
|
|
280
|
-
```
|
|
1
|
+
## Introduction
|
|
2
|
+
|
|
3
|
+
A browser-only ES module for generating SVG QR codes in pure JavaScript.
|
|
4
|
+
|
|
5
|
+
- pure JavaScript, zero dependencies
|
|
6
|
+
- ESM only
|
|
7
|
+
- SVG output
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
Install the package:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @pjaudiomv/qrcode-svg
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Inline example:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import QRCode from '@pjaudiomv/qrcode-svg'
|
|
21
|
+
|
|
22
|
+
const svg = new QRCode('Hello World!').svg()
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
More options:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
const qrcode = new QRCode({
|
|
29
|
+
content: 'http://github.com/',
|
|
30
|
+
padding: 4,
|
|
31
|
+
width: 256,
|
|
32
|
+
height: 256,
|
|
33
|
+
color: '#000000',
|
|
34
|
+
background: '#ffffff',
|
|
35
|
+
ecl: 'M',
|
|
36
|
+
})
|
|
37
|
+
const svg = qrcode.svg()
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Options
|
|
41
|
+
|
|
42
|
+
**List of options:**
|
|
43
|
+
|
|
44
|
+
- **content** - QR Code content, the only **required** parameter
|
|
45
|
+
- **padding** - white space padding, `4` modules by default, `0` for no border
|
|
46
|
+
- **width** - QR Code width in pixels
|
|
47
|
+
- **height** - QR Code height in pixels
|
|
48
|
+
- **color** - color of modules (squares), color name or hex string, e.g. `#000000`
|
|
49
|
+
- **background** - color of background, color name or hex string, e.g. `white`
|
|
50
|
+
- **ecl** - error correction level: `L`, `M`, `H`, `Q`
|
|
51
|
+
- **join** - join modules (squares) into one shape, into the SVG `path` element, **recommended** for web and responsive use, default: `false`
|
|
52
|
+
- **predefined** - to create a squares as pattern, then populate the canvas, default: `false`, see the output examples below
|
|
53
|
+
- **pretty** - apply indents and new lines, default: `true`
|
|
54
|
+
- **swap** - swap X and Y modules, only if you have issues with some QR readers, default: `false`
|
|
55
|
+
- **xmlDeclaration** - prepend XML declaration to the SVG document, i.e. `<?xml version="1.0" standalone="yes"?>`, default: `true`
|
|
56
|
+
- **container** - wrapping element, default: `svg`, see below
|
|
57
|
+
|
|
58
|
+
**Container options:**
|
|
59
|
+
|
|
60
|
+
- **svg** - populate squares in a SVG document with `width` and `height` attributes, recommended for converting to raster images or PDF where QR Code is being static (exact size)
|
|
61
|
+
- **svg-viewbox** - populate squares in a SVG document with `viewBox` attribute, **recommended** for responsive web pages
|
|
62
|
+
- **g** - put squares in `g` element, useful when you need to put multiple QR Codes in a single SVG document
|
|
63
|
+
- **none** - no wrapper
|
|
64
|
+
- **path-data** - returns only the raw SVG path data string (requires `join: true`)
|
|
65
|
+
|
|
66
|
+
## SVG output
|
|
67
|
+
|
|
68
|
+
### Editable squares
|
|
69
|
+
|
|
70
|
+
This mode is useful for designers to manipulate with particular squares.
|
|
71
|
+
Thus, one can open the QR Code in an editor, select particular modules, move around, change color, etc.
|
|
72
|
+
However, some old SVG viewers may generate minor gaps between the squares - the side effect when rendering an image at certain zoom level.
|
|
73
|
+
|
|
74
|
+
Default options
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const qrcode = new QRCode({
|
|
78
|
+
content: 'Pretty Fox',
|
|
79
|
+
join: false,
|
|
80
|
+
predefined: false,
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Output with `rect` elements
|
|
85
|
+
|
|
86
|
+
```xml
|
|
87
|
+
<?xml version="1.0" standalone="yes"?>
|
|
88
|
+
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
|
|
89
|
+
<rect x="0" y="0" width="256" height="256" style="fill:#ffffff;shape-rendering:crispEdges;"/>
|
|
90
|
+
<rect x="16" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
|
|
91
|
+
<rect x="24" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
|
|
92
|
+
<rect x="32" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
|
|
93
|
+
...
|
|
94
|
+
</svg>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Responsive web page
|
|
98
|
+
|
|
99
|
+
Squares joined into one `path` shape produce a compact file size, i.e. 4-5x reduced compared with `rect` elements.
|
|
100
|
+
A single `path` element will result in an optimized rendering, thus not producing any minor gaps between the squares.
|
|
101
|
+
Also using the container with `viewBox` attribute may contribute to the responsive scaling on the web.
|
|
102
|
+
|
|
103
|
+
Set `join` to `true`
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const qrcode = new QRCode({
|
|
107
|
+
content: 'Pretty Fox',
|
|
108
|
+
join: true,
|
|
109
|
+
container: 'svg-viewbox', //Useful but not required
|
|
110
|
+
})
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Output with `path` element
|
|
114
|
+
|
|
115
|
+
```xml
|
|
116
|
+
<?xml version="1.0" standalone="yes"?>
|
|
117
|
+
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 256 256">
|
|
118
|
+
<rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
|
|
119
|
+
<path x="0" y="0" style="fill:blue;shape-rendering:crispEdges;" d="M35.31,35.31 V44.14 H44.14 V35.31 H35.31 Z..." />
|
|
120
|
+
</svg>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Predefined pattern
|
|
124
|
+
|
|
125
|
+
Algorithm defines the square pattern once before populating a canvas. Useful if you want to generate QR Code with candies.
|
|
126
|
+
However, some SVG software and converters do not support `defs` or `use` elements.
|
|
127
|
+
|
|
128
|
+
Set `predefined` to `true`
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
const qrcode = new QRCode({
|
|
132
|
+
content: 'Pretty Fox',
|
|
133
|
+
predefined: true,
|
|
134
|
+
})
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Output with `defs` and `use` elements
|
|
138
|
+
|
|
139
|
+
```xml
|
|
140
|
+
<?xml version="1.0" standalone="yes"?>
|
|
141
|
+
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
|
|
142
|
+
<defs><path id="qrmodule" d="M0 0 h8.827586206896552 v8.827586206896552 H0 z" style="fill:maroon;shape-rendering:crispEdges;" /></defs>
|
|
143
|
+
<rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
|
|
144
|
+
<use x="35.310344827586206" y="35.310344827586206" href="#qrmodule" />
|
|
145
|
+
<use x="44.13793103448276" y="35.310344827586206" href="#qrmodule" />
|
|
146
|
+
...
|
|
147
|
+
</svg>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Usage in the browser
|
|
151
|
+
|
|
152
|
+
```html
|
|
153
|
+
<!DOCTYPE html>
|
|
154
|
+
<html>
|
|
155
|
+
<body>
|
|
156
|
+
<div id="container"></div>
|
|
157
|
+
<script type="module">
|
|
158
|
+
import QRCode from './dist/qrcode.js'
|
|
159
|
+
const qrcode = new QRCode({
|
|
160
|
+
content: 'Hello World!',
|
|
161
|
+
container: 'svg-viewbox',
|
|
162
|
+
join: true,
|
|
163
|
+
})
|
|
164
|
+
document.getElementById('container').innerHTML = qrcode.svg()
|
|
165
|
+
</script>
|
|
166
|
+
</body>
|
|
167
|
+
</html>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Thanks
|
|
171
|
+
|
|
172
|
+
Thanks to [davidshimjs](https://github.com/davidshimjs/qrcodejs) for the base library.
|
|
173
|
+
|
|
174
|
+
Thanks to [Kazuhiko Arase](http://www.d-project.com/) for the original QR Code in JavaScript algorithm.
|
|
175
|
+
|
|
176
|
+
Thanks to all contributors on the GitHub.
|
|
177
|
+
|
|
178
|
+
## Legal notice
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
Licensed under the MIT license:
|
|
182
|
+
http://www.opensource.org/licenses/mit-license.php
|
|
183
|
+
|
|
184
|
+
The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
|
|
185
|
+
http://www.denso-wave.com/qrcode/faqpatent-e.html
|
|
186
|
+
```
|
package/dist/qrcode.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type ErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H'
|
|
2
|
+
|
|
3
|
+
export type Container =
|
|
4
|
+
| 'svg'
|
|
5
|
+
| 'svg-viewbox'
|
|
6
|
+
| 'g'
|
|
7
|
+
| 'none'
|
|
8
|
+
| 'path-data'
|
|
9
|
+
|
|
10
|
+
export interface QRCodeOptions {
|
|
11
|
+
/** QR Code content (required). */
|
|
12
|
+
content: string
|
|
13
|
+
/** White-space padding in modules. Default: `4`. */
|
|
14
|
+
padding?: number
|
|
15
|
+
/** Image width in pixels. Default: `256`. */
|
|
16
|
+
width?: number
|
|
17
|
+
/** Image height in pixels. Default: `256`. */
|
|
18
|
+
height?: number
|
|
19
|
+
/** Foreground color. Default: `'#000000'`. */
|
|
20
|
+
color?: string
|
|
21
|
+
/** Background color. Default: `'#ffffff'`. */
|
|
22
|
+
background?: string
|
|
23
|
+
/** Error correction level. Default: `'M'`. */
|
|
24
|
+
ecl?: ErrorCorrectionLevel
|
|
25
|
+
/** Join modules into a single `<path>` element. Default: `false`. */
|
|
26
|
+
join?: boolean
|
|
27
|
+
/** Use `<defs>`/`<use>` for module shapes. Default: `false`. */
|
|
28
|
+
predefined?: boolean
|
|
29
|
+
/** Pretty-print the SVG output. Default: `true`. */
|
|
30
|
+
pretty?: boolean
|
|
31
|
+
/** Swap X and Y (workaround for some readers). Default: `false`. */
|
|
32
|
+
swap?: boolean
|
|
33
|
+
/** Prepend the `<?xml ... ?>` declaration. Default: `true`. */
|
|
34
|
+
xmlDeclaration?: boolean
|
|
35
|
+
/** Wrapper element. Default: `'svg'`. */
|
|
36
|
+
container?: Container
|
|
37
|
+
/** QR version hint. Default: `4`. */
|
|
38
|
+
typeNumber?: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SvgOptions {
|
|
42
|
+
container?: Container
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default class QRCode {
|
|
46
|
+
constructor(options: QRCodeOptions | string)
|
|
47
|
+
options: Required<Pick<QRCodeOptions, 'padding' | 'width' | 'height' | 'color' | 'background' | 'ecl' | 'typeNumber'>> & QRCodeOptions
|
|
48
|
+
svg(options?: SvgOptions): string
|
|
49
|
+
}
|