omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    How can I add an easer in “sketch.py” in Example?

    Pythonista
    eraser pi.path sketch.py
    3
    14
    3202
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • satsuki.kojima
      satsuki.kojima last edited by

      I want to customize “sketch.py” in Example. I want to add a button to switch to an eraser mode which enables to erase the scribbled line. “sketch.py” is very simple using only ui.Path to draw, and I first thought that changing color of the path (to white which is the same color of the view’s background) would do. However, ui.Path has no attribute for setting it’s color (is it correct?). Is there any good way to add the eraser function?

      cvp 3 Replies Last reply Reply Quote 0
      • cvp
        cvp @satsuki.kojima last edited by cvp

        @satsuki.kojima said:

        ui.Path has no attribute for setting it’s color (is it correct?)

        No 😀

        During drawing

        	def draw(self):
        		if self.path:
        			ui.set_color('red')
        			self.path.stroke()
        

        After saving

        	def path_action(self, sender):
        		path = sender.path
        		old_img = self.image_view.image
        		width, height = self.image_view.width, self.image_view.height
        		with ui.ImageContext(width, height) as ctx:
        			if old_img:
        				old_img.draw()
        			ui.set_color('blue')
        			path.stroke()
        			self.image_view.image = ctx.get_image()
        

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @satsuki.kojima last edited by

          @satsuki.kojima said:

          Is there any good way to add the eraser function?

          That's what the clear button does, isn'it?

          mikael 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @cvp last edited by mikael

            @cvp, I guess the idea is to add an eraser that clears some parts of the image.

            @satsuki-kojima:

            This is challenging because Path is a vector, so you would need to see which of the paths points are covered by the eraser, and remove & split the path accordingly. This would not be perfect if the line was drawn fast and has longer straight segments.

            Alternative is to turn the image to a bitmap when eraser is used. Depending on what you want to use the image for, loss of vector information is probably not an issue.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @mikael last edited by

              @mikael ok, agree. But small questions imply short answers 😀 And, of course, we're ready to help more during a project.

              1 Reply Last reply Reply Quote 1
              • cvp
                cvp @satsuki.kojima last edited by cvp

                @satsuki.kojima you could try this (a very little) modified MySketch.py

                mikael satsuki.kojima 2 Replies Last reply Reply Quote 0
                • mikael
                  mikael @cvp last edited by

                  @cvp, aa, a white, fatter path, of course.

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @mikael last edited by

                    @mikael ok, next button = line width 😀

                    1 Reply Last reply Reply Quote 0
                    • satsuki.kojima
                      satsuki.kojima @cvp last edited by

                      to :@cvp
                      Thank you, it helps a lot. I may borrow your color palette to my app as well.

                      cvp 2 Replies Last reply Reply Quote 0
                      • cvp
                        cvp @satsuki.kojima last edited by

                        @satsuki.kojima said:

                        your color palette

                        Not mine, old sample from, I think, Pythonista 's creator (or I even forgot I did it, very old brain)

                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @satsuki.kojima last edited by cvp

                          @satsuki.kojima for the fun, MySketch.py modified for width (idea of @mikael )

                          satsuki.kojima 1 Reply Last reply Reply Quote 1
                          • cvp
                            cvp last edited by cvp

                            Small modif of gist to 'show' white color in slider

                            1 Reply Last reply Reply Quote 0
                            • satsuki.kojima
                              satsuki.kojima @cvp last edited by

                              to:@cvp
                              Yeah.... I finally decided my eraser to set the line_width to 20 while the default pen to 2.
                              I’m building a game “4 numbers”, which is to guess 4 randomly given numbers by hints of hits and blows. I wanted to add a sketch board so the players can scribble their process of guessing.

                              By the way, I actually took for a while to fix my problem even though your advise was very clear. My sketch board showed some odd behavior. I finally found it was because I was writing my program adding subview of path_view before image_view like as below.

                              	iv = ui.ImageView(frame=(0, 0, width, height))
                              	pv = PathView(frame=self.bounds)
                              	pv.action = self.path_action
                              	pv.main = self
                              	self.add_subview(pv).  [ <- shoud be after self.add_subview(iv)]
                              	self.add_subview(iv)
                              
                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @satsuki.kojima last edited by

                                @satsuki.kojima 👍

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors