画布中的不稳定动画

时间:2010-12-15 05:25:15

标签: javascript jquery firefox canvas svg

我的画布动画像镀铬中的冰一样光滑,但在firefox中作为一个糟糕的发型不稳定。最奇怪的是,它甚至不是一个复杂的计算。有没有人看到任何错误(性能相关)我的代码可能会导致这种放缓?

这里是jsfiddle: http://jsfiddle.net/Wu5Jh/

这是SO:

$(document).ready(function(){
    //vars
    var x = 20,
            y = 20,
            pi = Math.PI,
            width,
            height,
            complete = 100,
            refreshInterval,
            ctx;

    // computed vars
    function init() {
      ctx = $('#canvas')[0].getContext("2d");
      width = $("#canvas").width();
      height = $("#canvas").height();  
        center = [width/2, height/2];
      refreshInterval = (function doSetTimeout(){
            draw();
            setTimeout(doSetTimeout, 30);
            })();
        };

    function circle(x,y,r) {

        // Draw the growing circle
        ctx.fillStyle = "#09f";
      ctx.beginPath();
    ctx.moveTo(x, y); // center of the pie
      ctx.arc(
            x, 
            y, 
            r, 
            -2*pi*complete/100 + pi*1.5,
            -pi*.5,
            true
        );
        ctx.lineTo(x, y);
      ctx.closePath();
      ctx.fill();

        // Draw the cutout
        ctx.globalCompositeOperation = "xor";
        ctx.fillStyle = "#fff";
        ctx.beginPath();  
        ctx.arc(x,y,r/2,0,pi*2,true); 
        ctx.fill();
    }

    function clear() {
      ctx.clearRect(0, 0, width, height);
    }

    function timeCheck(){
        if (complete>0){
            complete -= 1;
        }
        else {
            clearInterval(refreshInterval);
        }
    }

    function draw() {
      clear();
      circle(x, y, 20);
        timeCheck();
    }

    init();

});

我希望避免使用flash动画或gif,但我可能别无选择。

谢谢!

1 个答案:

答案 0 :(得分:1)

我没有看到任何问题,我在Linux上使用Chromium 8和Firefox 3.6.13。

但是,如果您仍然需要微优化的建议,那么您可以将-2*pi/1001.5*pi.5*pi等内容作为自己的常量。此外,这只是猜测,但"copy"使用"xor"代替ctx.globalCompositeOperation可能会更快。您还可以存储为第一个绘制弧绘制的弧角,并将其用于第二个弧,而不是仅仅绘制一个完整的圆。

相关问题