Skip to content

Commit

Permalink
Merge pull request #241 from kozakdenys/node-support
Browse files Browse the repository at this point in the history
Node support fix. Remove toDataUrl call. Fix types
  • Loading branch information
kozakdenys authored Oct 18, 2024
2 parents c607eac + 91e299c commit f666bd6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ const options = {

// For canvas type
const qrCodeImage = new QRCodeStyling({
nodeCanvas, // this is required
...options
jsdom: JSDOM, // this is required
nodeCanvas, // this is required,
...options,
imageOptions: {
saveAsBlob: true,
crossOrigin: "anonymous",
margin: 20
},
});

qrCodeImage.getRawData("png").then((buffer) => {
Expand Down
45 changes: 33 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@eslint/js": "^9.12.0",
"@types/eslint__js": "^8.42.3",
"@types/jsdom": "^21.1.7",
"canvas": "^2.11.2",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
5 changes: 0 additions & 5 deletions src/core/QRSVG.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import calculateImageSize from "../tools/calculateImageSize";
import toDataUrl from "../tools/toDataUrl";
import errorCorrectionPercents from "../constants/errorCorrectionPercents";
import QRDot from "../figures/dot/QRDot";
import QRCornerSquare from "../figures/cornerSquare/QRCornerSquare";
Expand Down Expand Up @@ -531,10 +530,6 @@ export default class QRSVG {
image.setAttribute("width", `${dw}px`);
image.setAttribute("height", `${dh}px`);

const imageUrl = await toDataUrl(options.image || "");

image.setAttribute("href", imageUrl || "");

this._element.appendChild(image);
}

Expand Down
27 changes: 4 additions & 23 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DOMWindow, JSDOM } from "jsdom";

export interface UnknownObject {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
Expand All @@ -16,28 +18,7 @@ export interface Canvas extends HTMLCanvasElement {
createCanvas?: (width: number, height: number) => Canvas;
loadImage?: (image: string) => Promise<HTMLImageElement>;
}

export interface Window {
Image: typeof HTMLImageElement;
XMLSerializer: typeof XMLSerializer;
document: Document;
}
declare const window: Window;

interface JsDomOptions {
resources: string;
}
export class JSDom {
window: Window;
_options: JsDomOptions;
_input: string;

constructor(input: string, options: JsDomOptions) {
this._options = options;
this._input = input;
this.window = window;
}
}
export type Window = DOMWindow;

export type Gradient = {
type: GradientType;
Expand Down Expand Up @@ -140,7 +121,7 @@ export type Options = {
data?: string;
image?: string;
nodeCanvas?: Canvas;
jsdom?: typeof JSDom;
jsdom?: typeof JSDOM;
qrOptions?: {
typeNumber?: TypeNumber;
mode?: Mode;
Expand Down

0 comments on commit f666bd6

Please sign in to comment.