heystreamer/public/alerts/video.js
Bashy 8ac8fe024c Reward / Power-up icon as a top-left sticker on the cassette and the video box
Twitch icons of channel point rewards (custom images) and Custom Power-ups are cached
(10 min, refreshed on unknown triggers, never holding an alert more than 2 s) and
attached as alert.icon; cassette.js and video.js show it as a white-outlined sticker
on the top-left corner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:14:39 +02:00

77 lines
3.9 KiB
JavaScript

/*
Video redeems, for the ?parts=video overlay (an OBS source of its own, e.g. instead of HUDFX).
Any source size works; 2560x1440 like HUDFX gives sharper video. The box is laid out for 1920x1080 and
zoomed to the page width, so it always lands in the alert corner.
Redeem variations with a video (style 'video', see src/redeems.js) play here full-screen, one at a time,
with a camcorder notification box (video.css) while the video runs.
VideoFX.play(alert) queue it
VideoFX.playTest(alert) test: replaces a playing/queued test at once
VideoFX.skip() stop the current one, carry on with the queue
alert: { video, volume, user, reward, variation, bits, test }
*/
(() => {
const queue = [];
let cur = null, vid = null, box = null, tcTimer = null;
function els() {
if (vid) return;
vid = document.createElement('video'); vid.id = 'vfx-video'; vid.playsInline = true; document.body.appendChild(vid);
box = document.createElement('div'); box.id = 'vfx-box';
box.innerHTML = '<img class="ico" alt=""><div class="rec">● REC</div><div class="tc"></div><div class="title"></div><div class="who"></div><div class="amt"></div><div class="brand">POWER-UP</div>';
document.body.appendChild(box);
fit(); addEventListener('resize', fit);
}
function fit() { if (box) box.style.zoom = innerWidth / 1920; }
const fmt = s => { s = Math.max(0, Math.floor(s)); return `▶ ${Math.floor(s / 3600)}:${String(Math.floor(s / 60) % 60).padStart(2, '0')}:${String(s % 60).padStart(2, '0')}`; };
function show(a) {
els();
const name = a.variation && a.variation !== a.reward ? a.variation : (a.reward || 'Power-up');
box.querySelector('.title').textContent = `Triggered ${name}`;
box.querySelector('.who').textContent = a.user || '';
box.querySelector('.amt').textContent = a.bits ? `◆ ${a.bits} bits ◆` : '';
box.querySelector('.tc').textContent = fmt(0);
const ico = box.querySelector('.ico'); // the Power-up's / reward's Twitch icon, top-left corner
if (a.icon) { ico.src = a.icon; ico.hidden = false; } else { ico.removeAttribute('src'); ico.hidden = true; }
box.style.visibility = 'visible';
box.animate([{ opacity: 0, transform: 'translateY(-12px)' }, { opacity: 1, transform: 'none' }], { duration: 400, easing: 'ease-out', fill: 'both' });
clearInterval(tcTimer); tcTimer = setInterval(() => { box.querySelector('.tc').textContent = fmt(vid.currentTime); }, 250);
}
function hideBox() {
clearInterval(tcTimer);
if (!box || box.style.visibility !== 'visible') return;
box.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 500, fill: 'both' }).finished
.then(() => { if (!cur) box.style.visibility = 'hidden'; }).catch(() => {});
}
function finish(a) {
if (cur !== a) return;
clearTimeout(a._guard);
vid.pause(); vid.removeAttribute('src'); vid.load(); vid.style.visibility = 'hidden';
cur = null;
hideBox();
setTimeout(next, 600);
}
function next() {
if (cur || !queue.length) return;
const a = cur = queue.shift();
show(a);
vid.src = a.video; vid.volume = Math.max(0, Math.min(1, a.volume ?? 0.8)); vid.currentTime = 0;
vid.style.visibility = 'visible';
vid.onended = () => finish(a);
vid.onerror = () => { console.warn('[video] could not play', a.video); setTimeout(() => finish(a), 2500); };
vid.play().catch(e => { console.warn('[video] play:', e.message); setTimeout(() => finish(a), 2500); });
a._guard = setTimeout(() => finish(a), 120000); // never block the queue on a broken/endless file
}
function play(a) { if (!a?.video) return; queue.push(a); next(); }
function playTest(a) {
if (!a?.video) return;
for (let i = queue.length - 1; i >= 0; i--) if (queue[i].test) queue.splice(i, 1);
if (cur?.test) { queue.unshift({ ...a, test: true }); finish(cur); }
else play({ ...a, test: true });
}
function skip() { if (cur) finish(cur); }
window.VideoFX = { play, playTest, skip };
})();