devmandan's picture
Claude Opus 5.5
Slants and cans, what a phone sees, a test sheet, SVG files and more
378f397 verified
Raw History Blame Contribute Delete
5.75 kB
// Small pieces every part of the page shares: elements, words and numbers.
const SVGNS = 'http://www.w3.org/2000/svg';
export const BASE_FRAME = 1280; // a typical phone camera image; the count behind every shade
export const HERE = ['zxing-java', 'wechat', 'zxing-cpp', 'opencv-aruco', 'opencv']; // the five this server runs, page order
export const WORDS = ['none', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const SPOKEN = { // scanner names inside sentences
'apple-vision': 'Apple’s scanner', google: 'Google’s scanner', 'hms-scankit': 'Huawei’s scanner', 'zxing-java': 'Android Settings’ scanner',
wechat: 'WeChat’s scanner', 'zxing-cpp': 'zxing-cpp', 'opencv-aruco': 'OpenCV’s newer detector', opencv: 'OpenCV’s original detector',
};
export const $ = (id) => document.getElementById(id);
export const css = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
export function el(tag, attrs = {}, children = []) {
const node = document.createElement(tag);
for (const [k, v] of Object.entries(attrs)) {
if (k === 'text') node.textContent = v;
else if (k === 'class') node.className = v;
else node.setAttribute(k, v);
}
for (const c of [].concat(children)) if (c != null) node.append(c);
return node;
}
export function sv(tag, attrs = {}, parent = null) {
const node = document.createElementNS(SVGNS, tag);
for (const [k, v] of Object.entries(attrs)) {
if (k === 'text') node.textContent = v;
else node.setAttribute(k, v);
}
if (parent) parent.append(node);
return node;
}
/** An element holding one string, or nothing when the string is empty (a missing word leaves its place out). */
export const textEl = (tag, cls, text, attrs = {}) => (text ? el(tag, { class: cls, ...attrs, text }) : null);
export const join = (xs) => (xs.length < 2 ? xs.join('') : `${xs.slice(0, -1).join(', ')} and ${xs[xs.length - 1]}`);
export const capital = (s) => s.replace(/^./, (c) => c.toUpperCase());
export const lower = (s) => s.replace(/^./, (c) => c.toLowerCase());
export const count = (n) => n.toLocaleString('en');
/** A share as a whole percentage, never 0% unless none read it nor 100% unless all did. */
export const pctText = (v) => (v > 0 && v < 0.005 ? '<1%' : v >= 0.995 && v < 1 ? '>99%' : `${Math.round(100 * v)}%`);
export const still = () => window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/** A "name: read · name: missed" line whose entries never break inside, so a line never starts with a dot. */
export const entries = (items) => items.flatMap((t, j) => [j ? ' ' : null, el('span', { class: 'entry', text: `${t}${j < items.length - 1 ? ' ·' : ''}` })]);
/** Scanner names for a sentence; both OpenCV detectors together read as one phrase. */
export function sayNames(keys) {
const both = keys.includes('opencv-aruco') && keys.includes('opencv');
const names = keys.filter((k) => !(both && k.startsWith('opencv'))).map((k) => SPOKEN[k]);
return both ? [...names, 'both OpenCV detectors'] : names;
}
/** The five that read a picture at 1280 pixels, the size behind every count. */
export const readAt1280 = (r) => HERE.filter((k) => r.reads[k][r.order.indexOf(BASE_FRAME)]);
/** Scroll a place into view and put keyboard focus on it, without a glide for those who asked for less motion. */
export function goTo(where, focus, block = 'start') {
where.scrollIntoView({ behavior: still() ? 'auto' : 'smooth', block });
focus?.focus({ preventScroll: true });
}
// ---- views of a picture's code: the one at the top of "Your picture" (yours.js) and what a phone sees (phone.js) ----
/** Corners 4 squares out from the code: the code's own grid (0..n squares) mapped through its four corners. */
function marginQuad(quad, n) {
const A = [];
[[0, 0], [n, 0], [n, n], [0, n]].forEach(([x, y], i) => {
const [u, v] = quad[i];
A.push([x, y, 1, 0, 0, 0, -u * x, -u * y, u], [0, 0, 0, x, y, 1, -v * x, -v * y, v]);
});
for (let c = 0; c < 8; c++) { // Gauss-Jordan, partial pivoting
let piv = c;
for (let k = c + 1; k < 8; k++) if (Math.abs(A[k][c]) > Math.abs(A[piv][c])) piv = k;
[A[c], A[piv]] = [A[piv], A[c]];
for (let k = 0; k < 8; k++) {
if (k === c) continue;
const f = A[k][c] / A[c][c];
for (let j = c; j <= 8; j++) A[k][j] -= f * A[c][j];
}
}
const h = A.map((row, i) => row[8] / row[i]);
const at = (x, y) => { const w = h[6] * x + h[7] * y + 1; return [(h[0] * x + h[1] * y + h[2]) / w, (h[3] * x + h[4] * y + h[5]) / w]; };
return [[-4, -4], [n + 4, -4], [n + 4, n + 4], [-4, n + 4]].map(([x, y]) => at(x, y));
}
/** The square a view shows at k view pixels per picture pixel: the code and 4 squares round it, with 12% more, centred.
* q: those corners 4 squares out, scaled; x0, y0: the square's top-left, side: its side, in view pixels. */
export function marginCrop(quad, n, k) {
const q = marginQuad(quad, n).map(([x, y]) => [x * k, y * k]);
const xs = q.map((v) => v[0]);
const ys = q.map((v) => v[1]);
const side = 1.12 * Math.max(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys));
return { q, side, x0: (Math.max(...xs) + Math.min(...xs) - side) / 2, y0: (Math.max(...ys) + Math.min(...ys) - side) / 2 };
}
/** Stripes over [0, n] on both axes, u canvas pixels to a CSS pixel: where a view runs past the picture's edge (white
* would pass for margin). Grey on grey, on the picture, not the page: the same in both themes. */
export function stripes(g, n, u) {
g.fillStyle = '#d4d4d4';
g.fillRect(0, 0, n, n);
g.strokeStyle = '#a3a3a3';
g.lineWidth = 1.5 * u;
g.beginPath();
for (let x = -n; x < n; x += 6 * u) { g.moveTo(x, n); g.lineTo(x + n, 0); }
g.stroke();
}