Skip to content

Commit

Permalink
kozakdenys#17 add SVG support
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Kozak committed May 27, 2021
1 parent 4cac713 commit b01c0ac
Show file tree
Hide file tree
Showing 16 changed files with 1,246 additions and 45 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions src/constants/drawTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DrawTypes } from "../types";

export default {
canvas: "canvas",
svg: "svg"
} as DrawTypes;
6 changes: 3 additions & 3 deletions src/core/QRCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import calculateImageSize from "../tools/calculateImageSize";
import errorCorrectionPercents from "../constants/errorCorrectionPercents";
import QRDot from "./QRDot";
import QRCornerSquare from "./QRCornerSquare";
import QRCornerDot from "./QRCornerDot";
import QRDot from "../figures/dot/canvas/QRDot";
import QRCornerSquare from "../figures/cornerSquare/canvas/QRCornerSquare";
import QRCornerDot from "../figures/cornerDot/canvas/QRCornerDot";
import { RequiredOptions, Gradient } from "./QROptions";
import gradientTypes from "../constants/gradientTypes";
import { QRCode } from "../types";
Expand Down
45 changes: 37 additions & 8 deletions src/core/QRCodeStyling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import getMode from "../tools/getMode";
import mergeDeep from "../tools/merge";
import downloadURI from "../tools/downloadURI";
import QRCanvas from "./QRCanvas";
import QRSVG from "./QRSVG";
import drawTypes from "../constants/drawTypes";

import defaultOptions, { Options, RequiredOptions } from "./QROptions";
import sanitizeOptions from "../tools/sanitizeOptions";
import { Extension, QRCode } from "../types";
Expand All @@ -16,6 +19,7 @@ export default class QRCodeStyling {
_options: RequiredOptions;
_container?: HTMLElement;
_canvas?: QRCanvas;
_svg?: QRSVG;
_qr?: QRCode;
_drawingPromise?: Promise<void>;

Expand All @@ -41,8 +45,15 @@ export default class QRCodeStyling {
this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
this._qr.addData(this._options.data, this._options.qrOptions.mode || getMode(this._options.data));
this._qr.make();
this._canvas = new QRCanvas(this._options);
this._drawingPromise = this._canvas.drawQR(this._qr);

if (this._options.type === drawTypes.canvas) {
this._canvas = new QRCanvas(this._options);
this._drawingPromise = this._canvas.drawQR(this._qr);
} else {
this._svg = new QRSVG(this._options);
this._drawingPromise = this._svg.drawQR(this._qr);
}

this.append(this._container);
}

Expand All @@ -55,8 +66,14 @@ export default class QRCodeStyling {
throw "Container should be a single DOM node";
}

if (this._canvas) {
container.appendChild(this._canvas.getCanvas());
if (this._options.type === drawTypes.canvas) {
if (this._canvas) {
container.appendChild(this._canvas.getCanvas());
}
} else {
if (this._svg) {
container.appendChild(this._svg.getElement());
}
}

this._container = container;
Expand All @@ -66,8 +83,6 @@ export default class QRCodeStyling {
if (!this._drawingPromise) return;

this._drawingPromise.then(() => {
if (!this._canvas) return;

let extension = "png";
let name = "qr";

Expand All @@ -86,8 +101,22 @@ export default class QRCodeStyling {
}
}

const data = this._canvas.getCanvas().toDataURL(`image/${extension}`);
downloadURI(data, `${name}.${extension}`);
if (this._options.type === drawTypes.canvas) {
if (!this._canvas) return;

const data = this._canvas.getCanvas().toDataURL(`image/${extension}`);
downloadURI(data, `${name}.${extension}`);
} else {
if (!this._svg) return;

//get svg source.
const serializer = new XMLSerializer();
let source = serializer.serializeToString(this._svg.getElement());

source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
const url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
downloadURI(url, `${name}.svg`);
}
});
}
}
6 changes: 5 additions & 1 deletion src/core/QROptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import qrTypes from "../constants/qrTypes";
import drawTypes from "../constants/drawTypes";
import errorCorrectionLevels from "../constants/errorCorrectionLevels";
import {
DotType,
Expand All @@ -7,7 +8,8 @@ import {
CornerDotType,
TypeNumber,
ErrorCorrectionLevel,
Mode
Mode,
DrawType
} from "../types";

export type Gradient = {
Expand Down Expand Up @@ -57,6 +59,7 @@ export type Options = {
};

export interface RequiredOptions extends Options {
type: DrawType;
width: number;
height: number;
margin: number;
Expand Down Expand Up @@ -84,6 +87,7 @@ export interface RequiredOptions extends Options {
}

const defaultOptions: RequiredOptions = {
type: drawTypes.canvas,
width: 300,
height: 300,
data: "",
Expand Down
Loading

0 comments on commit b01c0ac

Please sign in to comment.