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

Cut out the logo if the QR and logo background are transparent #212

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
18 changes: 17 additions & 1 deletion Example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {SafeAreaView, ScrollView, Text} from 'react-native';
import {SafeAreaView, ScrollView, Text, View} from 'react-native';
import SVG from './assets/ruby.svg';
import Description from './components/Description';
import QRCode from 'react-native-qrcode-svg';
Expand Down Expand Up @@ -50,6 +50,21 @@ const localPngQRCode = (
</Description>
);

const localTransparentPngQRCode = (
<Description text="QR code with local transparent PNG">
<View style={{backgroundColor: 'blue'}}>
<QRCode
value={googleUrl}
size={qrCodeSize}
logo={require('./assets/google.png')}
logoSize={logoSize}
logoBackgroundColor="transparent"
backgroundColor="transparent"
/>
</View>
</Description>
);

const localPngBackgroundColorQRCode = (
<Description text="QR code with local PNG with background color avatar">
<QRCode
Expand Down Expand Up @@ -119,6 +134,7 @@ const App = () => {
{colorfulQRCODe}
{urlPngQRCode}
{localPngQRCode}
{localTransparentPngQRCode}
{localPngBackgroundColorQRCode}
{urlSvgQRCode}
{stringSvgQRCode}
Expand Down
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ const QRCode = ({
const { path, cellSize } = result;
const displayLogo = logo || logoSVG;

const logoPosition = (size - logoSize - logoMargin * 2) / 2;
const logoPositionEnd = logoPosition + logoSize + logoMargin * 2;

return (
<Svg
testID={testID}
Expand All @@ -146,6 +149,12 @@ const QRCode = ({
<Stop offset="0" stopColor={linearGradient[0]} stopOpacity="1" />
<Stop offset="1" stopColor={linearGradient[1]} stopOpacity="1" />
</LinearGradient>
<ClipPath id="invert-clip-logo">
<Path
d={`M0,0 L${size},0 L${size},${size} L0,${size} Z
M${logoPosition},${logoPosition} L${logoPosition},${logoPositionEnd} L${logoPositionEnd},${logoPositionEnd} L${logoPositionEnd},${logoPosition} Z`}
/>
</ClipPath>
</Defs>
<G>
<Rect
Expand All @@ -162,6 +171,12 @@ const QRCode = ({
strokeLinecap="butt"
stroke={enableLinearGradient ? "url(#grad)" : color}
strokeWidth={cellSize}
clipPath={
displayLogo &&
logoBackgroundColor === "transparent" &&
backgroundColor === "transparent" &&
"url(#invert-clip-logo)"
}
/>
</G>
{displayLogo &&
Expand Down