Drawing on multiple layers interactively (AutoCad style)

I am sharing here a simple code I developed to handle the user interaction for a simple drawing tool.

The user can draw points dragging the mouse, or lines, if pressing SHIFT at the beginning of the drag. The cool thing is that lines are drawn dynamically, until the user releases the mouse, with the previous drawing history kept intact.

This is the link to the GitHub repository for this sketch: I hope it can serve you as a convenience to add to your project. Enjoy !


Thanks for sharing this!

Just one small note – you could simplify this a bit if you want by using Processing’s built-in point class, PVector, instead of Point.pde.

1 Like

I would also suggest that you modify the empty draw method to

void draw(){

When looking at sketch code people at the draw method to see what might be happening an empty method is confusing. Also in this case you can remove all the other stemsnts drawPermanentLayer() from your code, avoiding duplication.


Right ! I was too lazy to look it up, even if I remembered that class ! :upside_down_face:

Thank you for your suggestion and comment, I appreciate it !

1 Like

That’s an interesting suggestion, thank you for taking the time to review the code and then writing this !

I have modified the sketch a bit, in order to implement your suggestion; meanwhile I was coding, I also had the chance to better understand the role of a fully transparent background (alpha value set to 0): I may eventually discuss this in a future post.

The updated code that includes your suggestion is already on the aforementioned repo on GitHub, so anyone can check this new implementation, if they desire so.

Thank you everyone for the suggestions ! (I have also implemented @jeremydouglass ’ suggestion ! Thank you !)


Here’s a post that I just created on my blog, with some clarifications about how to use multiple layers to draw stacks of graphical objects, without losing the history of the other drawings already made on the canvas, together without creating an undesired trail ! You can find the code on my GitHub profile here, and also a link to the same repo on the blog post.

The post and the code clarify the usage of a fully transparent (0 opacity, i.e. alpha value set to 0) background, together with the usage of multiple, stacked layers.

I hope this can be useful ! Have a nice day :palm_tree: