【问题标题】:Responsive HTML5 Video + Canvas响应式 HTML5 视频 + 画布
【发布时间】:2013-04-20 15:52:08
【问题描述】:

我使用以下函数来缩放和翻译以及调整浏览器窗口的大小。但是,当窗口大小改变时,我想保存纵横比(现在视频的某些部分被截断),就像媒体播放器处理窗口大小调整一样。

/**
 * This function is called when 'resize' event occur, it simply changes the way 
 * <canvas> and <video> are displayed by browser using few CSS3 techniques.
 * @return none
 */
    function resize() {

        var w = 0;
        var h = 0;

        if (!window.innerWidth) {
            if (!(document.documentElement.clientWidth == 0)) {
                w = document.documentElement.clientWidth;
                h = document.documentElement.clientHeight;
            } else {
                w = document.body.clientWidth;
                h = document.body.clientHeight;
            }
        } else {
            w = window.innerWidth;
            h = window.innerHeight;
        }

        var cw = w;
        var ch = h;

        var aspect = videoWidth / videoHeight;

        if (w / h > aspect) {
            ch = cw / aspect;
        } else {
            cw = ch * aspect;
        }

        scalew = cw / videoWidth;
        scaleh = ch / videoHeight;

        dx = Math.round((w - cw) / 2);
        dy = Math.round((h - ch) / 2);

        var translateXForm = "translate(" + dx + "px," + dy + "px)";
        var scaleXForm = "scale(" + scalew + "," + scaleh + ")";
        var transform = translateXForm + " " + scaleXForm; 
        var style =
        "-webkit-transform:" + transform + ";" +
        "-moz-transform:" + transform + ";" +
        "-ms-transform:" + transform + ";" +
        "-o-transform:" + transform + ";" +
        "transform:" + transform;

        canvas.setAttribute("style", style);
        video.setAttribute("style", style);

    }

【问题讨论】:

    标签: html canvas resize aspect-ratio


    【解决方案1】:

    没有必要用 JavaScript 来做,我们可以用纯 CSS 来实现!

    只需为视频标签设置以下样式:

    video {
        height: auto;
        width: 100%;
    }
    

    这将在调整窗口大小时保持纵横比

    DEMO

    【讨论】:

    • 谢谢!它适用于
    猜你喜欢
    • 2013-05-10
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多