Undo functionality in paint applications
I was working on a personal project that involved painting with an HTML canvas and wondered how easy it would be to incorporate 'undo functionality'? So I did a little research to see what people were proposing, and whilst I came across a few suggestions, none of them included an example of an implementation. Therefore I decided to look into the matter myself and share the result.
Download the code for the example Paint application on GitHub.
Choosing the pattern
As with most challenges, there are multiple ways to approach the problem. In this case, the two most frequently suggested methods are to use either the Command or Memento design patterns. The Memento pattern is designed to save the state ('mementos') of an object, whilst the Command pattern is designed to change the state of the object by using 'Commands', which can be invoked repeatably.
After some deliberation, I chose the Command pattern as it provides an elegant solution for encapsulating the drawing logic. Added to this, it doesn't consume nearly as much memory as the Memento pattern, in which I was required to save the state of the canvas each time a modification was made.
Implementing the Command pattern
As previously touched upon, the Command pattern is used as a means to encapsulate behavioural logic in order for it to be executed at a later date. This pattern, explained in greater detail on wikipedia, consists of four parts; the Client, Receiver, Command and Invoker. All of these are described below in further detail:
The Client is where most of the application's logic is stored. The example application's Client is responsible for creating the commands to draw the shapes and clear the canvas. It then adds them to the invoker, which we'll come to later.
The code for the Client includes the 'undo' function; this is where the application uses the Invoker to undo the last executed command. The 3 steps below show how the logic is executed when the 'undo' button is clicked...
- Remove the last Command added to the Invoker.
- Clear the canvas by executing the ClearCanvasCommand, without adding it to the Invoker.
- Execute all the Commands in the order they were added to the Invoker.
It is worth noting that there are other ways to implement undo functionality with the Command pattern. For example, providing each Command with their own '
undo' function in order to reverse the actions they carried out against the Receiver when the '
execute' function was called. However, the problem with this is that the Commands in the example application will be modifying an HTML Canvas and therefore make reversing an action against it very difficult.
The Receiver, which in this instance is the Canvas, is acted upon by commands.
The Command section provides an example of how a command is used to draw a square on the Canvas (Receiver). As with all the Commands, when this command is instantiated it is provided with a reference to the canvas. The command's '
execute' function, once invoked, will then use the canvas' '
rect' and '
stroke' functions to draw the square.
The Invoker exposes functions for executing and managing a stack of Commands. The two most important functions for the undo functionality are the '
removeLast' and '
The Command modifies the state of the Receiver and at the point of instantiation, a reference is provided for the Receiver, which then alters it when executed. This process can be seen in the code below, which is responsible for drawing a square of a fixed dimension at the co-ordinate it's given. This occurs when its '
execute' function is called by the Invoker that it has been added to.
Overall, there are many different ways to implement undo functionality, such as using the Momento pattern or different variations of the Command pattern. However, I believe the aforementioned implementation of the Command pattern, which can be found on GitHub, was by far the simplest and most efficient method when working with the HTML Canvas.