Last active 1733009831

index.html Raw
1<div class="video-container">
2 <canvas id="ambient-canvas-1"/>
3 <canvas id="ambient-canvas-2"/>
4 <video src="./video.mp4"></video>
5</div>
script.js Raw
1var Player = document.querySelector('#ambient-player')
2var FirstCanvas = document.getElementById("ambient-canvas-1")
3var SecondCanvas = document.getElementById("ambient-canvas-2")
4var FirstCtx = FirstCanvas.getContext("2d")
5var SecondCtx = SecondCanvas.getContext("2d")
6
7const FrameInterval = 1600
8const CanvasOpacity = "0.4"
9
10let IntervalId
11let FirstFrame = true
12const Draw = () => {
13 if (FirstFrame) {
14 FirstCtx.drawImage(Player, 0, 0, FirstCanvas.width, FirstCanvas.height)
15 ToFirstCanvas()
16 } else {
17 SecondCtx.drawImage(Player, 0, 0, SecondCanvas.width, SecondCanvas.height)
18 ToSecondCanvas()
19 }
20 FirstFrame = !FirstFrame
21}
22
23const ToFirstCanvas = () => {
24 FirstCanvas.style.opacity = CanvasOpacity
25 SecondCanvas.style.opacity = "0"
26}
27
28const ToSecondCanvas = () => {
29 SecondCanvas.style.opacity = CanvasOpacity
30 FirstCanvas.style.opacity = "0"
31}
32
33const StartDrawing = () => {IntervalId = window.setInterval(Draw, FrameInterval)}
34const PauseDrawing = () => {if (IntervalId) window.clearInterval(IntervalId)}
35
36const init = () => {
37 Player.pause()
38 Player.play()
39 Player.addEventListener("play", StartDrawing, false)
40 Player.addEventListener("pause", PauseDrawing, false)
41 Player.addEventListener("ended", PauseDrawing, false)
42 FirstCanvas.style.transition = SecondCanvas.style.transition = `opacity ${FrameInterval}ms`
43}
44
45const cleanup = () => {
46 Player.removeEventListener("play", StartDrawing)
47 Player.removeEventListener("pause", PauseDrawing)
48 Player.removeEventListener("ended", PauseDrawing)
49 PauseDrawing()
50}
51
52window.addEventListener("load", init)
53window.addEventListener("unload", cleanup)
style.css Raw
1.video-container {
2 position: relative;
3}
4.video-container canvas {
5 top: 0px;
6 left: 0px;
7 width: 100%;
8 height: 100%;
9 object-fit: cover;
10 z-index: -1;
11 transition: 0.6s filter;
12}