在ie9任何替代解决方案中出错requestAnimationFrame

时间:2014-07-10 12:42:32

标签: javascript jquery canvas html5-canvas internet-explorer-9

我正在创建Canvas(我是这个Canvas的新用户)对象柱面,这是正常工作的Chrome& Firefox,但是当我在ie9中打开相同的文件时。

我收到错误' requestAnimationFrame'未定义

当我谷歌误报时,它说requestAniationFrame不会在ie9上工作。

任何人都可以帮我解决这个问题。

这是我的代码

var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");

        var degreeAngle = 0;
        requestAnimationFrame(animate);

        function drawRotatedCylinder(x, y, w, h, degreeAngle) {
            context.save();
            context.translate(x + w / 10, y + h / 10);
            context.rotate(degreeAngle * Math.PI / 180);
            drawCylinder(-w / 10, -h / 10, w, h);
            context.restore();
        }   
function animate() {
        //requestAnimationFrame(animate);
        context.clearRect(0, 0, canvas.width, canvas.height);
        drawRotatedCylinder(100, 100, 180, 180, degreeAngle++);
}

请帮我解决上述问题

谢谢你们 诗

1 个答案:

答案 0 :(得分:20)

ErikMöller开发了一种坚固的polyfill,Paul Irish现在在他的blog post about requestAnimationFrame中托管。如果您使用此代码,则可以在几乎任何浏览器中透明地使用requestAnimationFrame:

(function() {
    var lastTime = 0;
    var vendors = ['webkit', 'moz'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame =
          window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());