Chat: paper slips under the camera, Twitch + 7TV emotes
- public/chat/chat.css + chat.js (window.HSChat): cream paper slips with the name on a washi-tape label, newest on top sliding out from under the webcam, older ones glide down and fade at the bottom; message_timeout / max_messages - Emotes: Twitch (tags.emotes) and 7TV global + channel sets (refreshed every 10 min), zero-width 7TV emotes stack, emote-only messages get bigger emotes - Deleted messages, timeouts, bans and /clear remove slips at once - overlay.ejs: old chat CSS/JS removed, new files load only for ?parts=chat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0cc53e1d73
commit
463c6580f4
4 changed files with 264 additions and 55 deletions
71
public/chat/chat.css
Normal file
71
public/chat/chat.css
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/* Chat: cream paper slips with the name on a washi-tape label ("style 5" from the chat preview).
|
||||
The box sits under the webcam (Game scene: camera group at 1920,600, 640x360 on the 2560 canvas
|
||||
= 1440,450 480x270 on this 1920x1080 page). Newest message on top, slides out from under the camera,
|
||||
older ones are pushed down and dissolve at the bottom edge.
|
||||
Move or resize it with Custom CSS in Settings, e.g. #chat { --chat-top: 60%; --chat-height: 37%; } */
|
||||
#chat {
|
||||
--chat-right: 2%;
|
||||
--chat-top: 63%; /* 680px: just above the bottom of the camera ring, so messages come out from under it */
|
||||
--chat-width: 20%;
|
||||
--chat-height: 34%; /* down to the bottom edge */
|
||||
--ink: #3a2a20;
|
||||
--cream: #fffdf7;
|
||||
position: fixed;
|
||||
right: var(--chat-right);
|
||||
top: var(--chat-top);
|
||||
width: var(--chat-width);
|
||||
height: var(--chat-height);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: flex-start;
|
||||
gap: 8px;
|
||||
padding: 26px 8px 0;
|
||||
overflow: hidden;
|
||||
-webkit-mask-image: linear-gradient(transparent 0, #000 30px, #000 calc(100% - 60px), transparent);
|
||||
mask-image: linear-gradient(transparent 0, #000 30px, #000 calc(100% - 60px), transparent);
|
||||
}
|
||||
|
||||
#chat .msg {
|
||||
position: relative;
|
||||
flex: none;
|
||||
margin: 9px 6px 0;
|
||||
padding: 19px 14px 9px;
|
||||
border-radius: 2px;
|
||||
background: var(--cream);
|
||||
color: var(--ink);
|
||||
font: 800 17px/1.3 Nunito, sans-serif;
|
||||
word-break: break-word;
|
||||
box-shadow: 0 3px 8px rgba(0, 0, 0, .35);
|
||||
transform: rotate(var(--tilt, 0deg));
|
||||
}
|
||||
#chat .msg.action { font-style: italic; }
|
||||
|
||||
/* the name on a strip of washi tape, tinted with the chatter's Twitch colour */
|
||||
#chat .msg .u {
|
||||
position: absolute;
|
||||
top: -12px;
|
||||
left: 10px;
|
||||
max-width: 85%;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
padding: 1px 12px 2px;
|
||||
font: 700 23px/1 Caveat, cursive;
|
||||
color: var(--ink);
|
||||
background-color: var(--tape, rgba(201, 182, 228, .92));
|
||||
background-image: repeating-linear-gradient(45deg, #fff5 0 6px, #0000 6px 12px);
|
||||
transform: rotate(calc(var(--tilt, 0deg) * -2));
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
|
||||
clip-path: polygon(0 8%, 3% 0, 97% 4%, 100% 0, 98% 100%, 2% 94%);
|
||||
}
|
||||
|
||||
/* emotes (Twitch + 7TV). Zero-width 7TV emotes (RainTime, PETPET...) stack on the emote before them. */
|
||||
#chat .em { position: relative; display: inline-grid; vertical-align: -0.45em; margin: 0 1px; }
|
||||
#chat .em > img { grid-area: 1 / 1; height: 1.6em; width: auto; justify-self: center; align-self: center;
|
||||
filter: drop-shadow(0 0 1px rgba(58, 42, 32, .55)); } /* white emotes (Clap...) stay visible on cream */
|
||||
#chat .msg.big .em > img { height: 2.6em; } /* emote-only messages: bigger emotes */
|
||||
|
||||
#chat .msg.in { animation: chat-in .45s cubic-bezier(.2, 1.3, .4, 1) both; }
|
||||
#chat .msg.out { animation: chat-out .6s ease-in both; }
|
||||
@keyframes chat-in { from { opacity: 0; transform: translateY(-24px) rotate(calc(var(--tilt, 0deg) * -3)) scale(.95); } }
|
||||
@keyframes chat-out { to { opacity: 0; transform: translate(40px, 30px) rotate(calc(var(--tilt, 0deg) + 12deg)); } }
|
||||
164
public/chat/chat.js
Normal file
164
public/chat/chat.js
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
/* Chat messages for the overlay (?parts=chat). tmi.js in overlay.ejs feeds them in via HSChat.add().
|
||||
Look: public/chat/chat.css. Emotes: Twitch (from the message tags) + 7TV channel and global sets.
|
||||
Settings used: S.message_timeout (seconds, 0 = never fade) and S.max_messages. */
|
||||
(function () {
|
||||
const box = document.getElementById('chat');
|
||||
if (!box) return;
|
||||
|
||||
const TWITCH_EMOTE = id => `https://static-cdn.jtvnw.net/emoticons/v2/${id}/default/dark/2.0`;
|
||||
const SEVENTV = 'https://7tv.io/v3';
|
||||
const ZERO_WIDTH = 256; // 7TV emote data flag: overlays the emote before it
|
||||
const REFRESH_MS = 10 * 60 * 1000; // pick up newly added 7TV emotes without an OBS refresh
|
||||
|
||||
let emotes7tv = new Map(); // name -> { url, zw }
|
||||
let roomId = null, seq = 0;
|
||||
|
||||
// ---------- 7TV ----------
|
||||
function setFrom(list, into) {
|
||||
for (const e of list || []) {
|
||||
const host = e.data && e.data.host;
|
||||
if (!host || !host.url) continue;
|
||||
const file = (host.files || []).find(f => f.name === '2x.webp') || (host.files || []).find(f => /\.webp$/.test(f.name));
|
||||
if (!file) continue;
|
||||
into.set(e.name, { url: 'https:' + host.url + '/' + file.name, zw: ((e.data.flags || 0) & ZERO_WIDTH) !== 0 });
|
||||
}
|
||||
}
|
||||
async function getJSON(url) {
|
||||
const r = await fetch(url);
|
||||
if (!r.ok) throw new Error(url + ' -> ' + r.status);
|
||||
return r.json();
|
||||
}
|
||||
async function load7tv() {
|
||||
const next = new Map();
|
||||
try { setFrom((await getJSON(`${SEVENTV}/emote-sets/global`)).emotes, next); }
|
||||
catch (e) { console.warn('[chat] 7TV global emotes:', e.message); }
|
||||
if (roomId) {
|
||||
try { setFrom(((await getJSON(`${SEVENTV}/users/twitch/${roomId}`)).emote_set || {}).emotes, next); } // channel wins over global
|
||||
catch (e) { console.warn('[chat] 7TV channel emotes:', e.message); }
|
||||
}
|
||||
if (next.size) emotes7tv = next;
|
||||
console.log(`[chat] 7TV emotes loaded: ${next.size}`);
|
||||
}
|
||||
function setRoom(id) {
|
||||
if (!id || id === roomId) return;
|
||||
roomId = id;
|
||||
load7tv();
|
||||
}
|
||||
load7tv();
|
||||
setInterval(load7tv, REFRESH_MS);
|
||||
|
||||
// ---------- message -> HTML ----------
|
||||
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
||||
const img = (url, name) => `<img src="${esc(url)}" alt="${esc(name)}" title="${esc(name)}">`;
|
||||
|
||||
// Returns { html, emoteOnly, emoteCount }.
|
||||
function render(text, twitchEmotes) {
|
||||
// Twitch emote positions are in code points, so work on an array of characters
|
||||
const chars = Array.from(text);
|
||||
const ranges = [];
|
||||
for (const [id, list] of Object.entries(twitchEmotes || {})) {
|
||||
for (const r of list) { const [a, b] = r.split('-').map(Number); ranges.push({ a, b, id }); }
|
||||
}
|
||||
ranges.sort((x, y) => x.a - y.a);
|
||||
|
||||
const tokens = []; // { t:'text', s } | { t:'em', url, name, zw }
|
||||
let i = 0;
|
||||
for (const r of ranges) {
|
||||
if (r.a > i) tokens.push({ t: 'text', s: chars.slice(i, r.a).join('') });
|
||||
tokens.push({ t: 'em', url: TWITCH_EMOTE(r.id), name: chars.slice(r.a, r.b + 1).join(''), zw: false });
|
||||
i = r.b + 1;
|
||||
}
|
||||
if (i < chars.length) tokens.push({ t: 'text', s: chars.slice(i).join('') });
|
||||
|
||||
// 7TV: whole words inside the text parts
|
||||
const out = [];
|
||||
for (const tk of tokens) {
|
||||
if (tk.t !== 'text') { out.push(tk); continue; }
|
||||
for (const part of tk.s.split(/(\s+)/)) {
|
||||
const e = part && emotes7tv.get(part);
|
||||
out.push(e ? { t: 'em', url: e.url, name: part, zw: e.zw } : { t: 'text', s: part });
|
||||
}
|
||||
}
|
||||
|
||||
// build HTML; a zero-width emote joins the emote stack before it (skipping whitespace)
|
||||
let html = '', open = false, count = 0, words = 0;
|
||||
for (let k = 0; k < out.length; k++) {
|
||||
const tk = out[k];
|
||||
if (tk.t === 'text') {
|
||||
if (/^\s*$/.test(tk.s)) {
|
||||
const nx = out.slice(k + 1).find(x => !(x.t === 'text' && /^\s*$/.test(x.s)));
|
||||
if (open && nx && nx.t === 'em' && nx.zw) continue; // keep the stack open for the overlay emote
|
||||
if (open) { html += '</span>'; open = false; }
|
||||
html += esc(tk.s);
|
||||
continue;
|
||||
}
|
||||
if (open) { html += '</span>'; open = false; }
|
||||
html += esc(tk.s); words++;
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
if (tk.zw && open) { html += img(tk.url, tk.name); continue; }
|
||||
if (open) html += '</span>';
|
||||
html += '<span class="em">' + img(tk.url, tk.name); open = true;
|
||||
}
|
||||
if (open) html += '</span>';
|
||||
return { html, emoteOnly: count > 0 && words === 0, emoteCount: count };
|
||||
}
|
||||
|
||||
// ---------- add / remove ----------
|
||||
function tapeFrom(hex) {
|
||||
const m = /^#?([0-9a-f]{6})$/i.exec(hex || '');
|
||||
const n = m ? parseInt(m[1], 16) : 0xc9b6e4;
|
||||
const c = [n >> 16, (n >> 8) & 255, n & 255].map(v => Math.round(v * .55 + 255 * .45));
|
||||
return `rgba(${c[0]},${c[1]},${c[2]},.92)`;
|
||||
}
|
||||
const S = () => window.S || {};
|
||||
|
||||
function add(tags, text, isAction) {
|
||||
if (tags && tags['room-id']) setRoom(tags['room-id']);
|
||||
const name = (tags && (tags['display-name'] || tags.username)) || '?';
|
||||
const r = render(text, tags && tags.emotes);
|
||||
|
||||
const d = document.createElement('div');
|
||||
d.className = 'msg in' + (r.emoteOnly && r.emoteCount <= 3 ? ' big' : '') + (isAction ? ' action' : '');
|
||||
d.dataset.id = (tags && tags.id) || '';
|
||||
d.dataset.user = ((tags && tags.username) || '').toLowerCase();
|
||||
d.style.setProperty('--tilt', (Math.random() * 3.2 - 1.6).toFixed(2) + 'deg');
|
||||
d.style.setProperty('--tape', tapeFrom(tags && tags.color));
|
||||
d.innerHTML = `<span class="u">${esc(name)}</span>${r.html}`;
|
||||
d._n = ++seq;
|
||||
|
||||
// FLIP: remember where the others are, put the new one on top, then glide them down from the old spot
|
||||
const before = new Map([...box.children].map(m => [m, m.getBoundingClientRect().top]));
|
||||
box.prepend(d);
|
||||
before.forEach((top, m) => {
|
||||
const dy = top - m.getBoundingClientRect().top;
|
||||
if (dy && m.animate) m.animate([{ translate: `0 ${dy}px` }, { translate: '0 0' }], { duration: 320, easing: 'cubic-bezier(.2,.8,.3,1)' });
|
||||
});
|
||||
d.addEventListener('animationend', () => d.classList.remove('in'), { once: true });
|
||||
|
||||
// over the cap: the oldest leave with their exit animation
|
||||
const live = [...box.children].filter(m => !m.classList.contains('out')).sort((a, b) => a._n - b._n);
|
||||
const max = Number(S().max_messages) || 10;
|
||||
live.slice(0, Math.max(0, live.length - max)).forEach(leave);
|
||||
|
||||
const t = Number(S().message_timeout);
|
||||
if (t > 0) d._t = setTimeout(() => leave(d), t * 1000);
|
||||
}
|
||||
|
||||
function leave(d) {
|
||||
if (d.classList.contains('out')) return;
|
||||
clearTimeout(d._t);
|
||||
d.classList.remove('in');
|
||||
d.classList.add('out');
|
||||
d.addEventListener('animationend', () => d.remove(), { once: true });
|
||||
setTimeout(() => d.remove(), 1200); // in case the animation never ends (hidden source)
|
||||
}
|
||||
|
||||
// moderation: a deleted message, or everything from a user who got timed out / banned, leaves at once
|
||||
function removeId(id) { [...box.children].filter(m => id && m.dataset.id === id).forEach(leave); }
|
||||
function removeUser(user) { const u = String(user || '').toLowerCase(); [...box.children].filter(m => u && m.dataset.user === u).forEach(leave); }
|
||||
function clear() { [...box.children].forEach(leave); }
|
||||
|
||||
window.HSChat = { add, removeId, removeUser, clear, setRoom, reload7tv: load7tv, render };
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue