views:

1004

answers:

1

I'm just started on playing around with the canvas HTML5-object. For the sake of performance tests, I have made a little ping pong game.

Are there any performance improvements I could use?

The ball seems to be blue with a touch of red, but my declaration it should be yellow. How can I fix this?

+1  A: 

This should help you with the ball color;

function drawBall (x, y, r) {
    ctx.beginPath();
    ctx.fillStyle = "yellow";
    ctx.arc(x, y, r, 0, Math.PI*2, false);
    ctx.closePath();
    ctx.fill(); //added
    fps++;
}

function drawP1 (h) {
    ctx.fillStyle = '#FF0000';
    ctx.fillRect(0, h, 20, 100);
    //ctx.fill(); // remove in P2 also
    fps++;
    return true;
};

fill() doesn't apply to fillRect(), the latter is drawing a filled rectagle and fill() is to fill pathes.

There's not much you can improve with an simple pong game, but i'll give some general advices for canvas performance:

  • fillStyle/strokeStyle calls are expensive, avoid switching colors.
  • drawing complex shapes is expensive, too. you can draw then and use the pixel api to render them
  • try to keep rendering and processing separated, this will give you space for improvements
  • try to use pure js for high FPS games/animations

As said, very general advices and might not be appropriate for every case.

elias
Just tryed it - but no difference. And as said at the mozilla-homepage: "Note: When calling the fill method any open shapes will be closed automatically and it isn't necessary to use the closePath method." - I just don't get it, why it is not working
Vilius
---Edit: thanks for the usefull hints
Vilius
Hmm, tried it myself and doesn't work for me either. Can't find the problem..
elias
Ok, thank you very much - removing of the fill()-method was the solution!
Vilius
your wlecome, greetings from germany ;)
elias