summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorwuhei <wuhei@users.noreply.github.com>2026-11-19 12:38:24 +0100
committerwuhei <wuhei@users.noreply.github.com>2026-11-19 12:38:24 +0100
commita5b77eb48974cfae45bac65ceb4880752444eb47 (patch)
treea71b85b54273a2d7893b90acfab43f52325e5db2 /web
parent10ee627143419d6e9c9186fbb0a023a34b08948e (diff)
downloadlilchinese-a5b77eb48974cfae45bac65ceb4880752444eb47.tar.gz
english subtitles from a local translation model
Optional second line under the Chinese, translated on the machine: - -translate-model spawns llama-server (free port, /health wait, SIGTERM on exit) and uses Hy-MT2-1.8B (Tencent Hunyuan MT) — a dedicated zh→en model, 30 tok/s on CPU, so a phrase lands in 0.5-0.8s - -translate points at an already running OpenAI-compatible endpoint (ollama, LM Studio, llama-server) - the English line is sticky rather than tied to the 9 characters on screen: no LLM can keep up with a slide that lives 0.3s, so the translation lags by about one model call and never blanks between phrases - -translate-window (28 chars) trades context for latency; translations cached - phrase text is debounced 200ms and coalesced latest-wins so growth does not queue up dozens of requests - display reserves the English line only when translation is enabled, so the Chinese layout is unchanged without it; autofit now fits both lines
Diffstat (limited to 'web')
-rw-r--r--web/index.html97
1 files changed, 72 insertions, 25 deletions
diff --git a/web/index.html b/web/index.html
index 4838047..5179370 100644
--- a/web/index.html
+++ b/web/index.html
@@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>lilchinese</title>
<style>
- :root { --pad-y: 7vh; --pad-x: 5vw; }
+ :root { --pad-y: 6vh; --pad-x: 5vw; }
html, body {
margin: 0; padding: 0; height: 100%; overflow: hidden;
background: #fff; color: #000; cursor: none;
@@ -17,7 +17,8 @@
padding: var(--pad-y) var(--pad-x);
display: flex; align-items: center; justify-content: center;
}
- #t {
+ #stack { display: flex; flex-direction: column; align-items: center; max-width: 100%; }
+ #zh {
font-weight: 900;
line-height: 1.04;
letter-spacing: .02em;
@@ -28,11 +29,24 @@
max-width: 100%;
will-change: font-size;
}
+ #en {
+ font-family: "Noto Sans", "Inter", "DejaVu Sans", sans-serif;
+ font-weight: 500;
+ line-height: 1.18;
+ text-align: center;
+ white-space: pre-wrap;
+ overflow-wrap: break-word;
+ max-width: 100%;
+ margin-top: .28em;
+ opacity: .88;
+ display: none;
+ }
+ body.en #en { display: block; }
@keyframes pop {
from { opacity: .5; transform: scale(.955); }
to { opacity: 1; transform: scale(1); }
}
- #t.pop { animation: pop 90ms ease-out; }
+ .pop { animation: pop 90ms ease-out; }
#d {
position: fixed; right: 12px; bottom: 10px; display: none;
font: 12px/1.4 ui-monospace, monospace; color: #bbb; letter-spacing: 0;
@@ -40,19 +54,25 @@
</style>
</head>
<body>
-<div id="wrap"><div id="t"></div></div>
+<div id="wrap"><div id="stack"><div id="zh"></div><div id="en"></div></div></div>
<div id="d"></div>
<script>
(function () {
const wrap = document.getElementById('wrap');
- const t = document.getElementById('t');
+ const stack = document.getElementById('stack');
+ const zh = document.getElementById('zh');
+ const en = document.getElementById('en');
const d = document.getElementById('d');
const q = new URLSearchParams(location.search);
const debug = q.has('debug');
+ const wantEN = q.has('en');
if (debug) d.style.display = 'block';
+ if (wantEN) document.body.classList.add('en');
- let cur = '';
+ let curZh = '', curEn = '';
let padW = 0, padH = 0;
+ const EN_RATIO = 0.30;
+ const EN_MAX = 120; // keep the English line from crowding out the Chinese
function measurePad() {
const cs = getComputedStyle(wrap);
@@ -60,36 +80,63 @@
padH = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
}
- // Largest font size at which the whole phrase still fits the screen.
+ function setSizes(s) {
+ zh.style.fontSize = s + 'px';
+ en.style.fontSize = Math.max(11, Math.round(s * EN_RATIO)) + 'px';
+ }
+
+ // Largest Chinese font size at which Chinese + English still fit the screen.
function fit() {
const boxW = wrap.clientWidth - padW;
const boxH = wrap.clientHeight - padH;
if (boxW <= 0 || boxH <= 0) return;
+ // Reserve the English line even before its translation lands, so the Chinese
+ // text does not jump in size when the translation appears.
+ const reserve = wantEN && !curEn;
+ if (reserve) en.textContent = '\u00a0';
let lo = 8, hi = Math.max(boxW, boxH) * 1.7;
for (let i = 0; i < 18; i++) {
const mid = (lo + hi) / 2;
- t.style.fontSize = mid + 'px';
- const r = t.getBoundingClientRect();
+ setSizes(mid);
+ const r = stack.getBoundingClientRect();
if (r.width <= boxW && r.height <= boxH) lo = mid; else hi = mid;
}
- t.style.fontSize = Math.floor(lo) + 'px';
+ setSizes(Math.floor(lo));
+ if (reserve) en.textContent = '';
}
- function render(text, cap) {
- if (!text || text === cur) return;
- const growing = cur.length > 0 && text.startsWith(cur);
- t.textContent = text;
- if (!growing) {
- // New phrase: slam it in. Refinements just update in place (no flicker).
- t.classList.remove('pop');
- void t.offsetWidth;
- t.classList.add('pop');
+ function render(text, translation, cap) {
+ if (!text) return;
+ const newPhrase = text !== curZh;
+ const growing = !newPhrase ? false
+ : (curZh.length > 0 && text.startsWith(curZh));
+
+ if (newPhrase) {
+ curZh = text;
+ zh.textContent = text;
+ if (!growing) {
+ // New phrase: slam it in. Refinements just update in place (no flicker).
+ zh.classList.remove('pop');
+ void zh.offsetWidth;
+ zh.classList.add('pop');
+ }
+ }
+ let tr = translation || '';
+ if (tr.length > EN_MAX) tr = tr.slice(0, EN_MAX - 1).trimEnd() + '\u2026';
+ if (tr !== curEn) {
+ curEn = tr;
+ en.textContent = tr;
+ if (tr && newPhrase) {
+ en.classList.remove('pop');
+ void en.offsetWidth;
+ en.classList.add('pop');
+ }
}
- cur = text;
fit();
if (debug) {
const lat = cap ? (Date.now() - cap) : 0;
- d.textContent = text.length + '\u5b57 ' + (cap ? lat + 'ms' : '');
+ d.textContent = text.length + '\u5b57 ' + (cap ? lat + 'ms' : '') +
+ (tr ? ' en:' + tr.length : '');
}
}
@@ -103,8 +150,8 @@
measurePad();
- if (q.has('text')) {
- render(q.get('text'), 0); // design preview: index.html?text=你好世界
+ if (q.has('text')) { // design preview: index.html?en=1&text=你好&t=hello
+ render(q.get('text'), q.get('t') || '', 0);
return;
}
@@ -112,10 +159,10 @@
es.onmessage = function (e) {
try {
const s = JSON.parse(e.data);
- render(s.text, s.cap);
+ render(s.text, s.en, s.cap);
} catch (_) {}
};
- es.onerror = function () { /* EventSource reconnects on its own; keep the last phrase up */ };
+ es.onerror = function () { /* EventSource reconnects on its own; keep the phrase up */ };
})();
</script>
</body>