1

次の関数を使用して、スケーリングと変換、およびブラウザー ウィンドウのサイズ変更を行います。ただし、メディアプレーヤーがウィンドウのサイズ変更を処理するのと同じように、ウィンドウのサイズが変更されたときにアスペクト比を保存したい (現在、ビデオの一部が切り取られています)。

/**
 * 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);

    }
4

1 に答える 1