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

added eyeframes and eyeballs #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Binary file added src/.DS_Store
Binary file not shown.
Binary file modified src/assets/.DS_Store
Binary file not shown.
12 changes: 11 additions & 1 deletion src/constants/cornerDotTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@ import { CornerDotTypes } from "../types";

export default {
dot: "dot",
square: "square"
square: "square",
star: "star",
plus: "plus",
squareRounded: "square-rounded",
rightBottomSquare: "square-right-bottom",
leaf: "leaf",
leftTopCircle: "circle-left-top",
rightBottomCircle: "circle-right-bottom",
diamond: "diamond",
cross: "cross",
rhombus: "rhombus"
} as CornerDotTypes;
10 changes: 9 additions & 1 deletion src/constants/cornerSquareTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ import { CornerSquareTypes } from "../types";
export default {
dot: "dot",
square: "square",
extraRounded: "extra-rounded"
dottedSquare:"dotted-square",
extraRounded: "extra-rounded",
rightBottomSquare: "right-bottom-square",
leftTopSquare: "left-top-square",
leftTopCircle: "circle-left-top",
rightBottomCircle: "circle-right-bottom",
circleInSquare: "circle-in-square",
peanut: "peanut",
// paragonal: "paragonal",
} as CornerSquareTypes;
3 changes: 2 additions & 1 deletion src/constants/dotTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default {
classy: "classy",
classyRounded: "classy-rounded",
square: "square",
extraRounded: "extra-rounded"
extraRounded: "extra-rounded",
star: "star",
} as DotTypes;
46 changes: 13 additions & 33 deletions src/core/QRCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import QRCornerSquare from "../figures/cornerSquare/canvas/QRCornerSquare";
import QRCornerDot from "../figures/cornerDot/canvas/QRCornerDot";
import { RequiredOptions } from "./QROptions";
import gradientTypes from "../constants/gradientTypes";
import { QRCode, Gradient, FilterFunction, Canvas } from "../types";
import { QRCode, Gradient, FilterFunction } from "../types";

const squareMask = [
[1, 1, 1, 1, 1, 1, 1],
Expand All @@ -28,18 +28,14 @@ const dotMask = [
];

export default class QRCanvas {
_canvas: Canvas;
_canvas: HTMLCanvasElement;
_options: RequiredOptions;
_qr?: QRCode;
_image?: HTMLImageElement;

//TODO don't pass all options to this class
constructor(options: RequiredOptions) {
if (options.nodeCanvas?.createCanvas) {
this._canvas = options.nodeCanvas.createCanvas(options.width, options.height);
} else {
this._canvas = document.createElement("canvas");
}
this._canvas = document.createElement("canvas");
this._canvas.width = options.width;
this._canvas.height = options.height;
this._options = options;
Expand All @@ -57,7 +53,7 @@ export default class QRCanvas {
return this._canvas.height;
}

getCanvas(): Canvas {
getCanvas(): HTMLCanvasElement {
return this._canvas;
}

Expand Down Expand Up @@ -361,37 +357,21 @@ export default class QRCanvas {
loadImage(): Promise<void> {
return new Promise((resolve, reject) => {
const options = this._options;
const image = new Image();

if (!options.image) {
return reject("Image is not defined");
}

if (options.nodeCanvas?.loadImage) {
options.nodeCanvas
.loadImage(options.image)
.then((image: HTMLImageElement) => {
// fix blurry svg
if (/(\.svg$)|(^data:image\/svg)/.test(options.image ?? "")) {
image.width = this._options.width;
image.height = this._options.height;
}
this._image = image;
resolve();
})
.catch(reject);
} else {
const image = new Image();

if (typeof options.imageOptions.crossOrigin === "string") {
image.crossOrigin = options.imageOptions.crossOrigin;
}

this._image = image;
image.onload = (): void => {
resolve();
};
image.src = options.image;
if (typeof options.imageOptions.crossOrigin === "string") {
image.crossOrigin = options.imageOptions.crossOrigin;
}

this._image = image;
image.onload = (): void => {
resolve();
};
image.src = options.image;
});
}

Expand Down
59 changes: 1 addition & 58 deletions src/core/QRCodeStyling.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import QRCodeStyling from "./QRCodeStyling";
import fs from "fs";
import path from "path";
import nodeCanvas from "canvas";
import { JSDOM } from "jsdom";

describe("Test QRCodeStyling class", () => {
beforeAll(() => {
global.document.body.innerHTML = "<div id='container'></div>";
});

it("The README example should work correctly", (done) => {
it("The README example should work correctly", done => {
const expectedQRCodeFile = fs.readFileSync(
path.resolve(__dirname, "../assets/test/image_from_readme.png"),
"base64"
Expand Down Expand Up @@ -39,59 +37,4 @@ describe("Test QRCodeStyling class", () => {
done();
});
});

it("Compatible with node-canvas", (done) => {
const expectedQRCodeFile = fs.readFileSync(
path.resolve(__dirname, "../assets/test/image_from_readme.png"),
"base64"
);
const qrCode = new QRCodeStyling({
nodeCanvas,
width: 300,
height: 300,
data: "TEST",
image:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAAEUlEQVR42mNk+M+AARiHsiAAcCIKAYwFoQ8AAAAASUVORK5CYII=",
dotsOptions: {
color: "#4267b2",
type: "rounded"
},
backgroundOptions: {
color: "#e9ebee"
}
});
qrCode.getRawData("png").then((buffer) => {
const uri = `data:image/png;base64,${buffer.toString("base64")}`;
expect(uri).toEqual(expect.stringContaining(expectedQRCodeFile));
done();
});
});

it("Compatible with jsdom", (done) => {
const expectedQRCodeFile = fs.readFileSync(
path.resolve(__dirname, "../assets/test/image_from_readme.svg"),
"base64"
);
const qrCode = new QRCodeStyling({
jsdom: JSDOM,
type: "svg",
width: 300,
height: 300,
data: "TEST",
image:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAAEUlEQVR42mNk+M+AARiHsiAAcCIKAYwFoQ8AAAAASUVORK5CYII=",
dotsOptions: {
color: "#4267b2",
type: "rounded"
},
backgroundOptions: {
color: "#e9ebee"
}
});
qrCode.getRawData("svg").then((buffer) => {
const svgString = buffer.toString("base64");
expect(svgString).toEqual(expect.stringContaining(expectedQRCodeFile));
done();
});
});
});
23 changes: 6 additions & 17 deletions src/core/QRCodeStyling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,35 +113,24 @@ export default class QRCodeStyling {
this._container = container;
}

async getRawData(extension: Extension = "png"): Promise<Blob | Buffer | null> {
async getRawData(extension: Extension = "png"): Promise<Blob | null> {
if (!this._qr) throw "QR code is empty";
const element = await this._getQRStylingElement(extension);

if (extension.toLowerCase() === "svg") {
const serializer = new ((element as unknown) as QRSVG)._window.XMLSerializer();
const serializer = new XMLSerializer();
const source = serializer.serializeToString(((element as unknown) as QRSVG).getElement());
const svgString = `<?xml version="1.0" standalone="no"?>\r\n${source}`;

if (typeof Blob !== "undefined" && !this._options.jsdom) {
return new Blob([svgString], { type: "image/svg+xml" });
} else {
return Buffer.from(svgString);
}
return new Blob(['<?xml version="1.0" standalone="no"?>\r\n' + source], { type: "image/svg+xml" });
} else {
return new Promise((resolve) => {
const canvas = ((element as unknown) as QRCanvas).getCanvas();
if (canvas.toBuffer) {
resolve(canvas.toBuffer(`image/${extension}`));
} else {
canvas.toBlob(resolve, `image/${extension}`, 1);
}
});
return new Promise((resolve) =>
((element as unknown) as QRCanvas).getCanvas().toBlob(resolve, `image/${extension}`, 1)
);
}
}

async download(downloadOptions?: Partial<DownloadOptions> | string): Promise<void> {
if (!this._qr) throw "QR code is empty";
if (typeof Blob === "undefined") throw "Cannot download in Node.js, call getRawData instead.";
let extension = "png" as Extension;
let name = "qr";

Expand Down
2 changes: 0 additions & 2 deletions src/core/QROptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface RequiredOptions extends Options {
errorCorrectionLevel: ErrorCorrectionLevel;
};
imageOptions: {
saveAsBlob: boolean;
hideBackgroundDots: boolean;
imageSize: number;
crossOrigin?: string;
Expand Down Expand Up @@ -44,7 +43,6 @@ const defaultOptions: RequiredOptions = {
errorCorrectionLevel: errorCorrectionLevels.Q
},
imageOptions: {
saveAsBlob: false,
hideBackgroundDots: true,
imageSize: 0.4,
crossOrigin: undefined,
Expand Down
Loading