const moveStepVr = 10;  // Шаг для перемещения
const zoomStepVr = 0.01;  // Шаг для увеличения-уменьшения
const MIN_SCALEVr = 0.05; // Минимальный масштаб
const MAX_SCALEVr = 5;   // Максимальный масштаб
const DEAD_ZONE = 0.1; // Мертвая зона для стиков

let translateVrXLeft = 0;
let translateVrYLeft = 0;
let scaleVrLeft = 0.05; // Начальный масштаб для левой части

let translateVrXRight = 0;
let translateVrYRight = 0;
let scaleVrRight = 0.05; // Начальный масштаб для правой части

function getInitialTransformValues(board) {
    const transform = board.style.transform || '';
    const translateMatch = transform.match(/translate\((-?\d*\.?\d*)px,\s*(-?\d*\.?\d*)px\)/);
    const scaleMatch = transform.match(/scale\((-?\d*\.?\d*)\)/);

    const translateX = translateMatch ? parseFloat(translateMatch[1]) : 0;
    const translateY = translateMatch ? parseFloat(translateMatch[2]) : 0;
    const scale = scaleMatch ? parseFloat(scaleMatch[1]) : 0.05;

    return { translateX, translateY, scale };
}

window.addEventListener('gamepadconnected', function(e) {
    console.log('Gamepad connected at index %d: %s. %d buttons, %d axes.',
        e.gamepad.index, e.gamepad.id,
        e.gamepad.buttons.length, e.gamepad.axes.length);
    requestAnimationFrame(handleGamepadInput);
});

function handleGamepadInput() {
    const gamepads = navigator.getGamepads();
    if (!gamepads) return;

    const gamepad = gamepads[0];
    if (!gamepad) return;

    const boardLeft = document.getElementById('board-left');
    const boardRight = document.getElementById('board-right');

    if (boardLeft && boardRight) {
        if (translateVrXLeft === 0 && translateVrYLeft === 0 && scaleVrLeft === 0.05) {
            const initialLeft = getInitialTransformValues(boardLeft);
            translateVrXLeft = initialLeft.translateX;
            translateVrYLeft = initialLeft.translateY;
            scaleVrLeft = initialLeft.scale;
        }

        if (translateVrXRight === 0 && translateVrYRight === 0 && scaleVrRight === 0.05) {
            const initialRight = getInitialTransformValues(boardRight);
            translateVrXRight = initialRight.translateX;
            translateVrYRight = initialRight.translateY;
            scaleVrRight = initialRight.scale;
        }

        const centerLeftX = (boardLeft.offsetWidth / 4);
        const centerLeftY = boardLeft.offsetHeight / 2;
        const centerRightX = (boardRight.offsetWidth /8);
        const centerRightY = boardRight.offsetHeight / 2;

        let newScaleLeft = scaleVrLeft;
        let newScaleRight = scaleVrRight;

        if (gamepad.buttons[5].pressed) { // Zoom out
            newScaleLeft = Math.max(MIN_SCALEVr, scaleVrLeft - zoomStepVr);
            newScaleRight = Math.max(MIN_SCALEVr, scaleVrRight - zoomStepVr);
        }
        if (gamepad.buttons[3].pressed) {  // Zoom in
            newScaleLeft = Math.min(MAX_SCALEVr, scaleVrLeft + zoomStepVr);
            newScaleRight = Math.min(MAX_SCALEVr, scaleVrRight + zoomStepVr);
        }

        // Изменение translateX и translateY, чтобы центр оставался на месте
        const zoomFactorLeft = newScaleLeft / scaleVrLeft;
        const zoomFactorRight = newScaleRight / scaleVrRight;

        translateVrXLeft = centerLeftX - (centerLeftX - translateVrXLeft) * zoomFactorLeft;
        translateVrYLeft = centerLeftY - (centerLeftY - translateVrYLeft) * zoomFactorLeft;

        translateVrXRight = centerRightX - (centerRightX - translateVrXRight) * zoomFactorRight;
        translateVrYRight = centerRightY - (centerRightY - translateVrYRight) * zoomFactorRight;

        scaleVrLeft = newScaleLeft;
        scaleVrRight = newScaleRight;

        // Проверка осей стиков для перемещения с учетом мертвой зоны
        const moveLeftX = Math.abs(gamepad.axes[0]) > DEAD_ZONE ? gamepad.axes[0] * moveStepVr : 0;
        const moveLeftY = Math.abs(gamepad.axes[1]) > DEAD_ZONE ? gamepad.axes[1] * moveStepVr : 0;
        const moveRightX = Math.abs(gamepad.axes[0]) > DEAD_ZONE ? gamepad.axes[0] * moveStepVr : 0;
        const moveRightY = Math.abs(gamepad.axes[1]) > DEAD_ZONE ? gamepad.axes[1] * moveStepVr : 0;

        translateVrXLeft -= moveLeftX;
        translateVrYLeft -= moveLeftY;
        translateVrXRight -= moveRightX;
        translateVrYRight -= moveRightY;

        updateVRTransform(boardLeft, translateVrXLeft, translateVrYLeft, scaleVrLeft);
        updateVRTransform(boardRight, translateVrXRight, translateVrYRight, scaleVrRight);
    }

    requestAnimationFrame(handleGamepadInput);
}

function updateVRTransform(board, translateX, translateY, scale) {
    board.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
    board.style.backgroundPosition = `${translateX}px ${translateY}px`;
}

/* Basic Reset */
body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-family: 'Arial', sans-serif;
}

/* Main Board Styles */
#board-wrapper {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    position: relative;
    cursor: grab;
    background-image: url('/static/img/scale_1200.jpg');
    background-repeat: repeat;
    background-size: 1200px 675px;
}

#board {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
}

/* Note and Image Styles */
.note, .image {
    position: absolute;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
    padding: 10px;
    box-sizing: border-box;
    cursor: move;
}

textarea {
    width: 100%;
    height: 100%;
    border: none;
    resize: none;
    outline: none;
    margin: 0;
    padding: 0;
}

.image img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.resize-handle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 10px;
    height: 10px;
    background-color: #007bff;
    cursor: nwse-resize;
}

/* Audio Element Styles */
.audio {
    position: absolute;
    background-color: #ffffff;
    border: 1px solid #e0e0e0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    padding: 10px 15px;
    box-sizing: border-box;
    border-radius: 5px;
    width: 300px;
    transition: box-shadow 0.2s;
    cursor: move;
}

.audio:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.audio-file-name {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 8px;
    color: #444;
    text-align: center;
}

.controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
}

.controls button {
    background: #007bff;
    border: none;
    color: white;
    padding: 5px 12px;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
}

.controls button:hover {
    background: #0056b3;
    transform: scale(1.05);
}

.progress-bar {
    flex-grow: 1;
    margin: 0 10px;
    height: 6px;
    cursor: pointer;
    background: #e0e0e0;
    border-radius: 5px;
    outline: none;
    -webkit-appearance: none;
}

.progress-bar::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 12px;
    height: 12px;
    background: #007bff;
    border-radius: 50%;
    cursor: pointer;
}

audio {
    display: none;
}

/* Hidden Class */
.hidden {
    display: none;
}

/* Menu Styles */
#menu-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
}

#menu-button {
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

#dropdown-menu {
    position: absolute;
    bottom: 50px; /* Adjust the dropdown position */
    right: 0;
    background-color: white;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    z-index: 1000; /* Ensure menu appears on top */
}

#dropdown-menu button {
    width: 100%;
    padding: 10px 20px;
    background: none;
    color: #007bff;
    border: none;
    text-align: left;
    cursor: pointer;
}

#dropdown-menu button:hover {
    background-color: #f1f1f1;
}

#vr-wrapper {
    display: flex;
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10000; /* Ensure it appears on top */
}

.half-screen {
    width: 50%;
    overflow: hidden;
    position: relative;
}

#board-left, #board-right {
    width: 100%;
    height: 100%;
    transform-origin: 0 0;
}

Мне надо чтобы в vr левая и правые доски были строго квадратного размера. Если при изменении размера требуется заполнить пустую область то она должна быть черной