"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function drawCanvas(canvas, video, mediaStream, maxCanvasSize) {
    var videoTrackSettings = mediaStream.getVideoTracks()[0].getSettings();
    var cameraWidth = videoTrackSettings.width;
    var cameraHeight = videoTrackSettings.height;
    sizeCanvasAccordingToImage(canvas, cameraWidth, cameraHeight, maxCanvasSize);
    var ratio = Math.min(canvas.width / cameraWidth, canvas.height / cameraHeight);
    var scaledWidth = cameraWidth * ratio;
    var scaledHeight = cameraHeight * ratio;
    var x = (canvas.width - scaledWidth) / 2;
    var y = (canvas.height - scaledHeight) / 2;
    canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
    canvas.getContext("2d").drawImage(video, 0, 0, cameraWidth, cameraHeight, x, y, scaledWidth, scaledHeight);
}
exports.drawCanvas = drawCanvas;
function sizeCanvasAccordingToImage(canvas, width, height, maxCanvasSize) {
    var isLandscape = width > height;
    var imageRatio = width / height;
    if (isLandscape && width > maxCanvasSize) {
        canvas.width = maxCanvasSize;
        canvas.height = maxCanvasSize / imageRatio;
    }
    else if (!isLandscape && height > maxCanvasSize) {
        canvas.width = maxCanvasSize * imageRatio;
        canvas.height = maxCanvasSize;
    }
    else {
        canvas.width = width;
        canvas.height = height;
    }
}
//# sourceMappingURL=Canvas.js.map