clearRect not working

前端 未结 1 1032
有刺的猬
有刺的猬 2020-12-19 03:08

I\'m doing a Pong game in javascript in order to learn making games, and I want to make it object oriented.

I can\'t get clearRect to work. All it does

相关标签:
1条回答
  • 2020-12-19 03:30

    Your real problem is you are not closing your circle's path.

    Add ctx.beginPath() before you draw the circle.

    jsFiddle.

    Also, some tips...

    • Your assets should not be responsible for drawing themselves (their draw() method). Instead, perhaps define their visual properties (is it a circle? radius?) and let your main render function handle canvas specific drawing (this also has the advantage that you can switch your renderer to regular DOM elements or WebGL further down the track easily).
    • Instead of setInterval(), use requestAnimationFrame(). Support is not that great at the moment so you may want to shim its functionality with setInterval() or the recursive setTimeout() pattern.
    • Your clearRect() should be passed the dimensions from the canvas element (or have them defined somewhere). Including them in your rendering functions is akin to magic numbers and could lead to a maintenance issue further down the track.
    0 讨论(0)
提交回复
热议问题