This commit is contained in:
Ashley //// 2024-02-26 20:24:13 +00:00
parent 67fc4b76df
commit 645c979c18

View file

@ -1033,7 +1033,7 @@ display: block; !important;" autoplay controls>
</video> </video>
<% if (!a) { %> <% if (!a) { %>
<canvas width="150" height="150" id="ambient-canvas"></canvas> <canvas width="12" height="6" id="ambient-canvas"></canvas>
<% } %> <% } %>
@ -1889,61 +1889,41 @@ if (/[?&]autoplay=/.test(location.search)) {
<% if(!IsOldWindows) { %> <% if(!IsOldWindows) { %>
<script> <script>
const AMvideo = document.getElementById("video")
const canvas = document.getElementById("ambient-canvas")
const ctx = canvas.getContext("2d")
let requestId; let requestId;
let lastDrawTime = 0; // Timestamp of the last draw request let lastDrawTime = 0; // Timestamp of the last draw request
const loop = () => {
const loopStart = () => {
const now = performance.now(); const now = performance.now();
const elapsed = now - lastDrawTime; const elapsed = now - lastDrawTime;
if (elapsed >= (1000 / 30)) { // Adjusted for milliseconds if (elapsed >= (1000 / 24)) {
lastDrawTime = now; lastDrawTime = now;
draw(); ctx.drawImage(AMvideo, 0, 0, canvas.width, canvas.height);
} }
// Continue loop using requestAnimationFrame requestId = window.requestAnimationFrame(loop);
requestId = window.requestAnimationFrame(loopStart);
}; };
const drawStart = () => {
requestId = window.requestAnimationFrame(loop)
}
const loopCancel = () => { const drawPause = () => {
window.cancelAnimationFrame(requestId) window.cancelAnimationFrame(requestId)
requestId = undefined requestId = undefined
} }
const AMvideo = document.getElementById("video")
const canvas = document.getElementById("ambient-canvas")
const ctx = canvas.getContext("2d")
let step;
const draw = () => {
ctx.drawImage(AMvideo, 0, 0, canvas.width, canvas.height);
};
const drawLoop = () => {
draw()
step = window.requestAnimationFrame(drawLoop)
}
const drawPause = () => {
window.cancelAnimationFrame(step)
step = undefined
}
const init = () => { const init = () => {
video.pause();video.play(); video.pause();video.play();
AMvideo.addEventListener("loadeddata", draw, false) AMvideo.addEventListener("play", drawStart, false)
AMvideo.addEventListener("seeked", draw, false)
AMvideo.addEventListener("play", drawLoop, false)
AMvideo.addEventListener("pause", drawPause, false) AMvideo.addEventListener("pause", drawPause, false)
AMvideo.addEventListener("ended", drawPause, false) AMvideo.addEventListener("ended", drawPause, false)
} }
const cleanup = () => { const cleanup = () => {
AMvideo.removeEventListener("loadeddata", draw) AMvideo.removeEventListener("play", drawStart)
AMvideo.removeEventListener("seeked", draw)
AMvideo.removeEventListener("play", drawLoop)
AMvideo.removeEventListener("pause", drawPause) AMvideo.removeEventListener("pause", drawPause)
AMvideo.removeEventListener("ended", drawPause) AMvideo.removeEventListener("ended", drawPause)
} }