JSFiddle - React, Tailwind, and code Playground
HTML
<div id="video-background">
<div>
<video aria-hidden="true" playsinline="" autoplay="" muted="" loop="">
<source src="//starlink.ua/media/mod_starlink/car-blur.webm" type="video/webm">
<source src="//starlink.ua/media/mod_starlink/car-blur.mp4" type="video/mp4"></video>
</div>
</div>
CSS
#video-background {
top: 0;
left: 0;
position: fixed;
width: 100%;
height: 100%;
overflow: hidden;
pointer-events: none;
/* Center the grid */
display: flex;
justify-content: center;
align-items: center;
}
#video-background>div {
display: grid;
grid-template-columns: repeat(var(--video-bg-grid-cols), var(--video-width));
grid-template-rows: repeat(var(--video-bg-grid-rows), var(--video-height));
opacity: 0;
transition: opacity 2s ease;
}
#video-background>div.ready {
opacity: 1;
}
video {
display: block;
height: var(--video-height);
width: var(--video-width);
}
JavaScript
const videoWidth = 480;
let videoHeight = 0;
let grid;
let ww = window.innerWidth;
let wh = window.innerHeight;
const elWrapper = document.querySelector("#video-background > div");
const elVideo = elWrapper.querySelector("video");
elVideo.addEventListener(
"loadeddata",
() => {
setVideoHeight();
generateVideoGrid();
updateStyles();
},
false
);
function setVideoHeight() {
const aspectRatio = elVideo.videoWidth / elVideo.videoHeight;
videoHeight = videoWidth / aspectRatio;
}
function generateVideoGrid() {
let cols = 1;
let rows = 1;
if (ww > videoWidth) {
cols = Math.ceil(ww / videoWidth);
if (cols % 2 === 0) {
cols++;
}
}
if (wh > videoHeight) {
rows = Math.ceil(wh / videoHeight);
if (rows % 2 === 0) {
rows++;
}
}
const copies = cols * rows;
for (let i = 1; i < copies; i++) {
const clone = elVideo.cloneNode(true);
elWrapper.appendChild(clone);
}
grid = [cols, rows];
}
function updateStyles() {
document.documentElement.style.setProperty(
"--video-width",
`${videoWidth}px`
);
document.documentElement.style.setProperty(
"--video-height",
`${videoHeight}px`
);
document.documentElement.style.setProperty("--video-bg-grid-cols", grid[0]);
document.documentElement.style.setProperty("--video-bg-grid-rows", grid[1]);
elWrapper.classList.add('ready');
}