-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): enable use with NPM
- Loading branch information
Showing
16 changed files
with
213 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Dependency directories | ||
node_modules/ | ||
|
||
# build artifacts | ||
dist | ||
dist-* | ||
types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const qrcodegen = require("./dist/qrcodegen.cjs").default; | ||
|
||
module.exports = qrcodegen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import qrcodegen from "./dist-esm/qrcodegen.js"; | ||
|
||
export default qrcodegen; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"name": "qr-code-generator", | ||
"version": "1.8.1", | ||
"description": "QR Code generator library in multiple languages", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./types/qrcodegen.d.ts", | ||
"require": "./index.cjs", | ||
"default": "./index.js" | ||
}, | ||
"./package.json": "./package.json", | ||
"./utils": { | ||
"types": "./types/utils/index.d.ts", | ||
"require": "./dist/utils/index.cjs", | ||
"default": "./dist-esm/utils/index.js" | ||
} | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"utils": [ | ||
"types/utils/index.d.ts" | ||
] | ||
} | ||
}, | ||
"types": "types/qrcodegen.d.ts", | ||
"files": [ | ||
"index.cjs", | ||
"index.js", | ||
"dist", | ||
"dist-esm", | ||
"types" | ||
], | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"scripts": { | ||
"build:es5": "tsc -p ./tsconfig.es5.json", | ||
"postbuild:es5": "find ./dist -name \"*.js\" -exec sh -c 'mv \"$1\" \"${1%.js}.cjs\"' _ {} \\;", | ||
"build:es6": "tsc -p ./tsconfig.es6.json", | ||
"prebuild": "npm run clean", | ||
"build": "npm run build:es5 & npm run build:es6", | ||
"clean": "rm -rf dist dist-* types", | ||
"predemo": "./build.sh", | ||
"demo": "npx --yes browser-sync start --server --index qrcodegen-$npm_config_type-demo.html --no-notify --no-open --no-snippet --no-ui --no-ghost-mode", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.9.4" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nayuki/QR-Code-generator.git" | ||
}, | ||
"keywords": [ | ||
"qr", | ||
"qrcode", | ||
"generator", | ||
"svg" | ||
], | ||
"author": "Project Nayuki (me@nayuki.io)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nayuki/QR-Code-generator/issues" | ||
}, | ||
"homepage": "https://github.com/nayuki/QR-Code-generator#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"declaration": true, | ||
"declarationDir": "./types" | ||
}, | ||
"include": ["qrcodegen.ts", "utils/index.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.base", | ||
"compilerOptions": { | ||
"target": "ES5", | ||
"outDir": "dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.base", | ||
"compilerOptions": { | ||
"target": "ES2015", | ||
"outDir": "dist-esm" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import qrcodegen from "../qrcodegen"; | ||
|
||
// Draws the given QR Code, with the given module scale and border modules, onto the given HTML | ||
// canvas element. The canvas's width and height is resized to (qr.size + border * 2) * scale. | ||
// The drawn image is purely dark and light, and fully opaque. | ||
// The scale must be a positive integer and the border must be a non-negative integer. | ||
export function drawCanvas( | ||
qr: qrcodegen.QrCode, | ||
scale: number, | ||
border: number, | ||
lightColor: string, | ||
darkColor: string, | ||
canvas: HTMLCanvasElement | ||
): void { | ||
if (scale <= 0 || border < 0) throw new RangeError("Value out of range"); | ||
|
||
const width: number = (qr.size + border * 2) * scale; | ||
canvas.width = width; | ||
canvas.height = width; | ||
let ctx = canvas.getContext("2d") as CanvasRenderingContext2D; | ||
|
||
for (let y = -border; y < qr.size + border; y++) { | ||
for (let x = -border; x < qr.size + border; x++) { | ||
ctx.fillStyle = qr.getModule(x, y) ? darkColor : lightColor; | ||
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale); | ||
} | ||
} | ||
} | ||
|
||
// Returns a string of SVG code for an image depicting the given QR Code, with the given number | ||
// of border modules. The string always uses Unix newlines (\n), regardless of the platform. | ||
export function toSvgString( | ||
qr: qrcodegen.QrCode, | ||
border: number, | ||
lightColor: string, | ||
darkColor: string | ||
): string { | ||
if (border < 0) throw new RangeError("Border must be non-negative"); | ||
let parts: Array<string> = []; | ||
for (let y = 0; y < qr.size; y++) { | ||
for (let x = 0; x < qr.size; x++) { | ||
if (qr.getModule(x, y)) | ||
parts.push(`M${x + border},${y + border}h1v1h-1z`); | ||
} | ||
} | ||
return `<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | ||
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ${ | ||
qr.size + border * 2 | ||
} ${qr.size + border * 2}" stroke="none"> | ||
<rect width="100%" height="100%" fill="${lightColor}"/> | ||
<path d="${parts.join(" ")}" fill="${darkColor}"/> | ||
</svg> | ||
`; | ||
} |