zeichnen.js legt eine SVG-Flaeche genau ueber den Bildinhalt des Spielbilds (object-fit beruecksichtigt), Werkzeugleiste nur fuer die Regie im laufenden Zockabend, Maus und Finger ueber Pointer Events, Tippziele 44 px bei pointer: coarse. Verblassen nur ueber opacity. Die Buehnenquelle wird im Zockabend durchsichtig und zeigt nur die Zeichnung; buehne.js reicht Stand und Zeichnung als Ereignis weiter. Co-Authored-By: Claude Opus 5.5 <[email protected]>
509 lines
21 KiB
JavaScript
509 lines
21 KiB
JavaScript
/* =====================================================================
|
||
ZEICHNEN AUF DEM SPIELBILD (04.11.2026, Neuplan M5)
|
||
|
||
Die Regie (DogFather und VanVan, gleichberechtigt) zeichnet Pfeile,
|
||
Kreise und kurze Texte aufs Spielbild. Alle im Saal sehen sie an
|
||
derselben Stelle des Bildes, ebenso die Bühnenquelle für OBS
|
||
(buehne.html). Nach acht Sekunden verblassen sie; „halten“ lässt sie
|
||
stehen, „weg“ räumt alles ab.
|
||
|
||
DIESELBE DATEI FÜR ZWEI SEITEN:
|
||
* Saal (reaktion.html): Stand über `saal:lage`, Änderungen über
|
||
`saal:strom` (was: "zeichnung"). Die Zeichenfläche liegt genau
|
||
über dem BILDINHALT des Spielbilds – nicht über der Kachel. Ein
|
||
Spielbild mit schwarzen Rändern (object-fit: contain) hätte sonst
|
||
je nach Fenstergröße andere Ränder, und der Pfeil zeigte auf
|
||
einem Handy woandershin als am Rechner.
|
||
* Bühnenquelle (buehne.html, `body.obs-buehne`): Stand über
|
||
`buehne:stand`, Änderungen über `buehne:zeichnung`. Die Fläche ist
|
||
das ganze Fenster; im Zockabend wird die Quelle durchsichtig und
|
||
liegt in OBS über der Spielaufnahme (gleiche Größe).
|
||
|
||
ÜBERTRAGEN WERDEN ANTEILE (0..1) DER BILDFLÄCHE, keine Bilder. Jeder
|
||
Bildschirm rechnet sie in seine Pixel um und zeichnet scharf (SVG).
|
||
|
||
NUR BEIM ZOCKEN. Bei einer Reaction entsteht hier nichts: YouTubes
|
||
Entwicklerregeln verbieten Einblendungen über dem Player. Der Server
|
||
lehnt dort ohnehin ab – die Seite zeigt das Werkzeug gar nicht erst.
|
||
|
||
Bewegt wird nur `opacity` (Verblassen); bei `prefers-reduced-motion`
|
||
ohne Übergang.
|
||
===================================================================== */
|
||
(() => {
|
||
'use strict';
|
||
|
||
const SVG = 'http://www.w3.org/2000/svg';
|
||
const API = '/workspace/api/reaktion/zeichnen';
|
||
const VERBLASSEN_MS = 900;
|
||
const obs = document.body.classList.contains('obs-buehne');
|
||
const $ = (id) => document.getElementById(id);
|
||
|
||
let aktiv = false; // Zockabend live (Saal) bzw. art=spiel (Quelle)
|
||
let regie = false; // darf zeichnen (nur im Saal, nicht im Bühnenmodus)
|
||
let halten = false;
|
||
let werkzeug = ''; // '' | 'pfeil' | 'kreis' | 'text'
|
||
let offen = false; // Werkzeugleiste aufgeklappt
|
||
let geholtAm = 0;
|
||
const formen = new Map(); // id -> { form, g, uhr }
|
||
|
||
/* ---- Bausteine ---------------------------------------------------- */
|
||
const flaeche = document.createElement('div');
|
||
flaeche.className = 'zeichnung';
|
||
flaeche.id = 'zeichnung';
|
||
flaeche.hidden = true;
|
||
const bild = document.createElementNS(SVG, 'svg');
|
||
bild.setAttribute('class', 'zeichnung__bild');
|
||
bild.setAttribute('aria-hidden', 'true');
|
||
const vorschau = document.createElementNS(SVG, 'g');
|
||
vorschau.setAttribute('class', 'zeichnung__form zeichnung__form--vorschau');
|
||
bild.append(vorschau);
|
||
flaeche.append(bild);
|
||
|
||
/* Die Fangfläche für Maus und Finger – nur, wenn ein Werkzeug gewählt
|
||
ist. Sonst geht jeder Klick durch, wie bisher. */
|
||
const fang = document.createElement('div');
|
||
fang.className = 'zeichnung__fang';
|
||
fang.hidden = true;
|
||
flaeche.append(fang);
|
||
|
||
const textform = document.createElement('form');
|
||
textform.className = 'zeichnung__textfeld';
|
||
textform.hidden = true;
|
||
const textein = document.createElement('input');
|
||
textein.type = 'text';
|
||
textein.maxLength = 40;
|
||
textein.setAttribute('aria-label', 'Text für das Spielbild (höchstens 40 Zeichen)');
|
||
textein.placeholder = 'Kurzer Text …';
|
||
const textok = document.createElement('button');
|
||
textok.type = 'submit';
|
||
textok.className = 'zeichnung__knopf';
|
||
textok.textContent = 'Setzen';
|
||
textform.append(textein, textok);
|
||
flaeche.append(textform);
|
||
|
||
const leiste = document.createElement('div');
|
||
leiste.className = 'zeichenwerkzeug';
|
||
leiste.id = 'zeichenwerkzeug';
|
||
leiste.setAttribute('role', 'toolbar');
|
||
leiste.setAttribute('aria-label', 'Auf dem Spielbild zeichnen');
|
||
leiste.hidden = true;
|
||
const ZEICHEN = {
|
||
stift: '<path d="M4 20l4-1 11-11-3-3L5 16z"/><path d="M14 6l3 3"/>',
|
||
pfeil: '<path d="M5 19L19 5"/><path d="M10 5h9v9"/>',
|
||
kreis: '<circle cx="12" cy="12" r="7.5"/>',
|
||
text: '<path d="M5 6h14M12 6v13"/>',
|
||
halten: '<path d="M9 3.5h6l-1 5.5 3 3H7l3-3z"/><path d="M12 12v8.5"/>',
|
||
weg: '<path d="M5 7h14M9 7V4h6v3M7 7l1 13h8l1-13"/>',
|
||
fertig: '<path d="M5 12.5l4.5 4.5L19 7.5"/>',
|
||
};
|
||
function knopf(art, wort, titel) {
|
||
const b = document.createElement('button');
|
||
b.type = 'button';
|
||
b.className = 'zeichnung__knopf';
|
||
b.dataset.art = art;
|
||
b.setAttribute('aria-label', titel);
|
||
b.title = titel;
|
||
b.innerHTML = `<svg viewBox="0 0 24 24" aria-hidden="true">${ZEICHEN[art]}</svg><span>${wort}</span>`;
|
||
return b;
|
||
}
|
||
const kStift = knopf('stift', 'Zeichnen', 'Zeichenwerkzeug öffnen');
|
||
kStift.setAttribute('aria-expanded', 'false');
|
||
const kPfeil = knopf('pfeil', 'Pfeil', 'Pfeil ziehen');
|
||
const kKreis = knopf('kreis', 'Kreis', 'Kreis ziehen');
|
||
const kText = knopf('text', 'Text', 'Text setzen');
|
||
const kHalten = knopf('halten', 'Halten', 'Zeichnungen stehen lassen');
|
||
const kWeg = knopf('weg', 'Weg', 'Alle Zeichnungen wegräumen');
|
||
const werkzeuge = document.createElement('div');
|
||
werkzeuge.className = 'zeichenwerkzeug__wahl';
|
||
werkzeuge.hidden = true;
|
||
for (const b of [kPfeil, kKreis, kText]) b.setAttribute('aria-pressed', 'false');
|
||
kHalten.setAttribute('aria-pressed', 'false');
|
||
werkzeuge.append(kPfeil, kKreis, kText, kHalten, kWeg);
|
||
const hinweis = document.createElement('span');
|
||
hinweis.className = 'zeichenwerkzeug__hinweis';
|
||
hinweis.setAttribute('role', 'status');
|
||
leiste.append(kStift, werkzeuge, hinweis);
|
||
flaeche.append(leiste);
|
||
|
||
function einhaengen() {
|
||
if (flaeche.isConnected) return true;
|
||
const ziel = obs ? document.body : $('leinwand');
|
||
if (!ziel) return false;
|
||
ziel.append(flaeche);
|
||
return true;
|
||
}
|
||
|
||
/* ---- Wo liegt der Bildinhalt? -------------------------------------
|
||
Gemessen am Video des Spielbilds und seinem `object-fit`. Kennt die
|
||
Seite das Bildformat noch nicht (der Host sieht sein eigenes Bild im
|
||
Zockabend bewusst nicht, siehe reaktion.js), gilt 16:9 – das Format,
|
||
in dem geteilt wird. */
|
||
function spielRechteck() {
|
||
if (obs) return { left: 0, top: 0, width: innerWidth, height: innerHeight };
|
||
const leinwand = $('leinwand');
|
||
const fenster = document.querySelector('#kameras .kamera[data-host="ja"]');
|
||
if (!leinwand || !fenster) return null;
|
||
const v = fenster.querySelector('video');
|
||
const lr = leinwand.getBoundingClientRect();
|
||
const quelle = v && v.getBoundingClientRect().width > 0 ? v : fenster;
|
||
const r = quelle.getBoundingClientRect();
|
||
if (r.width < 20 || r.height < 20) return null;
|
||
let x = r.left; let y = r.top; let w = r.width; let h = r.height;
|
||
const vw = v && v.videoWidth > 0 ? v.videoWidth : 16;
|
||
const vh = v && v.videoHeight > 0 ? v.videoHeight : 9;
|
||
const fit = v && quelle === v ? getComputedStyle(v).objectFit : 'contain';
|
||
if (fit !== 'fill') {
|
||
const s = fit === 'cover' ? Math.max(w / vw, h / vh) : Math.min(w / vw, h / vh);
|
||
const cw = vw * s; const ch = vh * s;
|
||
x += (w - cw) / 2; y += (h - ch) / 2; w = cw; h = ch;
|
||
}
|
||
return { left: x - lr.left, top: y - lr.top, width: w, height: h };
|
||
}
|
||
|
||
let W = 0; let H = 0;
|
||
function legen() {
|
||
if (!aktiv || !einhaengen()) return;
|
||
const r = spielRechteck();
|
||
if (!r) { flaeche.hidden = true; return; }
|
||
flaeche.hidden = false;
|
||
if (!obs) {
|
||
flaeche.style.left = `${Math.round(r.left)}px`;
|
||
flaeche.style.top = `${Math.round(r.top)}px`;
|
||
flaeche.style.width = `${Math.round(r.width)}px`;
|
||
flaeche.style.height = `${Math.round(r.height)}px`;
|
||
}
|
||
const nw = Math.max(1, Math.round(r.width));
|
||
const nh = Math.max(1, Math.round(r.height));
|
||
if (nw !== W || nh !== H) {
|
||
W = nw; H = nh;
|
||
bild.setAttribute('viewBox', `0 0 ${W} ${H}`);
|
||
for (const e of formen.values()) malen(e.g, e.form);
|
||
}
|
||
leistePlatzieren();
|
||
}
|
||
|
||
/* ---- Wohin mit der Leiste? -----------------------------------------
|
||
Auf dem Spielbild liegen schon andere Bedienteile – am Handy die
|
||
Senderleiste quer über der Mitte, am Rechner Fährte, Kontrollbild,
|
||
Gästefenster. Statt einer Liste dieser Teile (die beim nächsten
|
||
Umbau still veraltet) wird GEMESSEN: Die Leiste probiert Ecken und
|
||
Kanten des Bildes durch und nimmt die erste, an der sie überall
|
||
selbst getroffen wird (`elementFromPoint`) – also nichts über ihr
|
||
liegt. Findet sie keine, bleibt sie oben mittig. */
|
||
function leistePlatzieren() {
|
||
if (leiste.hidden || flaeche.hidden || obs) return;
|
||
const lw = leiste.offsetWidth; const lh = leiste.offsetHeight;
|
||
if (!lw || !lh || !W || !H) return;
|
||
const rand = 6;
|
||
const mx = Math.max(rand, (W - lw) / 2);
|
||
const plaetze = [
|
||
[mx, rand], [W - lw - rand, rand], [rand, rand],
|
||
[W - lw - rand, H - lh - rand], [rand, H - lh - rand], [mx, H - lh - rand],
|
||
];
|
||
const frei = () => {
|
||
const r = leiste.getBoundingClientRect();
|
||
for (let i = 0; i < 5; i++) {
|
||
for (let j = 0; j < 2; j++) {
|
||
const e = document.elementFromPoint(r.left + 3 + (r.width - 6) * (i / 4), r.top + 3 + (r.height - 6) * j);
|
||
if (!e || !leiste.contains(e)) return false;
|
||
}
|
||
}
|
||
return true;
|
||
};
|
||
for (const [x, y] of plaetze) {
|
||
leiste.style.left = `${Math.round(Math.max(0, x))}px`;
|
||
leiste.style.top = `${Math.round(Math.max(0, y))}px`;
|
||
if (frei()) return;
|
||
}
|
||
leiste.style.left = `${Math.round(mx)}px`;
|
||
leiste.style.top = `${rand}px`;
|
||
}
|
||
|
||
/* ---- Zeichnen ----------------------------------------------------- */
|
||
function el(art, attrs) {
|
||
const n = document.createElementNS(SVG, art);
|
||
for (const [k, v] of Object.entries(attrs)) n.setAttribute(k, String(v));
|
||
return n;
|
||
}
|
||
/** Malt eine Form in Pixeln dieser Fläche – zweimal: dunkler Saum
|
||
* darunter, Farbe darüber. So bleibt sie auf hellem Himmel wie auf
|
||
* dunkler Straße lesbar, ohne grell zu sein. */
|
||
function malen(g, f) {
|
||
g.textContent = '';
|
||
const strich = Math.max(3, Math.min(7, W * 0.0045));
|
||
const px = (p) => [p[0] * W, p[1] * H];
|
||
const schichten = (bauen) => {
|
||
const saum = bauen();
|
||
saum.setAttribute('class', 'zeichnung__saum');
|
||
saum.setAttribute('stroke-width', String(strich + 4));
|
||
const farbe = bauen();
|
||
farbe.setAttribute('class', 'zeichnung__farbe');
|
||
farbe.setAttribute('stroke-width', String(strich));
|
||
g.append(saum, farbe);
|
||
};
|
||
if (f.art === 'pfeil') {
|
||
const [x1, y1] = px(f.von); const [x2, y2] = px(f.nach);
|
||
const kopf = Math.max(12, Math.min(30, W * 0.022));
|
||
const wkl = Math.atan2(y2 - y1, x2 - x1);
|
||
const sp = (d) => [x2 - kopf * Math.cos(wkl + d), y2 - kopf * Math.sin(wkl + d)];
|
||
const [ax, ay] = sp(0.5); const [bx, by] = sp(-0.5);
|
||
schichten(() => el('path', { d: `M${x1} ${y1}L${x2} ${y2}M${ax} ${ay}L${x2} ${y2}L${bx} ${by}`, fill: 'none' }));
|
||
} else if (f.art === 'kreis') {
|
||
const [cx, cy] = px(f.mitte); const [rx, ry] = px(f.rand);
|
||
const r = Math.hypot(rx - cx, ry - cy);
|
||
schichten(() => el('circle', { cx, cy, r, fill: 'none' }));
|
||
} else if (f.art === 'text') {
|
||
const [tx, ty] = px(f.punkt);
|
||
const groesse = Math.max(14, Math.min(40, H * 0.05));
|
||
const t = el('text', { x: tx, y: ty, 'font-size': groesse, 'dominant-baseline': 'middle' });
|
||
t.setAttribute('class', 'zeichnung__text');
|
||
t.setAttribute('stroke-width', String(Math.max(3, groesse * 0.16)));
|
||
/* Am rechten Rand rückwärts ausrichten, sonst läuft der Text aus
|
||
dem Bild. */
|
||
t.setAttribute('text-anchor', f.punkt[0] > 0.7 ? 'end' : (f.punkt[0] < 0.3 ? 'start' : 'middle'));
|
||
t.textContent = f.text;
|
||
g.append(t);
|
||
}
|
||
}
|
||
|
||
function entfernen(id, sanft) {
|
||
const e = formen.get(id);
|
||
if (!e) return;
|
||
clearTimeout(e.uhr);
|
||
formen.delete(id);
|
||
if (!sanft) { e.g.remove(); return; }
|
||
e.g.classList.add('zeichnung__form--weg');
|
||
setTimeout(() => e.g.remove(), VERBLASSEN_MS + 50);
|
||
}
|
||
|
||
/** Den Stand übernehmen: Neues malen, Fehlendes verblassen lassen,
|
||
* jedem seine Uhr neu stellen (halten/loslassen ändert sie). */
|
||
function standAnwenden(stand) {
|
||
if (!stand || !Array.isArray(stand.formen)) return;
|
||
halten = stand.halten === true;
|
||
kHalten.setAttribute('aria-pressed', halten ? 'true' : 'false');
|
||
if (!aktiv) { for (const id of [...formen.keys()]) entfernen(id, false); return; }
|
||
legen();
|
||
const da = new Set();
|
||
for (const f of stand.formen) {
|
||
if (!f || typeof f.id !== 'number') continue;
|
||
da.add(f.id);
|
||
let e = formen.get(f.id);
|
||
if (!e) {
|
||
const g = el('g', {});
|
||
g.setAttribute('class', 'zeichnung__form');
|
||
g.dataset.art = f.art;
|
||
g.dataset.id = String(f.id);
|
||
bild.insertBefore(g, vorschau);
|
||
e = { form: f, g, uhr: 0 };
|
||
formen.set(f.id, e);
|
||
malen(g, f);
|
||
}
|
||
clearTimeout(e.uhr);
|
||
if (typeof f.rest === 'number') {
|
||
const id = f.id;
|
||
e.uhr = setTimeout(() => entfernen(id, true), Math.max(0, f.rest));
|
||
}
|
||
}
|
||
for (const id of [...formen.keys()]) if (!da.has(id)) entfernen(id, true);
|
||
}
|
||
|
||
async function holen() {
|
||
if (obs || !aktiv) return;
|
||
geholtAm = Date.now();
|
||
try {
|
||
const a = await fetch(API, { credentials: 'same-origin', cache: 'no-store' });
|
||
const j = await a.json().catch(() => null);
|
||
if (a.ok && j) standAnwenden(j);
|
||
} catch { /* der nächste Takt holt es nach */ }
|
||
}
|
||
|
||
/* ---- Bedienung (nur Regie) ---------------------------------------- */
|
||
let hinweisUhr = 0;
|
||
function sag(text) {
|
||
hinweis.textContent = text;
|
||
clearTimeout(hinweisUhr);
|
||
hinweisUhr = setTimeout(() => { hinweis.textContent = ''; }, 3500);
|
||
}
|
||
async function senden(pfad, rumpf) {
|
||
try {
|
||
const a = await fetch(API + pfad, {
|
||
method: 'POST', credentials: 'same-origin',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(rumpf || {}),
|
||
});
|
||
const j = await a.json().catch(() => null);
|
||
if (a.ok && j) { standAnwenden(j); return true; }
|
||
const grund = j?.fehler;
|
||
sag(grund === 'zu_schnell' ? 'Kurz durchatmen – zu viel auf einmal.'
|
||
: grund === 'nur_beim_zocken' ? 'Zeichnen geht nur, solange der Zockabend live ist.'
|
||
: grund === 'text_zu_lang' ? 'Höchstens 40 Zeichen.'
|
||
: grund === 'zu_klein' ? 'Etwas größer ziehen.'
|
||
: 'Das ging gerade nicht.');
|
||
} catch { sag('Das ging gerade nicht.'); }
|
||
return false;
|
||
}
|
||
|
||
function werkzeugSetzen(neu) {
|
||
werkzeug = neu;
|
||
for (const b of [kPfeil, kKreis, kText]) b.setAttribute('aria-pressed', b.dataset.art === neu ? 'true' : 'false');
|
||
fang.hidden = !neu;
|
||
fang.dataset.art = neu || '';
|
||
flaeche.dataset.werkzeug = neu || '';
|
||
if (neu !== 'text') textform.hidden = true;
|
||
vorschau.textContent = '';
|
||
}
|
||
function leisteZeichnen() {
|
||
leiste.hidden = !(aktiv && regie);
|
||
if (leiste.hidden) { offen = false; werkzeugSetzen(''); }
|
||
werkzeuge.hidden = !offen;
|
||
kStift.setAttribute('aria-expanded', offen ? 'true' : 'false');
|
||
kStift.querySelector('span').textContent = offen ? 'Fertig' : 'Zeichnen';
|
||
kStift.querySelector('svg').innerHTML = offen ? ZEICHEN.fertig : ZEICHEN.stift;
|
||
kStift.setAttribute('aria-label', offen ? 'Fertig – Zeichenwerkzeug schließen' : 'Zeichenwerkzeug öffnen');
|
||
kStift.title = kStift.getAttribute('aria-label');
|
||
/* Solange gezeichnet wird, liegt die Fläche ÜBER den anderen
|
||
Bedienteilen des Bildes (die dann blass und ohne Wirkung sind) –
|
||
sonst wäre am Handy die halbe Bildfläche nicht zu erreichen.
|
||
Nur auf diesem einen Bildschirm; „Fertig“ stellt alles zurück. */
|
||
flaeche.dataset.offen = offen ? 'ja' : 'nein';
|
||
const l = $('leinwand');
|
||
if (l) { if (offen) l.dataset.zeichnen = 'an'; else delete l.dataset.zeichnen; }
|
||
leistePlatzieren();
|
||
}
|
||
|
||
kStift.addEventListener('click', () => {
|
||
offen = !offen;
|
||
if (!offen) werkzeugSetzen('');
|
||
else if (!werkzeug) werkzeugSetzen('pfeil');
|
||
leisteZeichnen();
|
||
});
|
||
for (const b of [kPfeil, kKreis, kText]) {
|
||
b.addEventListener('click', () => werkzeugSetzen(werkzeug === b.dataset.art ? '' : b.dataset.art));
|
||
}
|
||
kHalten.addEventListener('click', () => senden('/halten', { an: !halten }));
|
||
kWeg.addEventListener('click', () => senden('/weg', {}));
|
||
|
||
const anteil = (e) => {
|
||
const r = fang.getBoundingClientRect();
|
||
const k = (z) => Math.round(Math.min(1, Math.max(0, z)) * 10000) / 10000;
|
||
return [k((e.clientX - r.left) / r.width), k((e.clientY - r.top) / r.height)];
|
||
};
|
||
function formAus(art, a, b) {
|
||
if (art === 'pfeil') return { art, von: a, nach: b };
|
||
if (art === 'kreis') return { art, mitte: a, rand: b };
|
||
return null;
|
||
}
|
||
let start = null;
|
||
let textStelle = null;
|
||
fang.addEventListener('pointerdown', (e) => {
|
||
if (!werkzeug || e.button > 0) return;
|
||
e.preventDefault();
|
||
try { fang.setPointerCapture(e.pointerId); } catch { /* egal */ }
|
||
start = { id: e.pointerId, p: anteil(e) };
|
||
});
|
||
fang.addEventListener('pointermove', (e) => {
|
||
if (!start || start.id !== e.pointerId || werkzeug === 'text') return;
|
||
const f = formAus(werkzeug, start.p, anteil(e));
|
||
if (f) malen(vorschau, f);
|
||
});
|
||
function ende(e, abbrechen) {
|
||
if (!start || start.id !== e.pointerId) return;
|
||
const a = start.p; const b = anteil(e);
|
||
start = null;
|
||
vorschau.textContent = '';
|
||
if (abbrechen) return;
|
||
if (werkzeug === 'text') {
|
||
textStelle = b;
|
||
textform.hidden = false;
|
||
textform.style.left = `${Math.round(b[0] * 100)}%`;
|
||
textform.style.top = `${Math.round(b[1] * 100)}%`;
|
||
textform.dataset.seite = b[0] > 0.6 ? 'links' : 'rechts';
|
||
textform.dataset.hoehe = b[1] > 0.7 ? 'oben' : 'unten';
|
||
textein.value = '';
|
||
textein.focus();
|
||
return;
|
||
}
|
||
let ziel = b;
|
||
/* Ein Tipp statt eines Zugs: beim Kreis ein Kreis mittlerer Größe
|
||
um die Stelle, beim Pfeil ein kurzer Pfeil, der von links oben
|
||
auf die Stelle zeigt – ein Finger zieht nicht immer. */
|
||
if (Math.hypot(a[0] - b[0], a[1] - b[1]) < 0.012) {
|
||
if (werkzeug === 'kreis') ziel = [Math.min(1, a[0] + 0.05), a[1]];
|
||
else {
|
||
senden('', formAus('pfeil', [Math.max(0, a[0] - 0.08), Math.max(0, a[1] - 0.1)], a));
|
||
return;
|
||
}
|
||
}
|
||
senden('', formAus(werkzeug, a, ziel));
|
||
}
|
||
fang.addEventListener('pointerup', (e) => ende(e, false));
|
||
fang.addEventListener('pointercancel', (e) => ende(e, true));
|
||
textform.addEventListener('submit', async (e) => {
|
||
e.preventDefault();
|
||
const text = textein.value.trim();
|
||
if (!text || !textStelle) { textform.hidden = true; return; }
|
||
if (await senden('', { art: 'text', punkt: textStelle, text })) textform.hidden = true;
|
||
});
|
||
textein.addEventListener('keydown', (e) => {
|
||
if (e.key === 'Escape') { e.stopPropagation(); textform.hidden = true; }
|
||
});
|
||
document.addEventListener('keydown', (e) => {
|
||
if (e.key === 'Escape' && werkzeug && textform.hidden) werkzeugSetzen('');
|
||
});
|
||
|
||
/* ---- Lage --------------------------------------------------------- */
|
||
function zustand(neuAktiv, neuRegie) {
|
||
const vorher = aktiv;
|
||
aktiv = neuAktiv;
|
||
regie = neuRegie && !obs && document.body.dataset.nur !== 'buehne';
|
||
flaeche.dataset.regie = regie ? 'ja' : 'nein';
|
||
if (!aktiv) {
|
||
flaeche.hidden = true;
|
||
for (const id of [...formen.keys()]) entfernen(id, false);
|
||
} else {
|
||
legen();
|
||
}
|
||
leisteZeichnen();
|
||
return !vorher && aktiv;
|
||
}
|
||
|
||
if (obs) {
|
||
window.addEventListener('buehne:stand', (e) => {
|
||
const s = e.detail || {};
|
||
const spiel = s.art === 'spiel';
|
||
document.documentElement.dataset.art = spiel ? 'spiel' : 'video';
|
||
zustand(spiel && s.stand === 'live', false);
|
||
if (s.zeichnung) standAnwenden(s.zeichnung);
|
||
});
|
||
window.addEventListener('buehne:zeichnung', (e) => standAnwenden(e.detail));
|
||
} else {
|
||
window.addEventListener('saal:lage', (e) => {
|
||
const l = e.detail;
|
||
const neu = zustand(!!l && l.art === 'spiel' && l.stand === 'live', !!l?.ich?.host);
|
||
/* Beim Eintritt in den Zockabend und danach alle 15 s bei einer
|
||
ohnehin eintreffenden Lage: So heilt sich die Seite nach einem
|
||
Abriss des Stroms selbst, ohne eigenen Takt. */
|
||
if (aktiv && (neu || Date.now() - geholtAm > 15000)) holen();
|
||
});
|
||
window.addEventListener('saal:strom', (e) => {
|
||
if (e.detail?.was === 'zeichnung') standAnwenden(e.detail);
|
||
});
|
||
}
|
||
|
||
/* Größe und Lage folgen dem Spielbild: Fenstergröße, Drehen des
|
||
Handys, neue Kachel, Bildformat des Videos. */
|
||
addEventListener('resize', () => legen());
|
||
if ('ResizeObserver' in window) {
|
||
const ro = new ResizeObserver(() => legen());
|
||
const l = $('leinwand');
|
||
if (l) ro.observe(l);
|
||
}
|
||
document.addEventListener('loadedmetadata', () => legen(), true);
|
||
document.addEventListener('resize', () => legen(), true);
|
||
setInterval(() => { if (aktiv && document.visibilityState === 'visible') legen(); }, 2000);
|
||
document.addEventListener('visibilitychange', () => {
|
||
if (document.visibilityState === 'visible' && aktiv) holen();
|
||
});
|
||
})();
|