Skip to content Skip to sidebar Skip to footer

HTML5 Canvas Slows Down With Each Stroke And Clear

I've been playing around with the HTML5 Canvas and I've noticed something that I couldn't find a resolution for online. Here's the simple code I'm playing with

Solution 1:

The <canvas> element keeps track of a current path (i.e., set of points, lines, and curves). canvas.moveTo, canvas.lineTo, and canvas.stroke all operate on the current path. Every time you call canvas.moveTo or canvas.lineTo you are adding to the current path. As the path gets more and more complex, drawing gets slower and slower.

You can clear the path by calling canvas.beginPath(). Doing this at the start of your draw function should get rid of the slowdown.


Post a Comment for "HTML5 Canvas Slows Down With Each Stroke And Clear"