Clear Canvas Rect (but keep background)

前端 未结 3 975
感情败类
感情败类 2020-12-05 19:54

I\'m trying to animate a circle and just moving it horizontally which works fine. However while the circle is moving, I have to do a clearRect over that circle so that it re

3条回答
  •  悲&欢浪女
    2020-12-05 20:45

    This is actually very easy to accomplish by simply positioning more than one canvas on top of each other. You can draw your background on a canvas that is (wait for it...) in the background, and draw your circles on a second canvas that is in the foreground. (i.e. stacked in front of the background canvas)

    Multiple canvases is actually one of the best ways to increase performance of anything animation where elements of the final image move independently and do not not necessarily move in every frame. This allows you avoid redrawing items that have not moved in every frame. However, one thing to keep in mind is that changing the relative depth (think z-index) of items drawn on different canvases now requires that the actual elements be reordered in the dom. In practice, this is rarely an issue for 2D games and animations.

提交回复
热议问题