Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: circle mode for QR code styling #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ size | 100 | Size of rendered image in pixels
value | 'this is a QR code' | String Value of the QR code. Can also accept an array of segments as defined in [Manual mode](https://github.com/soldair/node-qrcode#manual-mode). Ex. [{ data: 'ABCDEFG', mode: 'alphanumeric' }, { data: '0123456', mode: 'numeric' }, { data: [253,254,255], mode: 'byte' }]
color | 'black' | Color of the QR code
backgroundColor | 'white' | Color of the background
mode | 'default' | Style mode for QR. Valid values are "default" or "circle"
enableLinearGradient | false | enables or disables linear gradient
linearGradient | ['rgb(255,0,0)','rgb(0,255,255)'] | array of 2 rgb colors used to create the linear gradient
gradientDirection| [170,0,0,0] | the direction of the linear gradient
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface QRCodeProps {
ecl?: "L" | "M" | "Q" | "H";
/* error handler called when matrix fails to generate */
onError?: Function;
/* mode of qr code default with draw path and circle will make circles. Defaults to normal*/
mode?: "default" | "circle";
}

export default QRCode;
6 changes: 3 additions & 3 deletions src/__tests__/__snapshots__/QRCode-test.js.snap

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Svg, {
} from 'react-native-svg'
import genMatrix from './genMatrix'
import transformMatrixIntoPath from './transformMatrixIntoPath'
import transformMatrixIntoCirclePath from './transformMatrixIntoCirclePath'

const renderLogo = ({
size,
Expand Down Expand Up @@ -81,12 +82,17 @@ const QRCode = ({
gradientDirection = ['0%', '0%', '100%', '100%'],
linearGradient = ['rgb(255,0,0)', 'rgb(0,255,255)'],
ecl = 'M',
mode = 'default',
getRef,
onError
}) => {
const result = useMemo(() => {
try {
return transformMatrixIntoPath(genMatrix(value, ecl), size)
if (mode === 'default') {
return transformMatrixIntoPath(genMatrix(value, ecl), size)
} else {
return transformMatrixIntoCirclePath(genMatrix(value, ecl), size)
}
} catch (error) {
if (onError && typeof onError === 'function') {
onError(error)
Expand Down Expand Up @@ -139,9 +145,10 @@ const QRCode = ({
<G>
<Path
d={path}
fill={enableLinearGradient ? 'url(#grad)' : color}
strokeLinecap='butt'
stroke={enableLinearGradient ? 'url(#grad)' : color}
strokeWidth={cellSize}
strokeWidth={mode === 'default' ? cellSize : 0}
/>
</G>
{logo &&
Expand Down
21 changes: 21 additions & 0 deletions src/transformMatrixIntoCirclePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default (matrix, size) => {
const cellSize = size / matrix.length
let path = ''

matrix.forEach((row, i) => {
row.forEach((column, j) => {
if (column) {
path += `
M ${cellSize * j + cellSize / 2} ${cellSize * i}
A ${cellSize / 2} 0 0 1 1 ${cellSize * j + cellSize / 2 - 0.0001} ${
cellSize * i - 0.00001
}`
}
})
})

return {
cellSize,
path
}
}