@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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SkImage } from '@shopify/react-native-skia';
|
|
2
|
+
export type ErrorCorrection = 'L' | 'M' | 'Q' | 'H';
|
|
3
|
+
export type DotStyle = 'square' | 'rounded' | 'dots';
|
|
4
|
+
export type EyeStyle = 'square' | 'rounded' | 'circle';
|
|
5
|
+
export interface QRGradient {
|
|
6
|
+
colors: string[];
|
|
7
|
+
/** 0..1 fractions of the size. Default top-left to bottom-right. */
|
|
8
|
+
start?: {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
};
|
|
12
|
+
end?: {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface QRLogo {
|
|
18
|
+
image: SkImage;
|
|
19
|
+
/** Fraction of the QR size. Default 0.22. */
|
|
20
|
+
size?: number;
|
|
21
|
+
/** Padding of the clear zone around the logo, px. Default 6. */
|
|
22
|
+
padding?: number;
|
|
23
|
+
/** Corner radius of the clear zone and logo clip, px. Default 8. */
|
|
24
|
+
radius?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface StyledQRProps {
|
|
27
|
+
value: string;
|
|
28
|
+
/** Width and height in px. Default 240. */
|
|
29
|
+
size?: number;
|
|
30
|
+
/** Default 'M'; forced to 'H' when a logo is set. */
|
|
31
|
+
errorCorrection?: ErrorCorrection;
|
|
32
|
+
dotStyle?: DotStyle;
|
|
33
|
+
eyeStyle?: EyeStyle;
|
|
34
|
+
color?: string;
|
|
35
|
+
gradient?: QRGradient;
|
|
36
|
+
/** 'transparent' for no background. Default '#FFFFFF'. */
|
|
37
|
+
backgroundColor?: string;
|
|
38
|
+
/** Corner radius of the background, px. Default 0. */
|
|
39
|
+
backgroundRadius?: number;
|
|
40
|
+
logo?: QRLogo;
|
|
41
|
+
/** Margin in modules. Default 2. */
|
|
42
|
+
quietZone?: number;
|
|
43
|
+
/** Ripple reveal on mount and on value change. Default false. */
|
|
44
|
+
animate?: boolean;
|
|
45
|
+
accessibilityLabel?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface StyledQRHandle {
|
|
48
|
+
/** Base64 PNG of the rendered code at the device pixel ratio, or null if not ready. */
|
|
49
|
+
toPNG: () => string | null;
|
|
50
|
+
/** SVG markup with the same geometry and colours. The logo is not included. */
|
|
51
|
+
toSVG: () => string;
|
|
52
|
+
}
|
|
53
|
+
export interface QRMatrix {
|
|
54
|
+
size: number;
|
|
55
|
+
/** Row-major, true for dark modules. */
|
|
56
|
+
modules: boolean[][];
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAE1D,MAAM,MAAM,eAAe,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AACpD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AACrD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEvD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,oEAAoE;IACpE,KAAK,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACjC,GAAG,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAChC;AAED,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,OAAO,CAAC;IACf,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,2CAA2C;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,UAAU,CAAC;IACtB,0DAA0D;IAC1D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,sDAAsD;IACtD,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,uFAAuF;IACvF,KAAK,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC3B,+EAA+E;IAC/E,KAAK,EAAE,MAAM,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQRMatrix.d.ts","sourceRoot":"","sources":["../../../src/useQRMatrix.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAS,CAAC;AAEzD,6CAA6C;AAC7C,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,eAAe,GAAE,eAAqB,GACrC,QAAQ,CAKV"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,168 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asaddevworld/styled-qr",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Styled QR codes for React Native drawn with Skia: dot and eye styles, gradients, logo overlay, animated reveal, and PNG or SVG export",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"rn-styled-qr-source": "./src/index.tsx",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"cpp",
|
|
21
|
+
"*.podspec",
|
|
22
|
+
"react-native.config.js",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/build",
|
|
25
|
+
"!android/gradle",
|
|
26
|
+
"!android/gradlew",
|
|
27
|
+
"!android/gradlew.bat",
|
|
28
|
+
"!android/local.properties",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/__fixtures__",
|
|
31
|
+
"!**/__mocks__",
|
|
32
|
+
"!**/.*"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"example": "yarn workspace rn-styled-qr-example",
|
|
36
|
+
"clean": "del-cli lib",
|
|
37
|
+
"prepare": "bob build",
|
|
38
|
+
"typecheck": "tsc",
|
|
39
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
40
|
+
"test": "jest"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"react-native",
|
|
44
|
+
"expo",
|
|
45
|
+
"qr",
|
|
46
|
+
"qrcode",
|
|
47
|
+
"qr-code",
|
|
48
|
+
"skia",
|
|
49
|
+
"styled",
|
|
50
|
+
"gradient",
|
|
51
|
+
"logo",
|
|
52
|
+
"svg",
|
|
53
|
+
"ios",
|
|
54
|
+
"android"
|
|
55
|
+
],
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/asadDevWorld/rn-styled-qr.git"
|
|
59
|
+
},
|
|
60
|
+
"author": "Asad Ali <sadali3690@gmail.com> (https://github.com/asadDevWorld)",
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"bugs": {
|
|
63
|
+
"url": "https://github.com/asadDevWorld/rn-styled-qr/issues"
|
|
64
|
+
},
|
|
65
|
+
"homepage": "https://github.com/asadDevWorld/rn-styled-qr#readme",
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"registry": "https://registry.npmjs.org/",
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@eslint/compat": "^2.1.1",
|
|
72
|
+
"@eslint/eslintrc": "^3.3.7",
|
|
73
|
+
"@eslint/js": "^9.39.5",
|
|
74
|
+
"@jest/globals": "^29.7.0",
|
|
75
|
+
"@react-native/babel-preset": "0.88.1",
|
|
76
|
+
"@react-native/eslint-config": "0.88.1",
|
|
77
|
+
"@react-native/jest-preset": "0.88.1",
|
|
78
|
+
"@shopify/react-native-skia": "2.14.0",
|
|
79
|
+
"@testing-library/react-native": "^13.3.3",
|
|
80
|
+
"@types/qrcode": "^1.5.6",
|
|
81
|
+
"@types/react": "^19.2.0",
|
|
82
|
+
"del-cli": "^8.0.1",
|
|
83
|
+
"eslint": "^9.39.5",
|
|
84
|
+
"eslint-config-prettier": "^10.1.8",
|
|
85
|
+
"eslint-plugin-ft-flow": "^3.0.11",
|
|
86
|
+
"eslint-plugin-prettier": "^5.5.7",
|
|
87
|
+
"jest": "^29.7.0",
|
|
88
|
+
"prettier": "^3.9.10",
|
|
89
|
+
"react": "19.3.0",
|
|
90
|
+
"react-native": "0.88.1",
|
|
91
|
+
"react-native-builder-bob": "^0.44.1",
|
|
92
|
+
"react-native-reanimated": "4.7.1",
|
|
93
|
+
"react-native-safe-area-context": "~5.10.1",
|
|
94
|
+
"react-native-worklets": "0.13.0",
|
|
95
|
+
"react-test-renderer": "19.3.0",
|
|
96
|
+
"turbo": "^2.11.10",
|
|
97
|
+
"typescript": "^6.0.3"
|
|
98
|
+
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"@shopify/react-native-skia": ">=2.0.0",
|
|
101
|
+
"react": "*",
|
|
102
|
+
"react-native": "*",
|
|
103
|
+
"react-native-reanimated": ">=4.0.0",
|
|
104
|
+
"react-native-worklets": "*"
|
|
105
|
+
},
|
|
106
|
+
"workspaces": [
|
|
107
|
+
"example"
|
|
108
|
+
],
|
|
109
|
+
"packageManager": "yarn@4.18.1",
|
|
110
|
+
"react-native-builder-bob": {
|
|
111
|
+
"source": "src",
|
|
112
|
+
"output": "lib",
|
|
113
|
+
"targets": [
|
|
114
|
+
[
|
|
115
|
+
"module",
|
|
116
|
+
{
|
|
117
|
+
"esm": true
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
[
|
|
121
|
+
"typescript",
|
|
122
|
+
{
|
|
123
|
+
"project": "tsconfig.build.json"
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
]
|
|
127
|
+
},
|
|
128
|
+
"prettier": {
|
|
129
|
+
"quoteProps": "consistent",
|
|
130
|
+
"singleQuote": true,
|
|
131
|
+
"tabWidth": 2,
|
|
132
|
+
"trailingComma": "es5",
|
|
133
|
+
"useTabs": false
|
|
134
|
+
},
|
|
135
|
+
"jest": {
|
|
136
|
+
"preset": "@react-native/jest-preset",
|
|
137
|
+
"testEnvironmentOptions": {
|
|
138
|
+
"customExportConditions": [
|
|
139
|
+
"require",
|
|
140
|
+
"react-native",
|
|
141
|
+
"rn-styled-qr-source"
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
"modulePathIgnorePatterns": [
|
|
145
|
+
"<rootDir>/example/node_modules",
|
|
146
|
+
"<rootDir>/lib/"
|
|
147
|
+
],
|
|
148
|
+
"setupFiles": [
|
|
149
|
+
"<rootDir>/jest.setup.js"
|
|
150
|
+
],
|
|
151
|
+
"resolver": "react-native-reanimated/jest/resolver",
|
|
152
|
+
"transformIgnorePatterns": [
|
|
153
|
+
"node_modules/(?!((jest-)?react-native|@react-native(-community)?|react-native-reanimated|react-native-worklets|react-native-safe-area-context|@shopify/react-native-skia)/)"
|
|
154
|
+
]
|
|
155
|
+
},
|
|
156
|
+
"create-react-native-library": {
|
|
157
|
+
"type": "library",
|
|
158
|
+
"languages": "js",
|
|
159
|
+
"tools": [
|
|
160
|
+
"eslint",
|
|
161
|
+
"jest"
|
|
162
|
+
],
|
|
163
|
+
"version": "0.64.0"
|
|
164
|
+
},
|
|
165
|
+
"dependencies": {
|
|
166
|
+
"qrcode": "^1.5.4"
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/StyledQR.tsx
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { forwardRef, useEffect, useImperativeHandle, useMemo } from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
Canvas,
|
|
5
|
+
Group,
|
|
6
|
+
Image,
|
|
7
|
+
LinearGradient,
|
|
8
|
+
Path,
|
|
9
|
+
RoundedRect,
|
|
10
|
+
Skia,
|
|
11
|
+
useCanvasRef,
|
|
12
|
+
vec,
|
|
13
|
+
type SkPath,
|
|
14
|
+
} from '@shopify/react-native-skia';
|
|
15
|
+
import {
|
|
16
|
+
Easing,
|
|
17
|
+
ReduceMotion,
|
|
18
|
+
useDerivedValue,
|
|
19
|
+
useSharedValue,
|
|
20
|
+
withTiming,
|
|
21
|
+
} from 'react-native-reanimated';
|
|
22
|
+
import { createMatrix, geometry, isEye } from './matrix';
|
|
23
|
+
import { qrToSVG } from './svg';
|
|
24
|
+
import type { StyledQRHandle, StyledQRProps } from './types';
|
|
25
|
+
|
|
26
|
+
interface Scene {
|
|
27
|
+
data: SkPath;
|
|
28
|
+
eyes: SkPath;
|
|
29
|
+
logoRect: { x: number; y: number; width: number; height: number } | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function buildScene(
|
|
33
|
+
props: Required<
|
|
34
|
+
Pick<
|
|
35
|
+
StyledQRProps,
|
|
36
|
+
| 'value'
|
|
37
|
+
| 'size'
|
|
38
|
+
| 'errorCorrection'
|
|
39
|
+
| 'dotStyle'
|
|
40
|
+
| 'eyeStyle'
|
|
41
|
+
| 'quietZone'
|
|
42
|
+
>
|
|
43
|
+
> & { logo?: StyledQRProps['logo'] }
|
|
44
|
+
): Scene {
|
|
45
|
+
const { value, size, errorCorrection, dotStyle, eyeStyle, quietZone, logo } =
|
|
46
|
+
props;
|
|
47
|
+
const matrix = createMatrix(value, logo ? 'H' : errorCorrection);
|
|
48
|
+
const { cell, offset } = geometry(matrix, size, quietZone);
|
|
49
|
+
|
|
50
|
+
const logoSize = logo ? size * (logo.size ?? 0.22) : 0;
|
|
51
|
+
const pad = logo ? (logo.padding ?? 6) : 0;
|
|
52
|
+
const logoRect = logo
|
|
53
|
+
? {
|
|
54
|
+
x: (size - logoSize) / 2 - pad,
|
|
55
|
+
y: (size - logoSize) / 2 - pad,
|
|
56
|
+
width: logoSize + pad * 2,
|
|
57
|
+
height: logoSize + pad * 2,
|
|
58
|
+
}
|
|
59
|
+
: null;
|
|
60
|
+
|
|
61
|
+
const data = Skia.PathBuilder.Make();
|
|
62
|
+
for (let r = 0; r < matrix.size; r++) {
|
|
63
|
+
for (let c = 0; c < matrix.size; c++) {
|
|
64
|
+
if (!matrix.modules[r]?.[c] || isEye(r, c, matrix.size)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const x = offset + c * cell;
|
|
68
|
+
const y = offset + r * cell;
|
|
69
|
+
if (logoRect) {
|
|
70
|
+
const cx = x + cell / 2;
|
|
71
|
+
const cy = y + cell / 2;
|
|
72
|
+
if (
|
|
73
|
+
cx > logoRect.x &&
|
|
74
|
+
cx < logoRect.x + logoRect.width &&
|
|
75
|
+
cy > logoRect.y &&
|
|
76
|
+
cy < logoRect.y + logoRect.height
|
|
77
|
+
) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (dotStyle === 'dots') {
|
|
82
|
+
data.addCircle(x + cell / 2, y + cell / 2, cell * 0.4);
|
|
83
|
+
} else if (dotStyle === 'rounded') {
|
|
84
|
+
data.addRRect(
|
|
85
|
+
Skia.RRectXY(
|
|
86
|
+
{
|
|
87
|
+
x: x + cell * 0.05,
|
|
88
|
+
y: y + cell * 0.05,
|
|
89
|
+
width: cell * 0.9,
|
|
90
|
+
height: cell * 0.9,
|
|
91
|
+
},
|
|
92
|
+
cell * 0.3,
|
|
93
|
+
cell * 0.3
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
} else {
|
|
97
|
+
data.addRect({ x, y, width: cell, height: cell });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const eyes = Skia.PathBuilder.Make();
|
|
103
|
+
const corners: [number, number][] = [
|
|
104
|
+
[0, 0],
|
|
105
|
+
[0, matrix.size - 7],
|
|
106
|
+
[matrix.size - 7, 0],
|
|
107
|
+
];
|
|
108
|
+
for (const [r, c] of corners) {
|
|
109
|
+
const x = offset + c * cell;
|
|
110
|
+
const y = offset + r * cell;
|
|
111
|
+
const outer = cell * 7;
|
|
112
|
+
const ring = cell * 5;
|
|
113
|
+
const inner = cell * 3;
|
|
114
|
+
if (eyeStyle === 'circle') {
|
|
115
|
+
eyes.addCircle(x + outer / 2, y + outer / 2, outer / 2);
|
|
116
|
+
eyes.addCircle(x + outer / 2, y + outer / 2, ring / 2);
|
|
117
|
+
eyes.addCircle(x + outer / 2, y + outer / 2, inner / 2);
|
|
118
|
+
} else {
|
|
119
|
+
const radius = eyeStyle === 'rounded' ? cell * 2 : 0;
|
|
120
|
+
const ringRadius = eyeStyle === 'rounded' ? cell * 1.2 : 0;
|
|
121
|
+
const innerRadius = eyeStyle === 'rounded' ? cell * 0.8 : 0;
|
|
122
|
+
eyes.addRRect(
|
|
123
|
+
Skia.RRectXY({ x, y, width: outer, height: outer }, radius, radius)
|
|
124
|
+
);
|
|
125
|
+
eyes.addRRect(
|
|
126
|
+
Skia.RRectXY(
|
|
127
|
+
{ x: x + cell, y: y + cell, width: ring, height: ring },
|
|
128
|
+
ringRadius,
|
|
129
|
+
ringRadius
|
|
130
|
+
)
|
|
131
|
+
);
|
|
132
|
+
eyes.addRRect(
|
|
133
|
+
Skia.RRectXY(
|
|
134
|
+
{ x: x + cell * 2, y: y + cell * 2, width: inner, height: inner },
|
|
135
|
+
innerRadius,
|
|
136
|
+
innerRadius
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// The eyes path is drawn even-odd: outer square minus the ring, plus the inner square.
|
|
142
|
+
return { data: data.build(), eyes: eyes.build(), logoRect };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A QR code drawn with Skia, styled, with an optional logo and an animated reveal. */
|
|
146
|
+
export const StyledQR = forwardRef<StyledQRHandle, StyledQRProps>(
|
|
147
|
+
function StyledQR(
|
|
148
|
+
{
|
|
149
|
+
value,
|
|
150
|
+
size = 240,
|
|
151
|
+
errorCorrection = 'M',
|
|
152
|
+
dotStyle = 'square',
|
|
153
|
+
eyeStyle = 'square',
|
|
154
|
+
color = '#000000',
|
|
155
|
+
gradient,
|
|
156
|
+
backgroundColor = '#FFFFFF',
|
|
157
|
+
backgroundRadius = 0,
|
|
158
|
+
logo,
|
|
159
|
+
quietZone = 2,
|
|
160
|
+
animate = false,
|
|
161
|
+
accessibilityLabel,
|
|
162
|
+
},
|
|
163
|
+
ref
|
|
164
|
+
) {
|
|
165
|
+
const canvasRef = useCanvasRef();
|
|
166
|
+
const scene = useMemo(
|
|
167
|
+
() =>
|
|
168
|
+
buildScene({
|
|
169
|
+
value,
|
|
170
|
+
size,
|
|
171
|
+
errorCorrection,
|
|
172
|
+
dotStyle,
|
|
173
|
+
eyeStyle,
|
|
174
|
+
quietZone,
|
|
175
|
+
logo,
|
|
176
|
+
}),
|
|
177
|
+
[value, size, errorCorrection, dotStyle, eyeStyle, quietZone, logo]
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const reveal = useSharedValue(animate ? 0 : 1);
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (!animate) {
|
|
183
|
+
reveal.value = 1;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
reveal.value = 0;
|
|
187
|
+
reveal.value = withTiming(1, {
|
|
188
|
+
duration: 650,
|
|
189
|
+
easing: Easing.out(Easing.cubic),
|
|
190
|
+
reduceMotion: ReduceMotion.System,
|
|
191
|
+
});
|
|
192
|
+
}, [animate, value, dotStyle, eyeStyle, reveal]);
|
|
193
|
+
|
|
194
|
+
const clip = useDerivedValue(() => {
|
|
195
|
+
const radius = (reveal.value * size * Math.SQRT2) / 2 + 1;
|
|
196
|
+
return Skia.PathBuilder.Make()
|
|
197
|
+
.addCircle(size / 2, size / 2, radius)
|
|
198
|
+
.build();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
useImperativeHandle(
|
|
202
|
+
ref,
|
|
203
|
+
() => ({
|
|
204
|
+
toPNG: () =>
|
|
205
|
+
canvasRef.current?.makeImageSnapshot()?.encodeToBase64() ?? null,
|
|
206
|
+
toSVG: () =>
|
|
207
|
+
qrToSVG(createMatrix(value, logo ? 'H' : errorCorrection), {
|
|
208
|
+
size,
|
|
209
|
+
quietZone,
|
|
210
|
+
dotStyle,
|
|
211
|
+
eyeStyle,
|
|
212
|
+
color,
|
|
213
|
+
gradient,
|
|
214
|
+
backgroundColor,
|
|
215
|
+
backgroundRadius,
|
|
216
|
+
logoHole: logo
|
|
217
|
+
? (logo.size ?? 0.22) + ((logo.padding ?? 6) * 2) / size
|
|
218
|
+
: 0,
|
|
219
|
+
}),
|
|
220
|
+
}),
|
|
221
|
+
[
|
|
222
|
+
canvasRef,
|
|
223
|
+
value,
|
|
224
|
+
logo,
|
|
225
|
+
errorCorrection,
|
|
226
|
+
size,
|
|
227
|
+
quietZone,
|
|
228
|
+
dotStyle,
|
|
229
|
+
eyeStyle,
|
|
230
|
+
color,
|
|
231
|
+
gradient,
|
|
232
|
+
backgroundColor,
|
|
233
|
+
backgroundRadius,
|
|
234
|
+
]
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
const start = gradient?.start ?? { x: 0, y: 0 };
|
|
238
|
+
const end = gradient?.end ?? { x: 1, y: 1 };
|
|
239
|
+
const paint = gradient ? (
|
|
240
|
+
<LinearGradient
|
|
241
|
+
start={vec(start.x * size, start.y * size)}
|
|
242
|
+
end={vec(end.x * size, end.y * size)}
|
|
243
|
+
colors={gradient.colors}
|
|
244
|
+
/>
|
|
245
|
+
) : null;
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<View
|
|
249
|
+
style={{ width: size, height: size }}
|
|
250
|
+
accessible
|
|
251
|
+
accessibilityRole="image"
|
|
252
|
+
accessibilityLabel={accessibilityLabel ?? `QR code for ${value}`}
|
|
253
|
+
>
|
|
254
|
+
<Canvas ref={canvasRef} style={{ width: size, height: size }}>
|
|
255
|
+
{backgroundColor !== 'transparent' ? (
|
|
256
|
+
<RoundedRect
|
|
257
|
+
x={0}
|
|
258
|
+
y={0}
|
|
259
|
+
width={size}
|
|
260
|
+
height={size}
|
|
261
|
+
r={backgroundRadius}
|
|
262
|
+
color={backgroundColor}
|
|
263
|
+
/>
|
|
264
|
+
) : null}
|
|
265
|
+
<Group clip={clip}>
|
|
266
|
+
<Path path={scene.data} color={color}>
|
|
267
|
+
{paint}
|
|
268
|
+
</Path>
|
|
269
|
+
<Path path={scene.eyes} color={color} fillType="evenOdd">
|
|
270
|
+
{paint}
|
|
271
|
+
</Path>
|
|
272
|
+
</Group>
|
|
273
|
+
{logo && scene.logoRect ? (
|
|
274
|
+
<Group>
|
|
275
|
+
<RoundedRect
|
|
276
|
+
x={scene.logoRect.x}
|
|
277
|
+
y={scene.logoRect.y}
|
|
278
|
+
width={scene.logoRect.width}
|
|
279
|
+
height={scene.logoRect.height}
|
|
280
|
+
r={logo.radius ?? 8}
|
|
281
|
+
color={
|
|
282
|
+
backgroundColor === 'transparent'
|
|
283
|
+
? '#FFFFFF'
|
|
284
|
+
: backgroundColor
|
|
285
|
+
}
|
|
286
|
+
/>
|
|
287
|
+
<Image
|
|
288
|
+
image={logo.image}
|
|
289
|
+
x={scene.logoRect.x + (logo.padding ?? 6)}
|
|
290
|
+
y={scene.logoRect.y + (logo.padding ?? 6)}
|
|
291
|
+
width={scene.logoRect.width - (logo.padding ?? 6) * 2}
|
|
292
|
+
height={scene.logoRect.height - (logo.padding ?? 6) * 2}
|
|
293
|
+
fit="contain"
|
|
294
|
+
/>
|
|
295
|
+
</Group>
|
|
296
|
+
) : null}
|
|
297
|
+
</Canvas>
|
|
298
|
+
</View>
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
);
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { StyledQR } from './StyledQR';
|
|
2
|
+
export { useQRMatrix } from './useQRMatrix';
|
|
3
|
+
export { createMatrix, isEye, geometry } from './matrix';
|
|
4
|
+
export { qrToSVG, type SVGOptions } from './svg';
|
|
5
|
+
export type {
|
|
6
|
+
DotStyle,
|
|
7
|
+
ErrorCorrection,
|
|
8
|
+
EyeStyle,
|
|
9
|
+
QRGradient,
|
|
10
|
+
QRLogo,
|
|
11
|
+
QRMatrix,
|
|
12
|
+
StyledQRHandle,
|
|
13
|
+
StyledQRProps,
|
|
14
|
+
} from './types';
|
package/src/matrix.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import QRCode from 'qrcode';
|
|
2
|
+
import type { ErrorCorrection, QRMatrix } from './types';
|
|
3
|
+
|
|
4
|
+
/** Encode text into a module grid. */
|
|
5
|
+
export function createMatrix(
|
|
6
|
+
value: string,
|
|
7
|
+
errorCorrection: ErrorCorrection = 'M'
|
|
8
|
+
): QRMatrix {
|
|
9
|
+
const code = QRCode.create(value || ' ', {
|
|
10
|
+
errorCorrectionLevel: errorCorrection,
|
|
11
|
+
});
|
|
12
|
+
const size = code.modules.size;
|
|
13
|
+
const data = code.modules.data;
|
|
14
|
+
const modules: boolean[][] = [];
|
|
15
|
+
for (let r = 0; r < size; r++) {
|
|
16
|
+
const row: boolean[] = [];
|
|
17
|
+
for (let c = 0; c < size; c++) {
|
|
18
|
+
row.push(data[r * size + c] === 1);
|
|
19
|
+
}
|
|
20
|
+
modules.push(row);
|
|
21
|
+
}
|
|
22
|
+
return { size, modules };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** True when (row, col) is inside one of the three finder patterns. */
|
|
26
|
+
export function isEye(row: number, col: number, size: number): boolean {
|
|
27
|
+
const inTopLeft = row < 7 && col < 7;
|
|
28
|
+
const inTopRight = row < 7 && col >= size - 7;
|
|
29
|
+
const inBottomLeft = row >= size - 7 && col < 7;
|
|
30
|
+
return inTopLeft || inTopRight || inBottomLeft;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Geometry {
|
|
34
|
+
/** Pixel size of one module. */
|
|
35
|
+
cell: number;
|
|
36
|
+
/** Offset of the first module from the edge, px. */
|
|
37
|
+
offset: number;
|
|
38
|
+
size: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function geometry(
|
|
42
|
+
matrix: QRMatrix,
|
|
43
|
+
pixelSize: number,
|
|
44
|
+
quietZone: number
|
|
45
|
+
): Geometry {
|
|
46
|
+
const total = matrix.size + quietZone * 2;
|
|
47
|
+
const cell = pixelSize / total;
|
|
48
|
+
return { cell, offset: quietZone * cell, size: matrix.size };
|
|
49
|
+
}
|