Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(sharing): export QR PNGs at a fixed pixel size

tianma-if492575bdcb25
diff --git a/apps/web/src/components/dialogs/ShareQrCode.test.mjs b/apps/web/src/components/dialogs/ShareQrCode.test.mjs
index 6be93745..173150f8 100644
--- a/apps/web/src/components/dialogs/ShareQrCode.test.mjs
+++ b/apps/web/src/components/dialogs/ShareQrCode.test.mjs
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { QRCodeSVG } from "qrcode.react";
-import { shareQrCodeFileName } from "./ShareQrCode.tsx";
+import { shareQrCodeFileName, shareQrCodePngBlob } from "./ShareQrCode.tsx";
const source = (name) => readFileSync(new URL(`./${name}`, import.meta.url), "utf8");
@@ -62,4 +62,41 @@ describe("share link QR code", () => {
expect(shareQrCodeFileName("https://notes.example/s/%3Cx%3E", "png")).toBe("edgeever-share-qr-3Cx3E.png");
expect(shareQrCodeFileName("not a url", "png")).toBe("edgeever-share-qr.png");
});
+
+ test("exports 1024 physical pixels at different screen densities without resizing the source", async () => {
+ const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document");
+ try {
+ for (const ratio of [1, 1.25, 2, 3]) {
+ const sourceCanvas = { width: 1024 * ratio, height: 1024 * ratio };
+ let drawnSource;
+ const context = {
+ imageSmoothingEnabled: true,
+ drawImage(source) { drawnSource = source; },
+ };
+ const exportCanvas = {
+ width: 0,
+ height: 0,
+ getContext: () => context,
+ toBlob(callback, type) {
+ callback(new Blob([`${this.width}x${this.height}`], { type }));
+ },
+ };
+ Object.defineProperty(globalThis, "document", {
+ configurable: true,
+ value: { createElement: () => exportCanvas },
+ });
+
+ const blob = await shareQrCodePngBlob(sourceCanvas);
+ expect(blob.type).toBe("image/png");
+ expect(await blob.text()).toBe("1024x1024");
+ expect(drawnSource).toBe(sourceCanvas);
+ expect(context.imageSmoothingEnabled).toBe(false);
+ expect(sourceCanvas.width).toBe(1024 * ratio);
+ expect(sourceCanvas.height).toBe(1024 * ratio);
+ }
+ } finally {
+ if (originalDocument) Object.defineProperty(globalThis, "document", originalDocument);
+ else delete globalThis.document;
+ }
+ });
});
diff --git a/apps/web/src/components/dialogs/ShareQrCode.tsx b/apps/web/src/components/dialogs/ShareQrCode.tsx
index 41aafb2e..e0edfd2d 100644
--- a/apps/web/src/components/dialogs/ShareQrCode.tsx
+++ b/apps/web/src/components/dialogs/ShareQrCode.tsx
@@ -10,6 +10,18 @@ import { canvasToPngBlob, downloadBlob } from "@/lib/infographic-image-export";
const QR_DOWNLOAD_SIZE = 1024;
const QR_DOWNLOAD_MARGIN = 4;
+export const shareQrCodePngBlob = (source: HTMLCanvasElement) => {
+ // QRCodeCanvas scales its backing store by devicePixelRatio. Export physical
+ // pixels at a fixed size regardless of screen density or browser zoom.
+ const canvas = document.createElement("canvas");
+ canvas.width = canvas.height = QR_DOWNLOAD_SIZE;
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("canvas");
+ context.imageSmoothingEnabled = false;
+ context.drawImage(source, 0, 0, QR_DOWNLOAD_SIZE, QR_DOWNLOAD_SIZE);
+ return canvasToPngBlob(canvas);
+};
+
/** File name from the last path segment of the link: the custom address or the token. */
export const shareQrCodeFileName = (url: string, extension: "png" | "svg") => {
let segment = "";
@@ -33,7 +45,7 @@ export const ShareQrCode = ({ url, hint }: { url: string; hint: string }) => {
const downloadPng = async () => {
if (!canvasRef.current) return;
- downloadBlob(await canvasToPngBlob(canvasRef.current), shareQrCodeFileName(url, "png"));
+ downloadBlob(await shareQrCodePngBlob(canvasRef.current), shareQrCodeFileName(url, "png"));
};
const downloadSvg = () => {