-
Hi, i'd like to be able to apply a transform to a rectangle after creation. My usecase is that I have a 100x100 canvas and want to draw a small symbol at 12 defined positions on the canvas. So i want to do this:
The Drawing is created from a text file that defines the small symbol. It can be a line, circle, rectangle. The text file reads (partly) like this:
which means "create a line and two filled circles on top". How can I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Your code is very close to working. What you want is the import drawsvg as draw
d = draw.Drawing(100, 100, origin='center')
g = (-5, -5, 5, 5)
rect_raw = draw.Rectangle(g[0], g[1], g[2], g[3],
stroke='black', fill='none', stroke_width=2)
positions = (
(16, 9),
(84, 9),
(49, 16),
(23, 37),
# more positions...
)
for x, y in positions:
x = x - 50
y = y - 50
d.append(draw.Use(rect_raw, x, y)) # <-- This line
d To explain why your code didn't work, Also, notice that every loop will modify the transform of the same rectangle object. Instead of drawing four rectangles at different locations, the code will draw the same rectangle four times at the last position (23, 37). |
Beta Was this translation helpful? Give feedback.
Your code is very close to working. What you want is the
Use
element which draws a duplicate of the given shape at a specific x, y offset:d.append(draw.Use(my_shape, x_offset, y_offset))
To explain why your code didn't work,
rect_raw.transform
is not where the SVG attribute is…