为什么这个代码在firefox中运行缓慢?

时间:2012-06-10 00:33:35

标签: javascript jquery performance html5 firefox

所以我为一个简单的游戏编写了这段代码。 Chrome和Safari的代码运行速度为60 fps,但Firefox几乎不能管理30-40 fps。代码看起来很简单。什么可能导致延迟?

我检查了萤火虫,发现只有一个“跟随”功能一直在占用。这是代码:

function checkCollision (ball0, ball1) {
    var dx = ball1.X - ball0.X,
        dy = ball1.Y - ball0.Y,
        dist = Math.sqrt(dx * dx + dy * dy);

    if (dist < ball0.rad + ball1.rad) {
      var angle = Math.atan2(dy, dx),
          sin = Math.sin(angle),
          cos = Math.cos(angle);

          var pos0 = {x: 0, y: 0}, 
          pos1 = rotate(dx, dy, sin, cos, true),
          vel0 = rotate(ball0.spdX, ball0.spdY, sin, cos, true),
          vel1 = rotate(ball1.spdX, ball1.spdY, sin, cos, true),
          vxTotal = vel0.x - vel1.x;
      vel0.x = ((ball0.mass - ball1.mass) * vel0.x + 2 * ball1.mass * vel1.x) /
               (ball0.mass + ball1.mass);
      vel1.x = vxTotal + vel0.x;
      var absV = Math.abs(vel0.x) + Math.abs(vel1.x),
          overlap = (ball0.rad + ball1.rad) - Math.abs(pos0.x - pos1.x);
      pos0.x += vel0.x / absV * overlap;
      pos1.x += vel1.x / absV * overlap;
      //rotate positions back
      var pos0F = rotate(pos0.x, pos0.y, sin, cos, false),
          pos1F = rotate(pos1.x, pos1.y, sin, cos, false);
      ball1.X = ball0.X + pos1F.x;
      ball1.Y = ball0.Y + pos1F.y;
      ball0.X = ball0.X + pos0F.x;
      ball0.Y = ball0.Y + pos0F.y;
      var vel0F = rotate(vel0.x, vel0.y, sin, cos, false),
          vel1F = rotate(vel1.x, vel1.y, sin, cos, false);
      ball0.spdX = vel0F.x;
      ball0.spdY = vel0F.y;
      ball1.spdX = vel1F.x;
      ball1.spdY = vel1F.y;
    }
}

function move()
{
    var side,i;
    for (i=0 ; i < balls.length; i++)
    {   
        var obj = balls[i];
        if (side=obj.edgeX())
        {
            if (side === 'l')
                obj.X = obj.rad;
            else if (side === 'r')
                obj.X = canvas.width() - obj.rad;
            obj.spdX*=-1;
        }
        if (side=obj.edgeY())
        {
            if (side === 't')
                obj.Y = obj.rad;
            else if (side === 'b')
                obj.Y = canvas.height() - obj.rad;
            obj.spdY*=-1;
        }

        if (leash == true && i === 0)
        {
            if (mouse.X>obj.X && (obj.spdX<10))
                obj.spdX+=obj.accX;
            else if (mouse.X<obj.X && (obj.spdX>-10))
                obj.spdX-=obj.accX;
            if (mouse.Y>obj.Y && (obj.spdY<10))
                obj.spdY+=obj.accY;
            else if (mouse.Y<obj.Y && (obj.spdY>-10))
                obj.spdY-=obj.accY;
        }


        obj.X+=obj.spdX;
        obj.Y+=obj.spdY;
        if (Math.abs(obj.spdX)>0.1)
            obj.spdX*=0.98;
        else obj.spdX=0;
        if (Math.abs(obj.spdY)>0.1)
            obj.spdY*=0.98;
        else obj.spdY = 0;
    };
}

function follow()
{
    var ballA, i, ballB,j;
    requestAnimationFrame(follow);
    //stats.begin();
    context.clearRect(0,0,canvas.width(),canvas.height());

    move();
    for (i = 0, len = balls.length - 1; i < len; i++) {
          ballA = balls[i];
          for (j = i + 1; j < balls.length; j++) {
            ballB = balls[j];
            checkCollision(ballA, ballB);
          }
     }
    balls.forEach(function(obj){
        drawCircle(obj.X,obj.Y,obj.rad, obj.color);
        if (leash == true && obj.control === true)
        {drawLeash(mouse.X,mouse.Y,obj.X,obj.Y,obj.color);}
    });
    //stats.end();
};

这是动画:http://ipsumturpis.xtreemhost.com/follower/index.html

2 个答案:

答案 0 :(得分:2)

我依旧记得FF在画布绘制性能方面曾经存在问题,所以我评论了drawCircle(obj.X,obj.Y,obj.rad, obj.color);和poof,魔术发生了 - 我的帧速率从11 FPS上升到60。

答案 1 :(得分:1)

尝试在变量中缓存球长度。除非由于我没有看到的原因绝对必要,否则在循环的每次迭代中运行balls.length(或任何函数)自然会非常耗时。

所以尝试这样的事情;

ballslen = balls.length;
for (j = i + 1; j < ballslen; j++)