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.


    Toggling between drawing freely and straight lines

    Pythonista
    image draw
    3
    5
    3624
    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.
    • HT
      HT last edited by

      Hi everyone,

      I'm learning pythonista and am trying to work on this example script:
      https://gist.github.com/8f81907e4844d579ef8eda2ed5d65d5e

      It allows you to draw freely in the bottom view.
      I've added a switch, with which I want to change the drawing behaviour from drawing freely to only being able to draw straight lines.
      I want the app to move the line following the touch until the touch is ended, which is when the line is supposed to be drawn. I hope this is understandable.

      Can anyone give me some pointers?

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @HT last edited by

        @HT If I correctly understand:

            def touch_began(self, touch):
                self.xb, self.yb = touch.location
        
            def touch_moved(self, touch):
                x, y = touch.location
                self.path = ui.Path()
                self.path.line_width = 2.0
                self.path.line_join_style = ui.LINE_JOIN_ROUND
                self.path.line_cap_style = ui.LINE_CAP_ROUND
                self.path.move_to(self.xb, self.yb)
                self.path.line_to(x, y)
                self.set_needs_display()
        
        1 Reply Last reply Reply Quote 1
        • cvp
          cvp @HT last edited by

          @HT If next line must start from end of previous one:

              def __init__(self, frame):
                  self.frame = frame
                  self.flex = 'WH'
                  self.path = None
                  self.action = None
                  self.xb = None
          
              def touch_began(self, touch):
                  if not self.xb:
                    self.xb, self.yb = touch.location
          
              def touch_moved(self, touch):
                  x, y = touch.location
                  self.path = ui.Path()
                  self.path.line_width = 2.0
                  self.path.line_join_style = ui.LINE_JOIN_ROUND
                  self.path.line_cap_style = ui.LINE_CAP_ROUND
                  self.path.move_to(self.xb, self.yb)
                  self.path.line_to(x, y)
                  self.set_needs_display()
          
              def touch_ended(self, touch):
                  self.xb, self.yb = touch.prev_location
                   ...
          
          1 Reply Last reply Reply Quote 1
          • HT
            HT last edited by

            Thanks, that's exactly what I needed!

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              For what it's worth, one of my many abandoned projects, a "pen tool"
              https://gist.github.com/2d9e24f31f471f629d56912ead624538

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