forked from rajun/ar-test
feat: add QR code helpers with the logo inside the code
This commit is contained in:
parent
ea26eeb4c2
commit
adfa93cc82
108
app/lib/assets/qr.ts
Normal file
108
app/lib/assets/qr.ts
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
import QRCode from 'qrcode';
|
||||||
|
import { QR_COLORS } from '~/data/assets/constants';
|
||||||
|
import { LOGO_BACKGROUND_PRESETS } from '~/data/projects/constants';
|
||||||
|
import { ROUTES } from '~/data/routes';
|
||||||
|
import { reachableUrl } from '~/lib/local-url';
|
||||||
|
import type { BrandIdentity } from '~/types/project';
|
||||||
|
|
||||||
|
const ID_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
|
|
||||||
|
// PocketBase record IDs: exactly 15 chars of [a-z0-9]
|
||||||
|
export const RECORD_ID_PATTERN = /^[a-z0-9]{15}$/;
|
||||||
|
|
||||||
|
// Share of the QR width cleared for the logo; error correction H still reads the code
|
||||||
|
const QR_LOGO_RATIO = 0.22;
|
||||||
|
// Quiet zone around the code, in modules
|
||||||
|
const QR_MARGIN = 2;
|
||||||
|
|
||||||
|
export function generateRecordId(): string {
|
||||||
|
const bytes = crypto.getRandomValues(new Uint8Array(15));
|
||||||
|
return Array.from(bytes, (b) => ID_ALPHABET[b % ID_ALPHABET.length]).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFrontendUrl(): string {
|
||||||
|
const envUrl = (import.meta.env.VITE_FRONTEND_URL ?? '').trim().replace(/\/+$/, '');
|
||||||
|
if (envUrl) return reachableUrl(envUrl);
|
||||||
|
return typeof window !== 'undefined' ? window.location.origin : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// e.g. "ar.thob.studio", for copy that names the site
|
||||||
|
export const getFrontendHost = () => getFrontendUrl().replace(/^https?:\/\//, '');
|
||||||
|
|
||||||
|
export function getArUrl(id: string): string {
|
||||||
|
return `${getFrontendUrl()}${ROUTES.ar(id)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface QrLayout {
|
||||||
|
// Modules across, quiet zone included
|
||||||
|
size: number;
|
||||||
|
// Dark modules as one SVG path, in module units
|
||||||
|
path: string;
|
||||||
|
// The cleared centre square for the logo, in modules
|
||||||
|
hole?: { start: number; size: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
// The code's modules; a branded code leaves an empty square in the middle for the logo
|
||||||
|
function qrLayout(url: string, branded: boolean): QrLayout {
|
||||||
|
const { modules } = QRCode.create(url, { errorCorrectionLevel: branded ? 'H' : 'M' });
|
||||||
|
const size = modules.size + QR_MARGIN * 2;
|
||||||
|
// An odd number of modules, like the code itself, so the hole sits exactly in the middle
|
||||||
|
const rounded = Math.round(size * QR_LOGO_RATIO);
|
||||||
|
const holeSize = rounded % 2 ? rounded : rounded - 1;
|
||||||
|
const hole = branded ? { start: (size - holeSize) / 2, size: holeSize } : undefined;
|
||||||
|
const inHole = (x: number, y: number) => !!hole && x >= hole.start && x < hole.start + hole.size && y >= hole.start && y < hole.start + hole.size;
|
||||||
|
|
||||||
|
let path = '';
|
||||||
|
for (let row = 0; row < modules.size; row++) {
|
||||||
|
// One rectangle per horizontal run of dark modules
|
||||||
|
let run = 0;
|
||||||
|
for (let col = 0; col <= modules.size; col++) {
|
||||||
|
const x = col + QR_MARGIN;
|
||||||
|
const y = row + QR_MARGIN;
|
||||||
|
const dark = col < modules.size && modules.get(row, col) && !inHole(x, y);
|
||||||
|
if (dark) run++;
|
||||||
|
else if (run) {
|
||||||
|
path += `M${x - run} ${y}h${run}v1h-${run}z`;
|
||||||
|
run = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { size, path, hole };
|
||||||
|
}
|
||||||
|
|
||||||
|
const escapeAttr = (value: string) => value.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<');
|
||||||
|
|
||||||
|
export interface QrLogo {
|
||||||
|
href: string;
|
||||||
|
// Behind the logo: white for light logos, the project's backing colour otherwise
|
||||||
|
fill: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const qrLogoFill = (brand: BrandIdentity) => brand.logoBgColor ?? LOGO_BACKGROUND_PRESETS[brand.logoBg].value;
|
||||||
|
|
||||||
|
interface QrSvgOptions {
|
||||||
|
logo?: QrLogo;
|
||||||
|
label?: string;
|
||||||
|
// Roughly this many pixels wide, rounded to whole pixels per module so they stay sharp; without it, fills its box
|
||||||
|
fileSize?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The QR code as SVG markup, with the logo inside the code rather than laid over it.
|
||||||
|
// The screen, the SVG download and the PNG download all draw from this.
|
||||||
|
export function qrSvgMarkup(url: string, { logo, label, fileSize }: QrSvgOptions = {}): string {
|
||||||
|
const { size, path, hole } = qrLayout(url, !!logo);
|
||||||
|
const pixels = fileSize && size * Math.max(1, Math.floor(fileSize / size));
|
||||||
|
const dimensions = pixels ? `width="${pixels}" height="${pixels}"` : 'width="100%" height="100%"';
|
||||||
|
const title = label ? ` role="img" aria-label="${escapeAttr(label)}"` : '';
|
||||||
|
let centre = '';
|
||||||
|
if (logo && hole) {
|
||||||
|
const inset = 0.6;
|
||||||
|
centre =
|
||||||
|
`<rect x="${hole.start}" y="${hole.start}" width="${hole.size}" height="${hole.size}" rx="0.8" fill="${escapeAttr(logo.fill)}"/>` +
|
||||||
|
`<image href="${escapeAttr(logo.href)}" x="${hole.start + inset}" y="${hole.start + inset}" width="${hole.size - inset * 2}" height="${hole.size - inset * 2}" preserveAspectRatio="xMidYMid meet"/>`;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" ${dimensions}${title} shape-rendering="crispEdges">` +
|
||||||
|
`<rect width="${size}" height="${size}" fill="${QR_COLORS.light}"/><path d="${path}" fill="${QR_COLORS.dark}"/>${centre}</svg>`
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user