I am drawing a graph on a <canvas>
that requires expensive calculations. I would like to create an animation (when moving the mouse across the canvas) where the graph is unchanging, but some other objects are drawn over it.
Because the canvas will have to be redrawn a lot, I don't want to perform the calculations to render the graph for every frame. How can I draw the graph once, save it, and then use the saved rendering to redraw subsequent frames of the animation, so that the expensive calculations only have to happen once & all I have to redraw is the much simpler animation layer?
I tried drawing the graph on a second canvas & then using ctx.drawImage()
to render it onto the main canvas, but drawing on the canvas doesn't seem to work unless it's in the dom & not display:none;
. Do I have to do something hacky like position the temp canvas out of view, or is there a cleaner way to do this?