Ring code: changed dashes wipe from a random end instead of cross-fading

This commit is contained in:
2026-11-16 22:50:17 +01:00
parent 413ea35bf6
commit 4584e516ff
3 changed files with 28 additions and 9 deletions
+23 -5
View File
@@ -168,7 +168,8 @@ function logo(ctx, cx, cy, r) {
// deterministic per-dash phase so every dash breathes on its own rhythm // deterministic per-dash phase so every dash breathes on its own rhythm
const phase = (k, j) => ((Math.sin(k * 12.9898 + j * 78.233) * 43758.5453) % 1 + 1) % 1; const phase = (k, j) => ((Math.sin(k * 12.9898 + j * 78.233) * 43758.5453) % 1 + 1) % 1;
// fade: { from: previous bits, k: 0..1 } while the code switches, so changed dashes cross-fade // fade: { from: previous bits, k: 0..1, seed } while the code switches. a changed dash is wiped:
// one edge crosses it from a random end, the new tone fills behind it while the old one empties ahead
export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) { export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
ctx.fillStyle = "#eef0f6"; ctx.fillStyle = "#eef0f6";
ctx.beginPath(); ctx.arc(cx, cy, DISC * unit, 0, 2 * Math.PI); ctx.fill(); ctx.beginPath(); ctx.arc(cx, cy, DISC * unit, 0, 2 * Math.PI); ctx.fill();
@@ -179,6 +180,7 @@ export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
const ease = fade ? fade.k * fade.k * (3 - 2 * fade.k) : 1; const ease = fade ? fade.k * fade.k * (3 - 2 * fade.k) : 1;
ctx.lineCap = "round"; ctx.lineCap = "round";
ctx.lineWidth = THICK * unit; ctx.lineWidth = THICK * unit;
ctx.strokeStyle = INK;
ringsOf(bits).forEach((ring, k) => { ringsOf(bits).forEach((ring, k) => {
const n = ring.length, r = RADII[k] * unit, step = (2 * Math.PI) / n; const n = ring.length, r = RADII[k] * unit, step = (2 * Math.PI) / n;
const cap = (THICK * unit) / 2 / r; const cap = (THICK * unit) / 2 / r;
@@ -186,12 +188,28 @@ export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
const ph = phase(k, j) * 2 * Math.PI, w = 0.5 + 0.5 * Math.sin(t * 1.3 + ph), w2 = 0.5 + 0.5 * Math.sin(t * 0.9 + ph * 1.7); const ph = phase(k, j) * 2 * Math.PI, w = 0.5 + 0.5 * Math.sin(t * 1.3 + ph), w2 = 0.5 + 0.5 * Math.sin(t * 0.9 + ph * 1.7);
const len = LEN[0] + (LEN[1] - LEN[0]) * w; const len = LEN[0] + (LEN[1] - LEN[0]) * w;
const alphaOf = (bit) => { const [a0, a1] = bit === "1" ? ALPHA_DARK : ALPHA_LIGHT; return a0 + (a1 - a0) * w2; }; const alphaOf = (bit) => { const [a0, a1] = bit === "1" ? ALPHA_DARK : ALPHA_LIGHT; return a0 + (a1 - a0) * w2; };
const now = alphaOf(ring[j]), before = prev ? alphaOf(prev[k][j]) : now;
const half = Math.max(0.01, (step * len) / 2 - cap); const half = Math.max(0.01, (step * len) / 2 - cap);
const a = sectorAngle(theta, j, n) - Math.PI / 2; const a = sectorAngle(theta, j, n) - Math.PI / 2;
ctx.globalAlpha = before + (now - before) * ease; const dash = () => { ctx.beginPath(); ctx.arc(cx, cy, r, a - half, a + half); ctx.stroke(); };
ctx.strokeStyle = INK; if (!prev || prev[k][j] === ring[j]) {
ctx.beginPath(); ctx.arc(cx, cy, r, a - half, a + half); ctx.stroke(); ctx.globalAlpha = alphaOf(ring[j]);
dash();
continue;
}
// the edge runs cap to cap: the new tone grows as a bean from one end while the old one shrinks into
// the other, a bean shorter than its own width thins down to a dot instead of popping
const lo = a - half - cap, hi = a + half + cap, fwd = phase(k + 7, j + (fade.seed || 0) * 31) < 0.5;
const edge = fwd ? lo + (hi - lo) * ease : hi - (hi - lo) * ease;
const parts = [[alphaOf(ring[j]), fwd ? lo : edge, fwd ? edge : hi], [alphaOf(prev[k][j]), fwd ? edge : lo, fwd ? hi : edge]];
for (const [alpha, s0, s1] of parts) {
const span = s1 - s0, f = Math.min(1, span / (2 * cap));
if (f < 0.02) continue;
const c = cap * f;
ctx.globalAlpha = alpha;
ctx.lineWidth = THICK * unit * f;
ctx.beginPath(); ctx.arc(cx, cy, r, s0 + c, Math.max(s0 + c, s1 - c) + 1e-4); ctx.stroke();
}
ctx.lineWidth = THICK * unit;
} }
}); });
ctx.globalAlpha = 1; ctx.globalAlpha = 1;
+1 -1
View File
@@ -81,7 +81,7 @@ function ringLoop(t) {
const fadeMs = Math.min(220, qr.interval_ms * 0.25); const fadeMs = Math.min(220, qr.interval_ms * 0.25);
const k = prevBits ? Math.min(1, (t - switchedAt) / fadeMs) : 1; const k = prevBits ? Math.min(1, (t - switchedAt) / fadeMs) : 1;
drawRing(ctx, size / 2, size / 2, (size / 2) / DISC * 0.98, (t / 1000) * (2 * Math.PI / 45), qr.bits, t / 1000, drawRing(ctx, size / 2, size / 2, (size / 2) / DISC * 0.98, (t / 1000) * (2 * Math.PI / 45), qr.bits, t / 1000,
prevBits ? { from: prevBits, k } : null); prevBits ? { from: prevBits, k, seed: qr.slot } : null);
requestAnimationFrame(ringLoop); requestAnimationFrame(ringLoop);
} }
+4 -3
View File
@@ -12,8 +12,9 @@ for (const ms of [1000, 500]) {
} }
let slot = 0, bits = "", prevBits = "", switchedAt = 0, idx = 0; let slot = 0, bits = "", prevBits = "", switchedAt = 0, idx = 0;
// changed dashes cross-fade over a short time, a few frames are then unreadable, which is fine // changed dashes wipe over a short time, a few frames are then unreadable, which is fine
const FADE = Math.min(220, MS * 0.25); // ?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.25);
function tick() { function tick() {
prevBits = bits || null; prevBits = bits || null;
switchedAt = performance.now(); switchedAt = performance.now();
@@ -34,7 +35,7 @@ function frame(t) {
if (MODE === "ring") { if (MODE === "ring") {
// one full turn every 45 s // one full turn every 45 s
const k = prevBits ? Math.min(1, (t - switchedAt) / FADE) : 1; 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 } : null); 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 { } else {
const m = lab.classic[idx].modules, n = m.length, s = Math.floor(size / (n + 8)); 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; const x0 = (w - n * s) / 2, y0 = (h - n * s) / 2;