Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Undo last line drawn
-
Hello!
I'm drawing lines like this:
def drawLine(self,x1,y1,x2,y2): self.path = ui.Path() self.path.line_width = 8.0 self.path.line_join_style = ui.LINE_JOIN_ROUND self.path.line_cap_style = ui.LINE_CAP_ROUND self.path.move_to(x1, y1) self.path.line_to(x2, y2) self.set_needs_display()
Is there a way to undo the last line drawn?
Thanks in advance!
-
One possible way I was thinking of (if a bit clunky), would be to draw a white line over the black one to "erase" it.
But I can't figure out how to change the color of the line. ui.set_color doesnt seem to do anything. Does anybody know how to change the color?
-
you need to store lines as a list operations, not the actual path. then regenrate the path when needed.
-
Just to add a bit to the previous answer:
Setup a new method which strictly redraws the self.path object, with the exception that you will maintain a list(), which contains all coordinates required paths (each line drawn)
In the case of undo, pop the last item from the list and call your redraw function which will replay each draw op (except for the last)
You will have to keep a list of "redo" as well for completeness, if you do this, before you pop the last result, add it to another list holding the redo actions.
If I get time I will post an example- but TBH don't hold your breath, got a 2 month old who owns me now.
-
Thanks guys, I got it to work with the saved coordinates. I would post the code, but it's pretty long and convoluted.
-
The problem I'm having now is that pythonista crashes if the array of points gets too long (I add the touch locations during touch_moved). It happens pretty fast when I'm drawing freely. Unfortunately there is no error message. Has anyone come across this problem?
-
you might need to post your code. likely you have some memory issues, though i know that huge paths are going to be slow.. one option is to convert path to image after N segments. another option migt be to not create a single large path, but lots of smaller ones.
-
@reticulated If, like asked in your other topic, you want to smooth draw out your lines, you could only store some points, not all, only if they are not too close of previous one.,
-
You are right, the problem was, that I drew each little line as a path individually. Now that I create one big path and draw it once, it works. Thanks!