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.
Can't fill path
-
Okay, so I'm trying to use the canvas to draw a trapezoid. Here's my code. For some reason it just creates an empty box.
# coding: utf-8 import canvas b = 10 canvas.set_size(500, 500 + b) canvas.draw_rect(0, 0 + b, 500, 500) canvas.set_fill_color(1, 0.1, 0.1) canvas.set_stroke_color(1, 0, 0) canvas.set_line_width(4) canvas.begin_path() canvas.add_line(150, 100 + b) canvas.add_line(350, 100 + b) canvas.add_line(400, 200 + b) canvas.add_line(100, 200 + b) canvas.add_line(150, 100 + b) canvas.draw_path() canvas.fill_path() canvas.close_path()
-
A few things:
- There are two sets of functions in the
canvas
module - there are thedraw_something
andfill_something
functions, which draw shapes to the canvas immediately, and there are theadd_something
functions, which add drawings to a "path" that you can display withdraw_path
orfill_path
. - When you call
draw_path
orfill_path
, the path is drawn and then cleared. This means that when you calldraw_path
(creating the outline) it also gets cleared from memory, so when you callfill_path
afterwards there is nothing there to fill. close_path
doesn't do what you think it does (probably). The way you use it, it looks like you thinkclose_path
is required to "finish" a path started withbegin_path
- this is not the case. What it does is connect the current "drawing point" of the path with the point where you started the path, making the line into a closed shape. For example, when you add lines to a path in an L shape and then callclose_path
, you connect the two ends of the L with a line and get a triangle.- I might be wrong, but it looks like when adding lines to a path you need to start with a
move_to
call to set the path's starting point. When I didn't callmove_to
, the lines I added were not drawn.
- There are two sets of functions in the
-
The canvas is also somewhat "depreciated" I think -- the ui or scene drawing functions are a little more complete. For instance, using a ui.Path, you could fill then stroke.
-
Example with ImageDraw:
import Image, ImageDraw b = 10 imagebuffer = Image.new('RGBA', (501, 500 + b), 'white') drawbuffer = ImageDraw.Draw(imagebuffer) drawbuffer.rectangle((0, b, 500, 500), outline='red') drawpoints = [(150, 100 + b), (350, 100 + b), (400, 200 + b), (100, 200 + b), (150, 100 + b)] #drawbuffer.line(drawpoints, fill='red', width=4) drawbuffer.polygon(drawpoints, fill='red') #filled path imagebuffer.show()