@asaddevworld/styled-qr 0.0.0-stage → 0.1.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/LICENSE +20 -0
- package/README.md +102 -2
- package/lib/module/StyledQR.js +234 -0
- package/lib/module/StyledQR.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/matrix.js +41 -0
- package/lib/module/matrix.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/svg.js +87 -0
- package/lib/module/svg.js.map +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useQRMatrix.js +9 -0
- package/lib/module/useQRMatrix.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/StyledQR.d.ts +4 -0
- package/lib/typescript/src/StyledQR.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/matrix.d.ts +14 -0
- package/lib/typescript/src/matrix.d.ts.map +1 -0
- package/lib/typescript/src/svg.d.ts +16 -0
- package/lib/typescript/src/svg.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +58 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/useQRMatrix.d.ts +4 -0
- package/lib/typescript/src/useQRMatrix.d.ts.map +1 -0
- package/package.json +166 -4
- package/src/StyledQR.tsx +301 -0
- package/src/index.tsx +14 -0
- package/src/matrix.ts +49 -0
- package/src/svg.ts +125 -0
- package/src/types.ts +57 -0
- package/src/useQRMatrix.ts +14 -0
package/src/svg.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { geometry, isEye } from './matrix';
|
|
2
|
+
import type { DotStyle, EyeStyle, QRGradient, QRMatrix } from './types';
|
|
3
|
+
|
|
4
|
+
export interface SVGOptions {
|
|
5
|
+
size: number;
|
|
6
|
+
quietZone?: number;
|
|
7
|
+
dotStyle?: DotStyle;
|
|
8
|
+
eyeStyle?: EyeStyle;
|
|
9
|
+
color?: string;
|
|
10
|
+
gradient?: QRGradient;
|
|
11
|
+
backgroundColor?: string;
|
|
12
|
+
backgroundRadius?: number;
|
|
13
|
+
/** A square hole in the middle, as a fraction of the size, left blank for a logo. */
|
|
14
|
+
logoHole?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function fmt(n: number): string {
|
|
18
|
+
return Number(n.toFixed(2)).toString();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Build SVG markup for a matrix with the same styling as the component. */
|
|
22
|
+
export function qrToSVG(matrix: QRMatrix, options: SVGOptions): string {
|
|
23
|
+
const {
|
|
24
|
+
size,
|
|
25
|
+
quietZone = 2,
|
|
26
|
+
dotStyle = 'square',
|
|
27
|
+
eyeStyle = 'square',
|
|
28
|
+
color = '#000000',
|
|
29
|
+
gradient,
|
|
30
|
+
backgroundColor = '#FFFFFF',
|
|
31
|
+
backgroundRadius = 0,
|
|
32
|
+
logoHole = 0,
|
|
33
|
+
} = options;
|
|
34
|
+
const { cell, offset } = geometry(matrix, size, quietZone);
|
|
35
|
+
const parts: string[] = [];
|
|
36
|
+
const fill = gradient ? 'url(#qrg)' : color;
|
|
37
|
+
|
|
38
|
+
if (gradient) {
|
|
39
|
+
const s = gradient.start ?? { x: 0, y: 0 };
|
|
40
|
+
const e = gradient.end ?? { x: 1, y: 1 };
|
|
41
|
+
const stops = gradient.colors
|
|
42
|
+
.map(
|
|
43
|
+
(c, i) =>
|
|
44
|
+
`<stop offset="${fmt((i / Math.max(1, gradient.colors.length - 1)) * 100)}%" stop-color="${c}"/>`
|
|
45
|
+
)
|
|
46
|
+
.join('');
|
|
47
|
+
parts.push(
|
|
48
|
+
`<defs><linearGradient id="qrg" x1="${s.x}" y1="${s.y}" x2="${e.x}" y2="${e.y}">${stops}</linearGradient></defs>`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (backgroundColor !== 'transparent') {
|
|
52
|
+
parts.push(
|
|
53
|
+
`<rect width="${size}" height="${size}" rx="${backgroundRadius}" fill="${backgroundColor}"/>`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const holeHalf = (logoHole * size) / 2;
|
|
58
|
+
const centre = size / 2;
|
|
59
|
+
const dots: string[] = [];
|
|
60
|
+
for (let r = 0; r < matrix.size; r++) {
|
|
61
|
+
for (let c = 0; c < matrix.size; c++) {
|
|
62
|
+
if (!matrix.modules[r]?.[c] || isEye(r, c, matrix.size)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const x = offset + c * cell;
|
|
66
|
+
const y = offset + r * cell;
|
|
67
|
+
if (
|
|
68
|
+
logoHole > 0 &&
|
|
69
|
+
Math.abs(x + cell / 2 - centre) < holeHalf &&
|
|
70
|
+
Math.abs(y + cell / 2 - centre) < holeHalf
|
|
71
|
+
) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (dotStyle === 'dots') {
|
|
75
|
+
dots.push(
|
|
76
|
+
`<circle cx="${fmt(x + cell / 2)}" cy="${fmt(y + cell / 2)}" r="${fmt(cell * 0.4)}"/>`
|
|
77
|
+
);
|
|
78
|
+
} else if (dotStyle === 'rounded') {
|
|
79
|
+
dots.push(
|
|
80
|
+
`<rect x="${fmt(x + cell * 0.05)}" y="${fmt(y + cell * 0.05)}" width="${fmt(cell * 0.9)}" height="${fmt(cell * 0.9)}" rx="${fmt(cell * 0.3)}"/>`
|
|
81
|
+
);
|
|
82
|
+
} else {
|
|
83
|
+
dots.push(
|
|
84
|
+
`<rect x="${fmt(x)}" y="${fmt(y)}" width="${fmt(cell)}" height="${fmt(cell)}"/>`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
parts.push(`<g fill="${fill}">${dots.join('')}</g>`);
|
|
90
|
+
|
|
91
|
+
const eyes: [number, number][] = [
|
|
92
|
+
[0, 0],
|
|
93
|
+
[0, matrix.size - 7],
|
|
94
|
+
[matrix.size - 7, 0],
|
|
95
|
+
];
|
|
96
|
+
const eyeParts: string[] = [];
|
|
97
|
+
for (const [r, c] of eyes) {
|
|
98
|
+
const x = offset + c * cell;
|
|
99
|
+
const y = offset + r * cell;
|
|
100
|
+
const outer = cell * 7;
|
|
101
|
+
const inner = cell * 3;
|
|
102
|
+
const ix = x + cell * 2;
|
|
103
|
+
const iy = y + cell * 2;
|
|
104
|
+
if (eyeStyle === 'circle') {
|
|
105
|
+
eyeParts.push(
|
|
106
|
+
`<circle cx="${fmt(x + outer / 2)}" cy="${fmt(y + outer / 2)}" r="${fmt(outer / 2 - cell / 2)}" fill="none" stroke="${fill}" stroke-width="${fmt(cell)}"/>`
|
|
107
|
+
);
|
|
108
|
+
eyeParts.push(
|
|
109
|
+
`<circle cx="${fmt(ix + inner / 2)}" cy="${fmt(iy + inner / 2)}" r="${fmt(inner / 2)}" fill="${fill}"/>`
|
|
110
|
+
);
|
|
111
|
+
} else {
|
|
112
|
+
const rx = eyeStyle === 'rounded' ? fmt(cell * 1.6) : '0';
|
|
113
|
+
const irx = eyeStyle === 'rounded' ? fmt(cell * 0.8) : '0';
|
|
114
|
+
eyeParts.push(
|
|
115
|
+
`<rect x="${fmt(x + cell / 2)}" y="${fmt(y + cell / 2)}" width="${fmt(outer - cell)}" height="${fmt(outer - cell)}" rx="${rx}" fill="none" stroke="${fill}" stroke-width="${fmt(cell)}"/>`
|
|
116
|
+
);
|
|
117
|
+
eyeParts.push(
|
|
118
|
+
`<rect x="${fmt(ix)}" y="${fmt(iy)}" width="${fmt(inner)}" height="${fmt(inner)}" rx="${irx}" fill="${fill}"/>`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
parts.push(eyeParts.join(''));
|
|
123
|
+
|
|
124
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}">${parts.join('')}</svg>`;
|
|
125
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { SkImage } from '@shopify/react-native-skia';
|
|
2
|
+
|
|
3
|
+
export type ErrorCorrection = 'L' | 'M' | 'Q' | 'H';
|
|
4
|
+
export type DotStyle = 'square' | 'rounded' | 'dots';
|
|
5
|
+
export type EyeStyle = 'square' | 'rounded' | 'circle';
|
|
6
|
+
|
|
7
|
+
export interface QRGradient {
|
|
8
|
+
colors: string[];
|
|
9
|
+
/** 0..1 fractions of the size. Default top-left to bottom-right. */
|
|
10
|
+
start?: { x: number; y: number };
|
|
11
|
+
end?: { x: number; y: number };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface QRLogo {
|
|
15
|
+
image: SkImage;
|
|
16
|
+
/** Fraction of the QR size. Default 0.22. */
|
|
17
|
+
size?: number;
|
|
18
|
+
/** Padding of the clear zone around the logo, px. Default 6. */
|
|
19
|
+
padding?: number;
|
|
20
|
+
/** Corner radius of the clear zone and logo clip, px. Default 8. */
|
|
21
|
+
radius?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StyledQRProps {
|
|
25
|
+
value: string;
|
|
26
|
+
/** Width and height in px. Default 240. */
|
|
27
|
+
size?: number;
|
|
28
|
+
/** Default 'M'; forced to 'H' when a logo is set. */
|
|
29
|
+
errorCorrection?: ErrorCorrection;
|
|
30
|
+
dotStyle?: DotStyle;
|
|
31
|
+
eyeStyle?: EyeStyle;
|
|
32
|
+
color?: string;
|
|
33
|
+
gradient?: QRGradient;
|
|
34
|
+
/** 'transparent' for no background. Default '#FFFFFF'. */
|
|
35
|
+
backgroundColor?: string;
|
|
36
|
+
/** Corner radius of the background, px. Default 0. */
|
|
37
|
+
backgroundRadius?: number;
|
|
38
|
+
logo?: QRLogo;
|
|
39
|
+
/** Margin in modules. Default 2. */
|
|
40
|
+
quietZone?: number;
|
|
41
|
+
/** Ripple reveal on mount and on value change. Default false. */
|
|
42
|
+
animate?: boolean;
|
|
43
|
+
accessibilityLabel?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface StyledQRHandle {
|
|
47
|
+
/** Base64 PNG of the rendered code at the device pixel ratio, or null if not ready. */
|
|
48
|
+
toPNG: () => string | null;
|
|
49
|
+
/** SVG markup with the same geometry and colours. The logo is not included. */
|
|
50
|
+
toSVG: () => string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface QRMatrix {
|
|
54
|
+
size: number;
|
|
55
|
+
/** Row-major, true for dark modules. */
|
|
56
|
+
modules: boolean[][];
|
|
57
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { createMatrix } from './matrix';
|
|
3
|
+
import type { ErrorCorrection, QRMatrix } from './types';
|
|
4
|
+
|
|
5
|
+
/** The module grid for a value, memoised. */
|
|
6
|
+
export function useQRMatrix(
|
|
7
|
+
value: string,
|
|
8
|
+
errorCorrection: ErrorCorrection = 'M'
|
|
9
|
+
): QRMatrix {
|
|
10
|
+
return useMemo(
|
|
11
|
+
() => createMatrix(value, errorCorrection),
|
|
12
|
+
[value, errorCorrection]
|
|
13
|
+
);
|
|
14
|
+
}
|