Skip to content

Commit

Permalink
canvas margin option + depedency update
Browse files Browse the repository at this point in the history
  • Loading branch information
justraman committed Jan 11, 2021
1 parent 0bea413 commit 06308af
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 236 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ width |number|`300` |Size of canvas
height |number|`300` |Size of canvas
data |string| |The date will be encoded to the QR code
image |string| |The image will be copied to the center of the QR code
margin |number|`0` |Margin around canvas
qrOptions |object| |Options will be passed to `qrcode-generator` lib
imageOptions |object| |Specific image options, details see below
dotsOptions |object| |Dots styling options
Expand Down
425 changes: 197 additions & 228 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
"eslint-config-prettier": "^6.1.0",
"eslint-loader": "^2.2.1",
"eslint-plugin-jest": "^22.15.1",
"eslint-plugin-prettier": "^3.1.0",
"html-webpack-plugin": "^4.5.0",
"eslint-plugin-prettier": "^3.3.1",
"html-webpack-plugin": "^4.5.1",
"jest": "^26.6.3",
"jest-environment-jsdom-fifteen": "^1.0.0",
"prettier": "^1.18.2",
"ts-jest": "^26.4.4",
"ts-loader": "^6.2.0",
"typescript": "^3.6.4",
"webpack": "^5.11.0",
"webpack": "^5.12.3",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.11.0",
"webpack-merge": "^5.7.2"
"webpack-dev-server": "^3.11.1",
"webpack-merge": "^5.7.3"
},
"scripts": {
"build": "webpack --mode=production --config webpack.config.build.js",
Expand Down
Binary file added src/assets/test/simple_qr_with_margin_canvas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/core/QRCanvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@ describe("Test QRCanvas class", () => {
done();
});
});
it("Should draw a qr code with margin around canvas", () => {
const expectedQRCodeFile = fs.readFileSync(
path.resolve(__dirname, "../assets/test/simple_qr_with_margin_canvas.png"),
"base64"
);
const canvas = new QRCanvas({
...defaultOptions,
margin: 20
});
canvas.drawQR(qr);
expect(canvas.getCanvas().toDataURL()).toEqual(expect.stringContaining(expectedQRCodeFile));
});
});
6 changes: 3 additions & 3 deletions src/core/QRCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class QRCanvas {

async drawQR(qr: QRCode): Promise<void> {
const count = qr.getModuleCount();
const minSize = Math.min(this._options.width, this._options.height);
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin / 2;
const dotSize = Math.floor(minSize / count);
let drawImageSize = {
hideXDots: 0,
Expand Down Expand Up @@ -173,7 +173,7 @@ export default class QRCanvas {
throw "The canvas is too small.";
}

const minSize = Math.min(options.width, options.height);
const minSize = Math.min(options.width, options.height) - options.margin;
const dotSize = Math.floor(minSize / count);
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
Expand Down Expand Up @@ -239,7 +239,7 @@ export default class QRCanvas {
const options = this._options;

const count = this._qr.getModuleCount();
const minSize = Math.min(options.width, options.height);
const minSize = Math.min(options.width, options.height) - options.margin;
const dotSize = Math.floor(minSize / count);
const cornersSquareSize = dotSize * 7;
const cornersDotSize = dotSize * 3;
Expand Down
2 changes: 2 additions & 0 deletions src/core/QROptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type Options = {
export interface RequiredOptions extends Options {
width: number;
height: number;
margin: number;
data: string;
qrOptions: {
typeNumber: TypeNumber;
Expand Down Expand Up @@ -86,6 +87,7 @@ const defaultOptions: RequiredOptions = {
width: 300,
height: 300,
data: "",
margin: 0,
qrOptions: {
typeNumber: qrTypes[0],
mode: undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/tools/sanitizeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ export default function sanitizeOptions(options: RequiredOptions): RequiredOptio

newOptions.width = Number(newOptions.width);
newOptions.height = Number(newOptions.height);
newOptions.margin = Number(newOptions.margin);
newOptions.imageOptions = {
...newOptions.imageOptions,
hideBackgroundDots: Boolean(newOptions.imageOptions.hideBackgroundDots),
imageSize: Number(newOptions.imageOptions.imageSize),
margin: Number(newOptions.imageOptions.margin)
};

if (newOptions.margin > Math.min(newOptions.width, newOptions.height)) {
newOptions.margin = Math.min(newOptions.width, newOptions.height);
}

newOptions.dotsOptions = {
...newOptions.dotsOptions
};
Expand Down

0 comments on commit 06308af

Please sign in to comment.