hls.js instead of iframe

This commit is contained in:
vance 2024-05-07 20:25:03 -07:00
parent db3a938a0b
commit 2ac4bb6dfc

View File

@ -21,7 +21,7 @@
color: #20C20E; color: #20C20E;
font-family: 'VT323', monospace; font-family: 'VT323', monospace;
font-size: 16pt; font-size: 16pt;
cursor: url('banana.png'), auto; cursor: none;
} }
html { html {
@ -52,6 +52,12 @@
font-size: 1rem; font-size: 1rem;
} }
#banana {
position: absolute;
z-index: 2;
pointer-events: none;
}
#bonzi { #bonzi {
position: absolute; position: absolute;
transition: transform 0.8s; transition: transform 0.8s;
@ -66,16 +72,16 @@
left: 50%; left: 50%;
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
z-index: 0; z-index: 0;
border: none;
} }
</style> </style>
</head> </head>
<body> <body>
<img alt='banana' id='banana' src='banana.png'/>
<img alt='bonzi' id='bonzi' src='bonzi.png'/> <img alt='bonzi' id='bonzi' src='bonzi.png'/>
<iframe allowfullscreen height='360px' id='live' referrerpolicy='origin' scrolling='no' <video autoplay height='360px' playsinline id='live' width='640px'>
src='https://live.vance.land/vance/' title='live.vance.land' width='640px'> </video>
</iframe> <script src="https://cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js"></script>
<script> <script>
const email = (function () { const email = (function () {
var p = Array.prototype.slice.call(arguments), var p = Array.prototype.slice.call(arguments),
@ -135,36 +141,38 @@
'link': 'https://dreamsinco.de' 'link': 'https://dreamsinco.de'
} }
}; };
const bonzi = document.getElementById('bonzi');
const live = document.getElementById('live');
// bonzi // bonzi
const banana = document.getElementById('banana');
const bonzi = document.getElementById('bonzi');
let mouseTop = 0; let mouseTop = 0;
let mouseLeft = 0; let mouseLeft = 0;
let bonziTop = 0; let bonziTop = 0;
let bonziLeft = 0; let bonziLeft = 0;
const speed = 0.02; const speed = 0.02;
function moveBonzi() {
(function moveBonzi() {
bonzi.style.transform = bonziLeft > mouseLeft ? 'translate(5%, -35%) scaleX(-1)' : 'translate(-80%, -35%)'; bonzi.style.transform = bonziLeft > mouseLeft ? 'translate(5%, -35%) scaleX(-1)' : 'translate(-80%, -35%)';
bonziTop = bonziTop + ((mouseTop - bonziTop) * speed); bonziTop = bonziTop + ((mouseTop - bonziTop) * speed);
bonziLeft = bonziLeft + ((mouseLeft - bonziLeft) * speed); bonziLeft = bonziLeft + ((mouseLeft - bonziLeft) * speed);
bonzi.style.top = `${bonziTop}px`; bonzi.style.top = `${bonziTop}px`;
bonzi.style.left = `${bonziLeft}px`; bonzi.style.left = `${bonziLeft}px`;
requestAnimationFrame(moveBonzi); requestAnimationFrame(moveBonzi);
} })();
moveBonzi();
['mousemove', 'touchstart', 'touchmove'].forEach(type => { ['mousemove', 'touchstart', 'touchmove'].forEach(type => {
window.addEventListener(type, event => { window.addEventListener(type, event => {
mouseTop = event.clientY || event.touches[0].clientY; mouseTop = event.clientY || event.touches[0].clientY;
mouseLeft = event.clientX || event.touches[0].clientX; mouseLeft = event.clientX || event.touches[0].clientX;
banana.style.top = `${mouseTop}px`;
banana.style.left = `${mouseLeft}px`;
}); });
}); });
// remove stream if embedded //live
if (window.self !== window.top) { const live = document.getElementById('live');
live.remove(); const liveSrc = new URL('index.m3u8', socials.live.link).toString();
}
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0); const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0); const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
@ -175,61 +183,90 @@
live.height = `${vw * (9 / 16)}px`; live.height = `${vw * (9 / 16)}px`;
} }
// check if stream active every 5 seconds (function loadStream() {
setInterval(function checkLive() { // remove stream if embedded
fetch(new URL('index.m3u8', socials.live.link)) if (window.self !== window.top) {
.then(response => { live.remove();
live.style.display = response.ok ? 'inline' : 'none'; return;
}); }
return checkLive;
}(), 5000);
const min_speed = 10; if (Hls.isSupported()) {
const max_speed = 20; const hls = new Hls({
let entities = 0; maxLiveSyncPlaybackRate: 1.5,
const max_entities = Math.floor(vw / 30); });
hls.on(Hls.Events.ERROR, (event, data) => {
if (data.fatal) {
live.style.display = 'none';
hls.destroy();
}
setTimeout(loadStream, 5000);
});
hls.on(Hls.Events.MEDIA_ATTACHED, () => {
hls.loadSource(liveSrc);
});
hls.on(Hls.Events.MANIFEST_PARSED, () => {
live.style.display = 'inline';
live.play();
});
hls.attachMedia(live);
} else if (live.canPlayType('application/vnd.apple.mpegurl')) {
fetch(liveSrc)
.then(() => {
live.src = liveSrc;
live.play();
});
}
})();
// animated anchors // animated anchors
function animate(entity) { const minSpeed = 10;
let flag = parseInt(entity.style.left, 10); const maxSpeed = 20;
let anchors = 0;
const maxAnchors = Math.floor(vw / 30);
function animateAnchor(anchor) {
let flag = parseInt(anchor.style.left, 10);
setInterval(() => { setInterval(() => {
entity.style.left = `${--flag}px`; anchor.style.left = `${--flag}px`;
if (entity.offsetWidth <= -flag) { if (anchor.offsetWidth <= -flag) {
flag = vw + Math.floor(entity.offsetWidth / 2); flag = vw + Math.floor(anchor.offsetWidth / 2);
let posy = Math.floor(Math.random() * vh) - Math.floor(entity.offsetHeight / 2); let posy = Math.floor(Math.random() * vh) - Math.floor(anchor.offsetHeight / 2);
entity.style.top = `${posy}px`; anchor.style.top = `${posy}px`;
} }
}, Math.floor(Math.random() * (max_speed - min_speed) + min_speed)); }, Math.floor(Math.random() * (maxSpeed - minSpeed) + minSpeed));
} }
function createAnchor(link, content) { function createAnchor(link, content) {
if (entities >= max_entities) { if (anchors >= maxAnchors) {
return; return;
} }
const entity = document.createElement('a');
entity.href = link;
entity.target = '_blank';
entity.innerHTML = content;
const posx = Math.floor(Math.random() * vw) - Math.floor(entity.offsetWidth / 2); const anchor = document.createElement('a');
const posy = Math.floor(Math.random() * vh) - Math.floor(entity.offsetHeight / 2); anchor.href = link;
let z_index = Math.floor(entities - max_entities / 2); anchor.target = '_blank';
z_index = (z_index >= 0) ? z_index + 1 : z_index; anchor.innerHTML = content;
entity.style.position = 'absolute'; const posTop = Math.floor(Math.random() * vh) - Math.floor(anchor.offsetHeight / 2);
entity.style.top = `${posy}px`; const posLeft = Math.floor(Math.random() * vw) - Math.floor(anchor.offsetWidth / 2);
entity.style.left = `${posx}px`; let zIndex = Math.floor(anchors - maxAnchors / 2);
entity.style.zIndex = `${z_index}`; zIndex = (zIndex >= 0) ? zIndex + 1 : zIndex;
document.body.prepend(entity); anchor.style.position = 'absolute';
++entities; anchor.style.top = `${posTop}px`;
animate(entity); anchor.style.left = `${posLeft}px`;
anchor.style.zIndex = `${zIndex}`;
document.body.prepend(anchor);
++anchors;
animateAnchor(anchor);
} }
// Always have socials // Always have socials
while (entities < max_entities / 4) { while (anchors < maxAnchors / 4) {
for (const key in socials) { for (const key in socials) {
createAnchor(socials[key].link, socials[key].text); createAnchor(socials[key].link, socials[key].text);
} }