Undo Redo for canvas


I am trying to add a undo redo method to my drawing app and have stumbled upon two ways in the community. One is to use saved images of the canvas but this would not work for me as the user can change the background color of the canvas or use a background image.

The second is this method:

I have looked over the blocks and it seems to me this just erases the previous lines which makes sense, but my question is: Does this also erase any lines that were drawn underneath?

Thank you,


There is also saving every line drawn and each time the user clicks undo, the whole canvas is cleared and redrawn from the saved lines but this gets very slow after a few lines.