javascript - Why does this code run slow in firefox? -


so wrote code simple game. code runs @ 60 fps in both chrome , safari firefox barely manages 30-40 fps. code looks simple enough me. causing delay?

i checked in firebug , found out 1 function "follow" taking time. here code:

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       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;     (i=0 ; < 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 && === 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();     (i = 0, len = balls.length - 1; < len; i++) {           balla = balls[i];           (j = + 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(); }; 

here animation: http://ipsumturpis.xtreemhost.com/follower/index.html

i vaguely remembered there used problem in ff regarding canvas drawing performance, have commented out drawcircle(obj.x,obj.y,obj.rad, obj.color); , poof, magic happened - frame rate went 11 fps 60.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -