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 to fix the position of a growing path properly?

    Pythonista
    4
    5
    1779
    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.
    • rownn
      rownn last edited by rownn

      Hey everyone,

      I have a ui.path in a shapeNode, which is growing somehow every second. Unfortunately, the path is jumping from state to state. I know why it is jumping and I also know for what this behavior is good for, but I‘d like to fix the path in center where it is meant to be. I guess playing around with the anchor_point would fix the problem only in some cases. Of course I could calculate around with the nodes bbox and the path bounds, but I cannot imagine that there is no better solution. I also thought about defining the size of path with a rect() before generating the actual path, but the resulting rectangle would be visible, of course. I hope you know what I’m talking about. Following a snippet which hopefully describes better what my long questions is about.

      from scene import *
      from time import time
      
      
      class MyScene (Scene):
      			
      	def setup(self):
      		self.points = (Point(-100, -100),Point(100, -100),Point(100, 100),Point(-200, 100))
      		self.startTime = time()
      		self.prev_dt = -1		
      		
      		ground = Node(parent=self)
      		ground.position = (self.size.w/2, self.size.h/2)
      		
      		self.node = ShapeNode(ui.Path.oval(0,0,10,10), fill_color='clear', stroke_color='red')
      		self.node2 = ShapeNode(ui.Path.oval(0,0,20,20), fill_color='clear', stroke_color='yellow')
      		
      		ground.add_child(self.node)
      		ground.add_child(self.node2)
      
      	
      	def update(self):
      		dt = int((time()-self.startTime)%4)
      
      		if dt != self.prev_dt:
      			self.prev_dt = dt
      			
      			path = ui.Path.oval(0,0,10,10)
      			path.move_to(self.points[0].x, self.points[0].y)
      			path.line_to(self.points[1].x, self.points[1].y)
      			
      			for i in range(dt): path.line_to(self.points[(i+2)%4].x,self.points[(i+2)%4].y)
      			
      			self.node.path = path
      
      if __name__ == '__main__':
      	run(MyScene(), show_fps=False) 
      

      Thx alot forfor every hint :)
      rownn

      mikael 1 Reply Last reply Reply Quote 0
      • stephen
        stephen last edited by

        @rownn hang in there ive been tying for like 4 hour lol

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

          The anchor point of 0,0 will fix the path in the center of the path's bounding box. But I gather you want the local path drawing coordinate 0,0 to be fixed in a particular spot.

          ShapeNodes are really just SpriteNodes -- the path is converted to an image, then acts just like a SpriteNode -- so it totally loses that internal coordinate system. You could perhaps draw to an ImageContext, where you maybe have slightly more control, but if your shape is allowed to grow to unlimited size, that wouldn't really work.

          There might be an option using ObjCInstance on your path, then callbounds -- that gives the bounds of the path in local path coords, I think, which would let you then easily adjust the anchor point, or use 0,0 anchor and some math to offset the x,y.

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

            @rownn, all that @JonB said, plus a thought for your ”drawing a rect first”, did you try drawing with transparent color?

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

              @mikael I have tried that before, but iirc it doesn't work.

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