Scripton / Docs

Shapes

The canvas API includes methods for drawing simple shapes and lines.

  • For drawing more elaborate shapes, see the documentation on paths.
  • For notes on how position coordinates in the methods below are interpreted, see the section on coordinates.

Rectangles

The draw_rect method of a Canvas instance renders a rectangle specified by its top-left coordinate (x, y) along with its width and height.

def draw_rect( self, x: Number, y: Number, width: Number, height: Number, *, fill: ColorStyle | None = None, stroke: ColorStyle | None = None, thickness: Number | None = None, dash: Sequence[Number] | None = None ) -> None: ...
  • The fill parameter accepts an optional color or a gradient for filling the interior of the rectangle.

    from scripton.canvas import Canvas canvas = Canvas( width=300, height=200, fill='#313268' ) canvas.draw_rect( x=100, y=50, width=100, height=100, fill='#FAB526' )

  • Similarly, the optional stroke parameter specifies the color of the rectangle's outline.

    canvas.draw_rect( x=100, y=50, width=100, height=100, stroke='#D38EE7' )

  • The thickness parameter controls the outline's thickness

    canvas.draw_rect( x=100, y=50, width=100, height=100, stroke='#D38EE7', thickness=8 )

  • The dash parameter can be used for creating dashed lines. It's interpreted as, alternatingly, the length of the line followed by the length of the gap. Odd length arrays are auto replicated (eg: [1, 2, 3][1, 2, 3, 1, 2, 3]) .

    canvas.draw_rect( x=100, y=50, width=100, height=100, stroke='#D38EE7', dash=(4, 2) )

Circles

The draw_circle method of a Canvas instance renders a circle specified by its center (x, y) and radius.

def draw_circle( self, x: Number, y: Number, radius: Number, *, fill: ColorStyle | None = None, stroke: ColorStyle | None = None, thickness: Number | None = None, dash: Sequence[Number] | None = None ) -> None: ...

The fill, stroke, thickness, and dash parameters behavior the same as in the case of a rectangle.

  • Example: Drawing nested circles

    from scripton.canvas import Canvas canvas = Canvas(width=300, height=200) colors = ('#471437', '#B13254', '#FF5449', '#FF7349', '#FF9249') for idx, color in enumerate(colors): canvas.draw_circle( x=150, y=100 + 10 * idx, radius=100 - 20 * idx, fill=color )

Lines

The draw_line method of a Canvas instance renders a line segment from point (x0, y0) to point (x1, y1). The stroke keyword argument specifies the color of the line.

def draw_line( self, x0: Number, y0: Number, x1: Number, y1: Number, *, stroke: ColorStyle, thickness: Number = 1.0, dash: DashSpec | None = None ) -> None: ...
  • Example: A simple line

    from scripton.canvas import Canvas canvas = Canvas(width=300, height=200, fill='#313268') canvas.draw_line( x0=30, y0=20, x1=270, y1=180, stroke='#D38EE7' )