简体   繁体   English

间隔动画和requestAnimationFrame之间的区别

[英]The difference between interval animation and requestAnimationFrame

What is the difference between those two? 两者之间有什么区别? requestAnimationFrame should be "intervaled" or invoked cca every 60milisecond on 60fps (depending on screen) and with setInterval you can set delay interval of invokation of function. requestAnimationFrame应该“间隔”或以60fps每60毫秒调用一次cca(取决于屏幕),并使用setInterval可以设置函数调用的延迟间隔。 Yet i have made simple drawning animation with both , interval and requestAnimationFrame and it seems that interval function is smoother and work properly, while requestAnimation function broke browser (tested on mozilla/chrome and on 2 Pcs). 然而,我使用,interval和requestAnimationFrame都制作了简单的绘制动画,似乎interval函数更流畅并且可以正常工作,而requestAnimation函数破坏了浏览器(在mozilla / chrome和2 Pcs上进行了测试)。

interval function: 间隔函数:

function animate(x) {
    var start = new Date();
    var id = setInterval(function () {
        var timepassed = new Date() - start;
        var progress = timepassed / x.duration;
        if (progress > 1) {
            progress = 1;
        }
        var delta = x.delta(progress);
        x.step(delta);
        if (progress == 1) {
            clearInterval(id);
        }
    }, x.delay);


}

requestAnimationFrame function: requestAnimationFrame函数:

function animate(x) {
    var start = new Date();
    var id = function () {

        var timepassed = new Date() - start;
        var progress = timepassed / x.duration;
        if (progress > 1) {
            progress = 1;
        }
        var delta = x.delta(progress);

        x.step(delta);
        requestAnimationFrame(id);
        if (progress < 1) {
            requestAnimationFrame(id);
        }
    }
    requestAnimationFrame(id)


}

function to do drawning 做图的功能

function move(delta) {
    var c = document.getElementById("myCanvas");


var ctx = c.getContext("2d");

    animate({
        delay:10,
        duration: 4000,
        delta: delta,
        step: function (delta) {
            ctx.clearRect(0, 0, c.width, c.height);
            ctx.beginPath();

            ctx.strokeStyle="red";
             ctx.lineWidth = 15;
ctx.arc(150, 150, 70, 0*Math.PI, delta*(2 * Math.PI));
            ctx.font="40px Georgia";
ctx.fillText((delta*100).toFixed(0)+"%",95,150);

ctx.stroke(); ;
        }


    });
}
move(function(p){return p});

demo for interval : http://jsfiddle.net/Trolstover/5tmu4j6z/ demo for requestAnimationFrame : http://jsfiddle.net/Trolstover/5tmu4j6z/1 间隔演示: http : //jsfiddle.net/Trolstover/5tmu4j6z/ requestAnimationFrame演示: http : //jsfiddle.net/Trolstover/5tmu4j6z/1

just comment out 只是注释掉

 //requestAnimationFrame(id);
        var timepassed = new Date() - start

see this: http://jsfiddle.net/5tmu4j6z/2/ 看到这个: http : //jsfiddle.net/5tmu4j6z/2/

After you call animate setInterval will run starting at the first interval, while requestAnimation will run before the next repaint of your canvas. 调用animate setInterval将在第一个间隔开始运行,而requestAnimation将在画布的下一次重绘之前运行。 The reason requestAnimation runs again is because it's recursive. requestAnimation再次运行的原因是因为它是递归的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM