'use strict'; let customRatio = true; // aspect 0 1 2 3 4 let ratios = ['0:0', '4:3', '16:9', '1:1', '1:2']; let aspect = 2; let ratio = getAspectRatio(); function getAspectRatio() { customRatio = aspect == 0 ? true : false; var ratio = ratios[aspect].split(':'); return ratio[1] / ratio[0]; } function setAspectRatio(i) { aspect = i; ratio = getAspectRatio(); resizeVideoMedia(); } function Area(Increment, Count, Width, Height, Margin = 10) { ratio = customRatio ? 0.75 : ratio; let i = 0; let w = 0; let h = Increment * ratio + Margin * 2; while (i < Count) { if (w + Increment > Width) { w = 0; h = h + Increment * ratio + Margin * 2; } w = w + Increment + Margin * 2; i++; } if (h > Height) return false; else return Increment; } function resizeVideoMedia() { let Margin = 3; let videoMediaContainer = document.getElementById('videoMediaContainer'); let Cameras = document.getElementsByClassName('Camera'); let Width = videoMediaContainer.offsetWidth - Margin * 2; let Height = videoMediaContainer.offsetHeight - Margin * 2; let max = 0; // full screen mode let bigWidth = Width * 4; if (videoMediaContainer.childElementCount == 1) { Width = Width - bigWidth; } // loop (i recommend you optimize this) let i = 1; while (i < 5000) { let w = Area(i, Cameras.length, Width, Height, Margin); if (w === false) { max = i - 1; break; } i++; } max = max - Margin * 2; setWidth(videoMediaContainer, Cameras, max, bigWidth, Margin, Height); } function setWidth(videoMediaContainer, Cameras, width, bigWidth, margin, maxHeight) { ratio = customRatio ? 0.68 : ratio; let isOneVideoElement = videoMediaContainer.childElementCount == 1 ? true : false; for (let s = 0; s < Cameras.length; s++) { Cameras[s].style.width = width + 'px'; Cameras[s].style.margin = margin + 'px'; Cameras[s].style.height = width * ratio + 'px'; if (isOneVideoElement) { Cameras[s].style.width = bigWidth + 'px'; Cameras[s].style.height = bigWidth * ratio + 'px'; let camHeigh = Cameras[s].style.height.substring(0, Cameras[s].style.height.length - 2); if (camHeigh >= maxHeight) Cameras[s].style.height = maxHeight - 2 + 'px'; } } } window.addEventListener( 'load', function (event) { resizeVideoMedia(); window.onresize = resizeVideoMedia; }, false, );