49 lines
2.3 KiB
JavaScript
49 lines
2.3 KiB
JavaScript
import { render, rsEncode, dataBits, SYNC, DISC } from "/static/js/ringcode.js";
|
|
|
|
const params = new URLSearchParams(location.search);
|
|
const MS = +params.get("ms") || 1000, MODE = params.get("mode") || "ring";
|
|
const lab = await fetch("/static/ringlab/lab.json").then((r) => r.json());
|
|
const cv = document.getElementById("stage"), ctx = cv.getContext("2d");
|
|
document.getElementById(`m-${MODE}`)?.classList.add("on");
|
|
for (const ms of [1000, 500]) {
|
|
const a = document.getElementById(`i-${ms}`);
|
|
a.href = `?mode=${MODE}&ms=${ms}`;
|
|
if (ms === MS) a.classList.add("on");
|
|
}
|
|
|
|
let slot = 0, bits = "", prevBits = "", switchedAt = 0, idx = 0;
|
|
// changed dashes wipe over a short time, a few frames are then unreadable, which is fine
|
|
// ?fade=0.5 tries a longer wipe, as a share of the interval
|
|
const FADE = params.get("fade") ? MS * +params.get("fade") : Math.min(220, MS * 0.28);
|
|
function tick() {
|
|
prevBits = bits || null;
|
|
switchedAt = performance.now();
|
|
slot = (slot + 1) & 0xffff;
|
|
const mac = [...crypto.getRandomValues(new Uint8Array(4))];
|
|
bits = SYNC + dataBits(rsEncode([(slot >> 8) & 255, slot & 255, ...mac]));
|
|
idx = (idx + 1) % lab.classic.length;
|
|
document.getElementById("info").textContent = MODE === "ring" ? `code ${slot}` : `code ${lab.classic[idx].token.split(".")[0].slice(-4)}`;
|
|
}
|
|
tick();
|
|
setInterval(tick, MS);
|
|
|
|
function frame(t) {
|
|
const dpr = devicePixelRatio || 1, w = innerWidth * dpr, h = innerHeight * dpr;
|
|
if (cv.width !== w || cv.height !== h) { cv.width = w; cv.height = h; }
|
|
ctx.fillStyle = "#f5f6fa"; ctx.fillRect(0, 0, w, h);
|
|
const size = Math.min(w, h) * 0.9;
|
|
if (MODE === "ring") {
|
|
// one full turn every 45 s
|
|
const k = prevBits ? Math.min(1, (t - switchedAt) / FADE) : 1;
|
|
render(ctx, w / 2, h / 2, size / (2 * DISC), (t / 1000) * (2 * Math.PI / 45), bits, t / 1000, prevBits ? { from: prevBits, k, seed: slot } : null);
|
|
} else {
|
|
const m = lab.classic[idx].modules, n = m.length, s = Math.floor(size / (n + 8));
|
|
const x0 = (w - n * s) / 2, y0 = (h - n * s) / 2;
|
|
ctx.fillStyle = "#fff"; ctx.fillRect(x0 - 4 * s, y0 - 4 * s, (n + 8) * s, (n + 8) * s);
|
|
ctx.fillStyle = "#000";
|
|
for (let y = 0; y < n; y++) for (let x = 0; x < n; x++) if (m[y][x] === "1") ctx.fillRect(x0 + x * s, y0 + y * s, s, s);
|
|
}
|
|
requestAnimationFrame(frame);
|
|
}
|
|
requestAnimationFrame(frame);
|