diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4283727..24b6203 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+## 2.3.0 (2022-11-14)
+
+### ✨ Features
+
+ * Add logo to canvas
+
+Credits
+
+* [@Bunlong](https://github.com/Bunlong)
+
## 2.2.2 (2022-10-17)
### ✨ Features
diff --git a/README.md b/README.md
index d364aaa..17177a3 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@ yarn add next-qrcode
## 💡 Canvas
-### usage
+### Usage
```js
import React from 'react';
@@ -60,7 +60,7 @@ function App() {
export default App;
```
-### props
+### Canvas props
@@ -84,6 +84,12 @@ export default App;
❌ |
QR code options. |
+
+ logo |
+ logo |
+ ❌ |
+ QR code options. |
+
@@ -145,9 +151,73 @@ export default App;
+### logo
+
+
+
+
+ Prop |
+ Type |
+ Require |
+ Description |
+
+
+
+
+ src |
+ string |
+ ✔️ |
+ The path to the image |
+
+
+ options |
+ options |
+ ❌ |
+ Logo options |
+
+
+
+
+### options
+
+
+
+
+ Prop |
+ Type |
+ Default |
+ Require |
+ Description |
+
+
+
+
+ width |
+ number |
+ |
+ ❌ |
+ Logo dimension. |
+
+
+ x |
+ number |
+ |
+ ❌ |
+ If none, will center. |
+
+
+ y |
+ number |
+ |
+ ❌ |
+ If none, will center. |
+
+
+
+
## 💡 Image
-### usage
+### Usage
```js
import React from 'react';
@@ -178,7 +248,7 @@ function App() {
export default App;
```
-### props
+### Image props
@@ -279,9 +349,9 @@ export default App;
## 📜 Changelog
-Latest version 2.2.2 (2022-10-17):
+Latest version 2.3.0 (2022-11-14):
- * Update dependencies
+ * Add logo to canvas
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/Bunlong/next-qrcode/blob/master/CHANGELOG.md).
diff --git a/package.json b/package.json
index a20d2c7..546c239 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "next-qrcode",
- "version": "2.2.2",
+ "version": "2.3.0",
"description": "React hooks for generating QR code for your next React apps.",
"author": "Bunlong ",
"license": "MIT",
@@ -21,7 +21,7 @@
"react-qrcode",
"create-react-app"
],
- "homepage": "https://next-qrcode.github.io",
+ "homepage": "https://next-qrcode.js.org",
"main": "dist/next-qrcode.js",
"module": "dist/next-qrcode.es.js",
"jsnext:main": "dist/next-qrcode.es.js",
@@ -55,6 +55,7 @@
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"bundlesize": "^0.18.1",
+ "canvas": "^2.10.1",
"eslint": "^7.32.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
diff --git a/src/useQRCode.tsx b/src/useQRCode.tsx
index 7e10d0b..df9b72d 100644
--- a/src/useQRCode.tsx
+++ b/src/useQRCode.tsx
@@ -6,7 +6,7 @@ export interface Colors {
light?: string;
}
-export interface Options {
+export interface QRCodeOptions {
type?: string;
quality?: number;
level?: string;
@@ -16,9 +16,21 @@ export interface Options {
color?: Colors;
}
+export interface LogoOptions {
+ width?: number;
+ x?: number;
+ y?: number;
+}
+
+export interface Logo {
+ src: string;
+ options?: LogoOptions;
+}
+
export interface IQRCode {
text: string;
- options?: Options;
+ options?: QRCodeOptions;
+ logo?: Logo;
}
function useImageComponent() {
@@ -56,6 +68,7 @@ function useCanvasComponent() {
const CanvasComponent = ({
text,
options,
+ logo,
}: IQRCode) => {
const inputRef = React.useRef(null);
@@ -72,9 +85,34 @@ function useCanvasComponent() {
}
},
);
+
+ if (logo) {
+ const crt = inputRef.current;
+ const ctx = crt.getContext('2d');
+ if (ctx) {
+ const img = new Image();
+ img.src = logo.src;
+ const logoWidth = logo?.options?.width || 30;
+ if (logo?.options?.x && logo?.options?.y) {
+ const x = logo?.options?.x;
+ const y = logo?.options?.y;
+ img.onload = function () {
+ ctx.drawImage(img, x, y, logoWidth, logoWidth);
+ };
+ } else if (!logo?.options?.x || !logo?.options?.y) {
+ let margin = options?.margin;
+ margin = !margin ? (margin === 0 ? 0 : 32) : (margin * 8);
+ const width = options?.width || (116 + margin);
+ const center = (width - logoWidth) / 2;
+ img.onload = function () {
+ ctx.drawImage(img, center, center, logoWidth, logoWidth);
+ };
+ }
+ }
+ }
}
},
- [text, options, inputRef],
+ [inputRef, text, options, logo],
);
return ;
diff --git a/supports/create-next-app/pages/index.tsx b/supports/create-next-app/pages/index.tsx
index 028b505..033fcf8 100644
--- a/supports/create-next-app/pages/index.tsx
+++ b/supports/create-next-app/pages/index.tsx
@@ -2,11 +2,33 @@ import type { NextPage } from 'next'
import { useQRCode } from 'next-qrcode'
const Home: NextPage = () => {
- const { Canvas } = useQRCode()
+ const { Canvas, Image } = useQRCode()
return (
<>
+
+ {/* {
light: '#FFBF60FF',
},
}}
- />
+ /> */}
>
)
}
export default Home
+
+// logo={
+// src: '',
+// options: {
+// x: '',
+// y: '',
+// width: 100,
+// },
+// }