【问题标题】:What is happening when you press escape to exit fullscreen? How can I replicate it with a button?当您按 Escape 退出全屏时会发生什么?如何用按钮复制它?
【发布时间】:2016-03-26 23:31:39
【问题描述】:

我正在尝试为我的 JavaScript 画布游戏添加全屏选项,它在某些浏览器上都可以使用,但 Firefox 的问题让我意识到我的逻辑非常错误,幸运的是它适用于任何浏览器浏览器。

我想在画布上有一个按钮,让您切换全屏,但我遇到了一些问题。我发现的第一个问题是,如果我 canvas.getBoundingClientRect() 调整大小(当我退出/进入全屏时),那么在某些浏览器中咳嗽 Firefox,该函数运行得太早并且它得到了边界 之前他们已经改变了,所以游戏仍然认为它应该是巨大的。

您可以查看代码和程序here。如果我对任何事情不是很清楚,请告诉我,这个很难解释。

【问题讨论】:

  • 你应该几乎总是在调整大小事件上使用一个小的超时
  • 应该在每个调整大小功能上吗?有没有办法一次性完成?
  • 查看去抖动模式,这对于滚动和调整大小很有用。 davidwalsh.name/function-debounce 它通过避免锤击来提高性能,但它也会修复你的不同步 dom 维度嗅探。
  • 保存游戏开始时的原始宽度和高度,并保留一个布尔变量,以便您知道玩家是否进入全屏,然后当玩家按下 esc 键时,检查是否全屏 bool为真,如果是则将画布设置回原始宽度和高度。

标签: javascript jquery html canvas fullscreen


【解决方案1】:

这是我不久前创建的一个 sn-p,它添加了 esc 键功能以退出全屏。我希望它也对你有用。

$('#view-fullscreen').click(function(){

    $('#container').css({'background': '#000000'}).fullScreen({

        'callback'      : function(fullScreen){
            if ( !fullScreen ) {


                $('#container').css({'background': '#ffffff'});

            }
        }

    });

    $(this).hide();
    return false;


});

【讨论】:

    【解决方案2】:

    欢迎来到fullscreen API 噩梦。

    此 API 仍在开发中,自从它首次在网络上发布以来,规范已经发生了很大变化。
    不幸的是,截至今天,没有一个主流浏览器真正支持它(例如,实际规范建议使用 Promise),更糟糕的是,这些浏览器都没有为这个 API 使用相同的关键字。

    所以让他们都满意可能有点苛刻。

    但首先,您不应该听resize 事件,因为它可能会在全屏模式的激活/停用期间发生多次。

    您需要的是fullscreenchange 事件。
    幸运的是,每个 UA 都写了整个事件名称,其中没有任何驼峰式,但带有供应商前缀。 (并且由于某种原因,IE 不支持使用addEventListener 方法附加它...)

    一旦你得到事件,为了知道我们是否真正进入或退出全屏模式,你必须检查document.[vendor-prefix]full(s||S)creenElement,如果有,那么我们进入了模式;否则我们退出它。

    现在,要退出全屏模式,您必须调用
    document.[vendor-prefix]((e||E)xit||Cancel)(f||F)ull(s||S)creen 方法。

    所以这里有一个 dirty 辅助函数:

    // if fullscreen API is supported it will return an array containing
    // [vendor-prefix, 'full(s||S)creen', 'Exit||Cancel'];
    var fs = function(){
        // if it is natively supported without vendor prefix (may it happen some day...)
        if('onfullscreenchange' in document){
            return ['', 'Fullscreen', 'exit'];
        }
        if('onmozfullscreenchange' in document){
            return ['moz','FullScreen', 'Cancel'];
        }
        if('onwebkitfullscreenchange' in document){
            return ['webkit', 'Fullscreen', 'Exit'];
        }
        if('onmsfullscreenchange' in document){
            return ['ms', 'Fullscreen', 'Exit'];
        }
    }();
    
    
    if(fs){
        // for some reason, IE doesn't support the addEventListener method...
        document['on'+fs[0]+'fullscreenchange'] = function(){
    
            ctx.clearRect(0, 0, c.width, c.height);
            // check for 'fullscreenElement' to know weither we entered or exited the fullscreen mode
            var status = document[fs[0]+fs[1]+'Element'];
    
            var statusString =  status ? 'entered':'exited';
            ctx.fillText(statusString+' fullscreen', 250, 50);
            // increment our fullscreen change counter
            fs_count++;
            if(status){
                ctx.fillText('click the canvas to exit fullscreen mode', 150 , 100);
                // attach the exit/cancel fullscreen call
                c.onclick = function(){document[fs[0]+fs[2]+fs[1]]();};
                }
            // log the counters
            ctx.fillText('fullscreen calls : '+fs_count, 0, 140);
            ctx.fillText('resize calls : '+resize_count, 0, 150);
            };
    
        btn.onclick = function(){
            //this one implies a new camelCase if a vendor prefix is needed...
            var camel = fs[0] ? 'R':'r';
            c[fs[0]+camel+'equest'+fs[1]]();
        };
    }
    var ctx = c.getContext('2d');
    ctx.fillStyle = "red";
    
    var resize_count = 0;
    var fs_count = 0;
    
    // increment our resize counter
    window.onresize= function(){resize_count++};
    
    <canvas id="c" width="500"></canvas>
    <button id="btn">enter fullscreen</button>
    

    由于全屏请求在 iframe 中被阻止,您可以在操作中看到它here 并使用代码here

    此外,您会注意到每个浏览器都会对这个请求采取不同的行动:webkit 浏览器将使页面全屏但保持元素的比例相同,而 FF 和 IE 将缩放元素以适应新的页面尺寸。
    这意味着您不应查看getBoundingClientRect() rect,而应根据window.innerWidthwindow.innerHeight 属性计算新大小。

    【讨论】:

    • 底部的第一个链接坏了,第二个没有全屏打开,至少在 chrome 中。但我喜欢逻辑的运作方式,这真的很聪明
    • @Howzieky 是的,这是一个临时链接,没有注意到...我会更新答案,但您应该可以通过单击“以窗口模式打开”从第二个链接打开它“ 关联。它也应该在您自己的页面上工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2021-12-18
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2015-06-27
    • 2023-03-29
    相关资源
    最近更新 更多