Skip to content

Commit

Permalink
feat:circle mode for QR code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
sakul-budhathoki committed Apr 14, 2021
1 parent cdba62f commit b751453
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
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
}
}

0 comments on commit b751453

Please sign in to comment.