requestAnimationFrame附加到App对象而不是Window [英] requestAnimationFrame attached to App object not Window

查看:227
本文介绍了requestAnimationFrame附加到App对象而不是Window的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置以下jsFiddle http://jsfiddle.net/ZbhQY/4/

I have set up the following jsFiddle http://jsfiddle.net/ZbhQY/4/

我使用一个小的requestAnimationFrame polyfill并将结果返回到window.reqeuestAnimFrame这一切都很好,所有调用这个函数工作正常。

Im using a small requestAnimationFrame polyfill and returning the result to window.reqeuestAnimFrame this all works fine and all calls to this function work as expected.

如果我改变这个返回到game.reqeuestAnimFrame并将所有调用window.reqeuestAnimFrame替换为新的game.reqeuestAnimFrame它不再工作。

If i change this to return to game.reqeuestAnimFrame and replace all calls to window.reqeuestAnimFrame to the new game.reqeuestAnimFrame it no longer works.

有人可以解释为什么是这种情况,我可以做什么,以使这项工作?
感谢。

Could someone please explain why this is the case and what i could do in order to make this work? Thanks.

推荐答案

requestAnimationFrame 窗口才能正常工作。

您可以将重拨为:

game.requestAnimFrame.call(win, game.run);

,它会按预期运作。

你遇到的错误是因为 requestAnimationFrame 期望其上下文( this )为窗口,而是其上下文 game

The error you were running into is because requestAnimationFrame expects its context (this) to be window, but instead its context was game.

http://jsfiddle.net/ZbhQY/5/

或者,您可以重写 requestAnimFrame getter:

Alternatively, you could rewrite your requestAnimFrame getter like so:

game.requestAnimFrame = (function() {
        var rAF = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
            window.setTimeout(callback, 1000 / 60);
        };

        return function(callback){
            rAF.call(window, callback);
        };            
    })();

这将允许您调用 game.requestAnimFrame(game.run)

这篇关于requestAnimationFrame附加到App对象而不是Window的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆