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

Rounded QR-Code #190

Open
wants to merge 2 commits 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
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { Image, ImageSourcePropType } from "react-native";
import { ImageSourcePropType } from "react-native";

declare class QRCode extends React.PureComponent<QRCodeProps, any> {}

Expand Down Expand Up @@ -35,6 +35,8 @@ export interface QRCodeProps {
getRef?: (c: any) => any;
/* error correction level */
ecl?: "L" | "M" | "Q" | "H";
/* Renderind mode */
mode?: "default" | "rounded";
/* error handler called when matrix fails to generate */
onError?: Function;
}
Expand Down
31 changes: 23 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React, { useMemo } from 'react'
import Svg, {
ClipPath,
Defs,
G,
Path,
Rect,
Image,
ClipPath,
LinearGradient,
Path,
Rect,
Stop
} from 'react-native-svg'
import genMatrix from './genMatrix'
import transformMatrixIntoPath from './transformMatrixIntoPath'
import transformMatrixIntoRoundedPath from './transformMatrixIntoRoundedPath'

const renderLogo = ({
size,
Expand Down Expand Up @@ -81,12 +82,18 @@ 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)
const matrix = genMatrix(value, ecl);
switch (mode) {
case 'rounded':
return transformMatrixIntoRoundedPath(matrix, size);
default: return transformMatrixIntoPath(matrix, size);
}
} catch (error) {
if (onError && typeof onError === 'function') {
onError(error)
Expand All @@ -95,7 +102,7 @@ const QRCode = ({
throw error
}
}
}, [value, size, ecl])
}, [value, size, ecl, mode])

if (!result) {
return null
Expand Down Expand Up @@ -135,15 +142,23 @@ const QRCode = ({
height={size + quietZone * 2}
fill={backgroundColor}
/>
</G>
<G>
</G>
{mode === 'default' ? <G>
<Path
d={path}
strokeLinecap='butt'
stroke={enableLinearGradient ? 'url(#grad)' : color}
strokeWidth={cellSize}
opacity={0.5}
/>
</G>
</G> : null}
{mode === 'rounded' ? <G>
<Path
d={path}
strokeLinejoin='round'
fill={enableLinearGradient ? 'url(#grad)' : color}
/>
</G> : null}
{logo &&
renderLogo({
size,
Expand Down
26 changes: 26 additions & 0 deletions src/svg-rounded-rectangle-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Get path data for a rounded rectangle. Allows for different radius on each corner.
* @param {Number} w Width of rounded rectangle
* @param {Number} h Height of rounded rectangle
* @param {Number} tlr Top left corner radius
* @param {Number} trr Top right corner radius
* @param {Number} brr Bottom right corner radius
* @param {Number} blr Bottom left corner radius
* @return {String} Rounded rectangle SVG path data
*/


var roundedRectData = (positionX, positionY, width, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius) => {
return `M${positionX + topLeftRadius},${positionY}\
h${width - (topLeftRadius + topRightRadius)}\
a${topRightRadius},${topRightRadius} 0 0 1 ${topRightRadius},${topRightRadius}\
v${height - (topRightRadius + bottomRightRadius)}\
a${bottomRightRadius},${bottomRightRadius} 0 0 1 -${bottomRightRadius},${bottomRightRadius}\
h-${width - (bottomLeftRadius + bottomRightRadius)}\
a${bottomLeftRadius},${bottomLeftRadius} 0 0 1 -${bottomLeftRadius},-${bottomLeftRadius}\
v-${height - (topLeftRadius + bottomLeftRadius)}\
a${topLeftRadius},${topLeftRadius} 0 0 1 ${topLeftRadius},-${topLeftRadius}\
z`;
}

export default roundedRectData
29 changes: 29 additions & 0 deletions src/transformMatrixIntoRoundedPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import roundedRectData from "./svg-rounded-rectangle-path";

export default function transformMatrixIntoRoundedPath(matrix, size) {
const cellSize = size / matrix.length
let path = ''

function drawRect(x, y) {
const radius = cellSize /2;
const tlr = matrix[x-1]?.[y] || matrix[x][y-1] ? 0 : radius;
const trr = matrix[x+1]?.[y] || matrix[x][y-1] ? 0 : radius;
const brr = matrix[x+1]?.[y] || matrix[x][y+1] ? 0 : radius;
const blr = matrix[x-1]?.[y] || matrix[x][y+1] ? 0 : radius;
return roundedRectData(cellSize * x, cellSize * y, cellSize, cellSize, tlr, trr, brr, blr) + ' '
}

matrix.forEach((row, i) => {
let needDraw = false
row.forEach((column, j) => {
if (column) {
path += drawRect(i, j)
}
})
})

return {
cellSize,
path
}
}