New overlay modules (taped-paper look, shared with the polaroid alerts): - cassette.js: redeems as a cassette tape; rarity = shell colour (Ultra Rare gold with glow, foil and sparkles), reels spin for the sound's length, sticker corner. - fx.js: Gigantify (viewer's emote grows giant), On-Screen Celebration (emote copies bounce around the screen), highlight / Message Effect (tall taped note). - poll.js: live poll card with bars, countdown and a WINNER stamp. - hypebar.js: persistent Hype Train strip (level, progress, time left). - raidcard.js: silent raid send-off card that stays during the raid countdown. - nowplaying.js: Feishin now-playing slip (not loaded yet, see overlay). polaroid.js: shared queue now also drives cassettes/effects; test alerts replace only other tests and never cut off real alerts; alerts stay up for the animation + 3 s or the sound's length; sound lengths measured in the browser for UI-picked sounds; tip and Hype Train alerts; optional ALERTS_BASE for offline test pages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
4.5 KiB
JavaScript
91 lines
4.5 KiB
JavaScript
/*
|
|
Live poll card for the overlay. Not part of the alert queue: it stays up while the poll runs.
|
|
PollCard.update(phase, poll) phase: begin | progress | end
|
|
poll: { id, title, choices:[{ id, title, votes }], ends_at, status } (status on end: completed | archived | terminated)
|
|
Votes animate live; on 'completed' the winner(s) get a heart and a WINNER stamp, then the card drops away.
|
|
*/
|
|
(function () {
|
|
const COLORS = ['#f4a0a8', '#a88be0', '#7fcfa6', '#f6c75b', '#8cc8f0'];
|
|
const HEART = (window.ALERTS_BASE || '/alerts/') + 'art/Heart.png';
|
|
const SHOW_RESULT_MS = 9000;
|
|
const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c]));
|
|
let el = null, pollId = null, timer = null, hideTimer = null, endsAt = 0;
|
|
|
|
function root() {
|
|
if (!el) { el = document.createElement('div'); el.id = 'poll-card'; document.body.appendChild(el); }
|
|
return el;
|
|
}
|
|
|
|
function build(poll) {
|
|
const el = root();
|
|
clearTimeout(hideTimer); el.getAnimations().forEach(a => a.cancel());
|
|
pollId = poll.id;
|
|
el.innerHTML = `<div class="head"><span>POLL</span><span class="timer"></span></div>
|
|
<div class="q">${esc(poll.title)}</div>
|
|
${poll.choices.map((c, i) => `<div class="c" data-id="${esc(c.id)}">
|
|
<div class="row"><span class="t">${esc(c.title)}</span><span class="n">0</span></div>
|
|
<div class="track"><div class="fill" style="background-color:${COLORS[i % COLORS.length]}"></div></div>
|
|
<img class="heart" src="${HEART}" alt=""></div>`).join('')}
|
|
<div class="stamp">WINNER</div>`;
|
|
el.style.visibility = 'visible';
|
|
const tilt = (Math.random() * 4 - 2).toFixed(2);
|
|
el.animate([
|
|
{ transform:`translateX(-560px) rotate(-8deg)`, opacity:0 },
|
|
{ transform:`translateX(10px) rotate(${+tilt + 1}deg)`, opacity:1, offset:.75 },
|
|
{ transform:`translateX(0) rotate(${tilt}deg)`, opacity:1 },
|
|
], { duration:650, easing:'cubic-bezier(.2,.8,.3,1)', fill:'forwards' });
|
|
}
|
|
|
|
function votes(poll) {
|
|
const total = poll.choices.reduce((s, c) => s + c.votes, 0);
|
|
for (const c of poll.choices) {
|
|
const row = el.querySelector(`.c[data-id="${CSS.escape(c.id)}"]`);
|
|
if (!row) continue;
|
|
const pct = total ? Math.round(100 * c.votes / total) : 0;
|
|
row.querySelector('.n').textContent = `${c.votes} · ${pct}%`;
|
|
row.querySelector('.fill').style.width = `${total ? Math.max(3, pct) : 0}%`;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
function tick() {
|
|
const s = Math.max(0, Math.round((endsAt - Date.now()) / 1000));
|
|
const t = el.querySelector('.timer');
|
|
if (t) t.textContent = `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')} left`;
|
|
}
|
|
|
|
function hide(delay) {
|
|
clearTimeout(hideTimer);
|
|
hideTimer = setTimeout(() => {
|
|
el.animate([{ transform:'translateY(0)', opacity:1 }, { transform:'translateY(160px) rotate(10deg)', opacity:0 }],
|
|
{ duration:550, easing:'ease-in', fill:'forwards' }).finished.then(() => { el.style.visibility = 'hidden'; pollId = null; }).catch(() => {});
|
|
}, delay);
|
|
}
|
|
|
|
function update(phase, poll) {
|
|
if (!poll || !poll.choices) return;
|
|
if (phase === 'begin' || pollId !== poll.id) build(poll);
|
|
votes(poll);
|
|
if (poll.ends_at) { endsAt = Date.parse(poll.ends_at); clearInterval(timer); timer = setInterval(tick, 500); tick(); }
|
|
if (phase !== 'end') return;
|
|
clearInterval(timer);
|
|
const t = el.querySelector('.timer');
|
|
if (poll.status !== 'completed') { if (t) t.textContent = 'cancelled'; hide(1500); return; }
|
|
if (t) t.textContent = ''; // the WINNER stamp takes this corner
|
|
const max = Math.max(...poll.choices.map(c => c.votes));
|
|
el.querySelectorAll('.c').forEach(row => {
|
|
const c = poll.choices.find(x => x.id === row.dataset.id);
|
|
const win = max > 0 && c.votes === max;
|
|
row.classList.add(win ? 'win' : 'lose');
|
|
if (win) row.querySelector('.heart').animate([
|
|
{ transform:'rotate(-40deg) scale(0)', opacity:0 }, { transform:'rotate(10deg) scale(1.3)', opacity:1, offset:.6 }, { transform:'rotate(6deg) scale(1)', opacity:1 },
|
|
], { delay:300, duration:450, easing:'ease-out', fill:'forwards' });
|
|
});
|
|
if (max > 0) el.querySelector('.stamp').animate([
|
|
{ transform:'rotate(-12deg) scale(2.2)', opacity:0 }, { transform:'rotate(-12deg) scale(.9)', opacity:1, offset:.7 }, { transform:'rotate(-12deg) scale(1)', opacity:1 },
|
|
], { delay:700, duration:380, easing:'ease-in', fill:'forwards' });
|
|
hide(SHOW_RESULT_MS);
|
|
}
|
|
|
|
window.PollCard = { update };
|
|
})();
|