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

Use different lib to render qr code, also render it to svg instead of canvas. #38

Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ and only exists to deliver some static files.
### Developing

You can clone this repository, edit the files and open `index.html` -
as everything's static, you can test and play around.
as everything's static[^1], you can test and play around.
Even "Open Chat" should work if Delta Chat is installed.

However, note, that local or other sever's links cannot open Delta Chat directly.

[^1]: if you want to change the qr code library `qr.min.js`, you instead need to edit it's source in src and then run `npm run build-qr-generator` to recompile it.
17 changes: 5 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<link rel="icon" href="identity.svg" sizes="any" type="image/svg+xml">

<script src="./qrcode.min.js"></script>
<script src="./qr.min.js"></script>
<script src="./i18n/i18next.min.js"></script>
<script src="./i18n/en.js"></script>
<script src="./i18n/fr.js"></script>
Expand Down Expand Up @@ -192,15 +192,11 @@
}

details #qrcode {
width:200px;
min-width: 200px;
width: 60%;
padding-top: 1em;
height:200px;
margin: auto;
}

#qrcode img {
image-rendering: pixelated;
}
</style>
</head>
<body>
Expand Down Expand Up @@ -265,6 +261,7 @@ <h2>
<summary>Tap if you have Delta Chat on another device</summary>

<div id="qrcode"></div>

<p>
<i>Scan to open the chat on the other device</i>
</p>
Expand Down Expand Up @@ -296,11 +293,7 @@ <h2>
}

if (isValid) {
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 200,
height : 200
});
qrcode.makeCode(openpgp4fprStr);
QR.renderTextToElement(document.getElementById("qrcode"), openpgp4fprStr)
document.getElementById('say-hello').classList.remove('hidden')
} else {
document.querySelector('form').classList.remove('hidden')
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"scripts": {
"start": "http-server . -p 3000",
"test": "mocha --spec **/ui-test-*.js --file mocha-start.js --reporter dot",
"postinstall": "npx playwright install chromium"
"postinstall": "npx playwright install chromium",
"build-qr-generator": "esbuild qr-src/qr-code.ts --bundle --minify --format=iife --global-name=QR --outfile=qr.min.js --target=es2015 --banner:js='// THIS is compiled from `qr-src/qr-code.ts` with `npm run build-qr-generator`'"
},
"dependencies": {
"i18next": "latest"
},
"devDependencies": {
"mocha": "latest",
"@playwright/test": "latest",
"http-server": "latest",
"start-server-and-test": "latest"
"mocha": "latest",
"start-server-and-test": "latest",
"esbuild": "^0.20.2"
}
}
35 changes: 35 additions & 0 deletions qr-src/qr-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import qrcodegen from "./qrcodegen";

// adapted from https://github.com/nayuki/QR-Code-generator/blob/master/typescript-javascript/qrcodegen-input-demo.ts
// 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.
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 `<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>
`;
}

export function renderTextToElement(element: HTMLDivElement, content: string) {
const code = qrcodegen.QrCode.encodeText(
content,
qrcodegen.QrCode.Ecc.MEDIUM
);
element.innerHTML = toSvgString(code, 2, "white", "black");
}
Loading
Loading