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 !
I would also suggest that you modify the empty draw method to
void draw(){
drawPermanentLayer();
}
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.
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.