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

Node support fix. Remove toDataUrl call. Fix types #241

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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
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
Loading