Problem positioning ShapeNode
I’m trying to draw a box in the center of the screen using the below. The box is drawn centered about the origin (lower left corner of the screen). It doesn’t seem to respect the x and y values I’m giving it. I’m sure I’m missing something obvious :). Can anyone point me in the right direction?
import scene as sc import ui class MyScene (sc.Scene): def setup(s): s.background_color = 'blue' s.bdr = sc.ShapeNode() s.bdr.fill_color = (0,0,0,0) s.bdr.stroke_color = 'yellow' cx = s.size.width cy = s.size.height s.bdr.path = ui.Path.rect(cx / 2, cy / 2, 100, 100) s.bdr.line_width = 3 s.add_child(s.bdr) sc.run(MyScene())
s.bdr = sc.ShapeNode() s.bdr.position = s.size/2
Just to expand a bit, shapenode can be a bit confusing and frustrating to work with.
When you draw a path, that path has some coordinate system -- eg your line_to's, rects, etc.
But that natural coordinate system basically gets thrown away -- the path is converted to an image, then the bounding box of that image becomes the new coordinate system (you can define the anchor_point to position the Sprite based on the corner, or center).
What that means is that if you had Path drawing code that worked inside draw(), and want to convert individual paths to ShapeNodes, the relative positions get all screwed up, because there is not a good way to know for complex paths where the bottom left corner of the bounds will be. As a simple example, line_to(0,0,50,50) will have the anchor point at bottom left as you might expect. But line_to(0,0,50,-50) will not even have the anchor point on the line, unless you manually adjust anchor_point =[0,1]
Thanks for the replies! After reading them a few times and experimenting I think I'm understanding it now.
To create and position a rect using paths, I'm creating it with ui.Path.rect() and just giving 0,0 for the position with the desired size. Then I'm setting the position property to locate the center where I want it.
Seems to be working, thanks for the help!