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

use*_*621 9 javascript jquery canvas internet-explorer-9 html5-canvas

我正在创建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++);
}
Run Code Online (Sandbox Code Playgroud)

请帮我解决上述问题

谢谢玛哈

use*_*556 20

ErikMöller在他关于requestAnimationFrame的博客文章中开发了一个强大的polyfill,Paul Irish现在主持了这种填充.如果您使用此代码,则可以在几乎任何浏览器中透明地使用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);
        };
}());
Run Code Online (Sandbox Code Playgroud)