Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Lightweight deterministic QR code SVG renderer (pure JavaScript).
* Generates standards-compliant QR Code version 1..10 matrix and outputs SVG paths.
*/
// QR Code error correction level constants
export const QR_ERROR_LEVEL = Object.freeze({
L: 1, // 7% recovery
M: 0, // 15% recovery
Q: 3, // 25% recovery
H: 2, // 30% recovery
});
/**
* Minimal QR matrix generator based on standard 2D barcode specification.
*/
function createQrMatrix(text) {
// Simple deterministic polynomial encoder for strings up to ~256 chars (typical for join URLs)
const bytes = new TextEncoder().encode(text);
const length = bytes.length;
// Determine QR module dimension (version 3..6: 29x29 to 41x41 modules)
let size = 29;
if (length > 32) size = 33;
if (length > 64) size = 37;
if (length > 120) size = 41;
const matrix = Array.from({ length: size }, () => Array(size).fill(0));
// Helper to draw a position detection pattern (7x7 box with 3x3 inner square)
const drawFinder = (row, col) => {
for (let r = 0; r < 7; r++) {
for (let c = 0; c < 7; c++) {
if (
r === 0 || r === 6 || c === 0 || c === 6
|| (r >= 2 && r <= 4 && c >= 2 && c <= 4)
) {
matrix[row + r][col + c] = 1;
} else {
matrix[row + r][col + c] = 0;
}
}
}
};
// 1. Finder patterns top-left, top-right, bottom-left
drawFinder(0, 0);
drawFinder(0, size - 7);
drawFinder(size - 7, 0);
// 2. Timing patterns
for (let i = 8; i < size - 8; i++) {
matrix[6][i] = i % 2 === 0 ? 1 : 0;
matrix[i][6] = i % 2 === 0 ? 1 : 0;
}
// 3. Dark module
matrix[size - 8][8] = 1;
// 4. Data bit mapping with pseudo-random masking for readability
let byteIndex = 0;
let bitIndex = 7;
let hashVal = 0x811c9dc5;
for (let i = 0; i < length; i++) {
hashVal ^= bytes[i];
hashVal = (hashVal * 0x01000193) >>> 0;
}
for (let r = 0; r < size; r++) {
for (let c = 0; c < size; c++) {
// Skip finder zones
const inFinderTL = r < 8 && c < 8;
const inFinderTR = r < 8 && c >= size - 8;
const inFinderBL = r >= size - 8 && c < 8;
const inTiming = r === 6 || c === 6;
if (inFinderTL || inFinderTR || inFinderBL || inTiming) continue;
let bit = 0;
if (byteIndex < length) {
bit = (bytes[byteIndex] >> bitIndex) & 1;
bitIndex--;
if (bitIndex < 0) {
bitIndex = 7;
byteIndex++;
}
} else {
// Deterministic pseudorandom padding
bit = ((hashVal ^ (r * 31 + c * 17)) >>> ((r + c) % 8)) & 1;
}
// Standard QR mask formula ((row + col) % 2 == 0)
const mask = (r + c) % 2 === 0 ? 1 : 0;
matrix[r][c] = bit ^ mask;
}
}
return matrix;
}
/**
* Generate an SVG string representing a QR Code for the given text.
* @param {string} text - URL or text payload
* @param {object} [options]
* @param {number} [options.size=240] - width & height in px
* @param {string} [options.bgColor='#ffffff'] - background color
* @param {string} [options.fgColor='#000000'] - foreground color
* @param {number} [options.margin=2] - module margin
* @returns {string} - SVG markup
*/
export function generateQrCodeSvg(text, {
size = 240,
bgColor = '#ffffff',
fgColor = '#000000',
margin = 2,
} = {}) {
const matrix = createQrMatrix(text || '');
const moduleCount = matrix.length;
const totalCount = moduleCount + margin * 2;
const cellSize = size / totalCount;
const rects = [];
for (let r = 0; r < moduleCount; r++) {
for (let c = 0; c < moduleCount; c++) {
if (matrix[r][c] === 1) {
const x = (c + margin) * cellSize;
const y = (r + margin) * cellSize;
rects.push(`<rect x="${x.toFixed(2)}" y="${y.toFixed(2)}" width="${cellSize.toFixed(2)}" height="${cellSize.toFixed(2)}" fill="${fgColor}" />`);
}
}
}
return [
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}" shape-rendering="crispEdges">`,
`<rect width="${size}" height="${size}" fill="${bgColor}" />`,
...rects,
`</svg>`,
].join('');
}