Draw Lines with Undo

Question on Flashn00bz mailing list stuck with me today. A bloke wanted to draw some lines, one at a time, and then have an undo that you could actually see. I figured the command pattern was where it was at.

Upon getting home, I tried working on a side project, but couldn’t concentrate (lot on my mind), so I fiddled with this. Was a lot easier than using that bloody pattern. It has undo as well as undo all, and you can see them undo. Draw on the gray square via clicking and holding, drag your mouse, and let go of left mouse click.

Drawing Lines with Undo – HTML | ZIP

4 Replies to “Draw Lines with Undo”

  1. hey, just curious as to why you do this in the code:

    Key.removeListener(this);Key.addListener(this);

    looks like you do it with all your listener objects.

  2. Paranoia… or no, uh, best practice… yeah yeah, best practice. What would happen if some goob called init twice? For classes, you should allow someone to re-initialize the entire class without having them to have to re-create an instance, via the init function.

    I tend to treat simple root level scripts like that too. You can re-init the simple app via calling the init function. Those calls ensure you won’t accidentally have the event handler registered twice. If you didn’t, and you called init twice, then when you clicked undo, it’d undo twice. See what I mean?

  3. Jesse – you helped me on the Flex mailing list a while back with a question about drawing (I think it is even elsewhere on your blog) but what you have here is exactly the type of thing I was interested in doing but in Flex. In short, I want to drag custom components onto a canvas, draw lines that will connect them and use the dynamic abilities of Flex to allow users to make extremely simple applications. Without resorting to Flash directly, can this be done? (As soon as I get outside Flex explicitly, I feel somewhat lost). Thanks for any advice you might have. -M

Comments are closed.