/*
Card alerts: the plain style (Actions → Alerts: style "Card"). Used for events (follow, sub, gift, raid, bits, tip,
Hype Train) and for redeem variations / read-aloud redeems set to Card. Called by polaroid.js (shared queue):
window.CardAlert.show(d, cur, { url, len, volume }) -> Promise (resolves when it has left)
d: { art|sticker|emote (picture URL), name, line, msg, badge {big, small, color}, tag (small label above the name),
tts (voice URL instead of the sound), ttsEnv (loudness for word timing) }
cur: { anims, timer, audio } from polaroid.js, so reset / skip can stop it.
Timeline: slides in (0.6 s), sound starts, stays until the sound / voice ends (at least 4 s), slides out.
*/
(function () {
const MIN_SHOW = 4000, SOUND_LEAD = 350, ENV_STEP = 0.04;
const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c]));
let el = null;
function root() {
if (!el) { el = document.createElement('div'); el.id = 'card-alert'; document.body.appendChild(el); }
return el;
}
const theme = () => (window.S && window.S.alert_theme) || {};
function show(d, cur, { url, len, volume }) {
const el = root(), c = theme().card || {};
if (c.accent) el.style.setProperty('--accent', c.accent);
if (c.bg) el.style.setProperty('--bg', c.bg);
if (c.text) el.style.setProperty('--fg', c.text);
const pic = d.emote || d.sticker || d.art || '';
const name = d.name || 'Someone';
const words = d.tts && d.msg ? String(d.msg).split(/\s+/).filter(Boolean) : null;
el.innerHTML = `
${pic ? `
})
` : `
${esc(name.trim()[0] || '♥').toUpperCase()}`}
${d.tag ? `
${esc(d.tag)}
` : ''}
${esc(name)}
${esc(d.line || '')}
${d.msg ? `
${words ? words.map(w => `${esc(w)}`).join(' ') : `“${esc(d.msg)}”`}
` : ''}
${d.badge ? `
${esc(d.badge.big)}${esc(d.badge.small || '')}
` : ''}
`;
el.style.visibility = 'visible';
const card = el.querySelector('.card');
const A = (node, frames, opts) => { const a = node.animate(frames, { fill:'both', ...opts }); cur.anims.push(a); return a; };
A(card, [
{ transform:'translateX(-700px)', opacity:0 },
{ transform:'translateX(14px)', opacity:1, offset:.75 },
{ transform:'translateX(0)', opacity:1 },
], { duration:600, easing:'cubic-bezier(.2,.8,.3,1)' });
A(el.querySelector('.pic'), [{ transform:'scale(.6) rotate(-8deg)' }, { transform:'scale(1.08) rotate(3deg)', offset:.6 }, { transform:'scale(1) rotate(0)' }],
{ delay:350, duration:450, easing:'ease-out' });
// the voice (read-aloud redeems) or the sound
const voice = d.tts || null;
const playUrl = voice || url;
const playLen = voice ? (d.ttsMs || 0) : len;
if (playUrl) {
const a = new Audio(playUrl); a.volume = volume;
cur.timer = setTimeout(() => { cur.audio = a; a.play().catch(err => console.warn('[alerts] sound blocked:', err.message)); }, SOUND_LEAD);
// words light up roughly as they are spoken
if (words) {
const spans = [...el.querySelectorAll('.msg .w')];
const total = Math.max(playLen, 1000), step = total / Math.max(spans.length, 1);
spans.forEach((s, i) => setTimeout(() => s.classList.add('on'), SOUND_LEAD + i * step));
}
} else if (words) el.querySelectorAll('.msg .w').forEach(s => s.classList.add('on'));
const stay = Math.max(MIN_SHOW, playUrl ? SOUND_LEAD + playLen + 600 : 0);
A(el.querySelector('.bar'), [{ transform:'scaleX(1)' }, { transform:'scaleX(0)' }], { delay:600, duration:Math.max(stay - 600, 500), easing:'linear' });
const exit = A(card, [
{ transform:'translateX(0)', opacity:1 },
{ transform:'translateX(18px)', opacity:1, offset:.25 },
{ transform:'translateX(-720px)', opacity:0 },
], { delay:stay, duration:550, easing:'cubic-bezier(.5,0,.8,.4)' });
return exit.finished.then(() => { el.style.visibility = 'hidden'; cur.anims.forEach(a => a.cancel()); }).catch(() => {});
}
function hide() { if (el) el.style.visibility = 'hidden'; }
window.CardAlert = { show, hide };
})();