简体   繁体   中英

fullscreen centering html5 video live feed

I want to have a fullscreen live feed from my device's camera, but centered. So if i have my face in the middle, i want that on any device to have my face in the middle (whatever aspect ratio (landscape <-> portrait))

HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Picture time!</title>
    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <video id="video" autoplay="autoplay" onclick="snapshot()"></video>
    <canvas id="canvas" style="display: none"></canvas>

    <script src="./jquery.min.js"></script>
    <script src="./script.js"></script>
</body>

</html>

CSS:

body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

#video {
    position: absolute;
}

JS function that does the rescaling:

function rescaleEverthing(){
    var jqueryVideo = $("#video");
    var body = $("body");

    var videoWidth = jqueryVideo.width();
    var videoHeight = jqueryVideo.height();
    var bodyWidth = body.width();
    var bodyHeight = body.height();

    var videoRatio = videoWidth / videoHeight;
    var bodyRatio = bodyWidth / bodyHeight;

    if(vi)
    if(videoRatio > bodyRatio){
        jqueryVideo.css("height", "100%");
        jqueryVideo.css("transform", "translateX(-" + (videoWidth-bodyWidth)/2 + "px)");
    } else {
        jqueryVideo.css("width", "100%");
        jqueryVideo.css("transform", "translateY(-" + (videoHeight-bodyHeight)/2 + "px)");
    }
}

But when you run this, the middle of my video is not in the middle when portrait mode

Can someone help? thanks!

You don't need JS to do it, look at object-fit option:

 body { margin: 0; padding: 0; width: 100vw; height: 100vh; overflow: hidden; position:relative; } video { width:100%; height:100%; object-fit: contain; } 
 <video video-player controls id="video_player" src="http://dash.akamaized.net/akamai/bbb/bbb_1280x720_60fps_6000k.mp4"></video> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM