PyCairo – Drawing different Pen dashes
Last Updated :
12 Nov, 2020
Each line can be drawn with different pen dashes. A pen dash can be defined as the style of a line. The dash pattern is specified by the set_dash( ) method. The pattern is set by the dash list which of floating value. They can set the on and off part of the dashes in pattern. The dashes is used by the stroke( ) method to create a line pattern. If the number of dashes is 0, dashing is disabled and a plane simple line will be drawn. If the number of dashes is 1, a symmetric pattern is assumed with alternative on and off portion of the size specified as the single values in dash.
Steps of Implementation :
- Import the Pycairo module.
- Create a SVG surface object and add context to it.
- Setting color of the context & line width
- Setting of line pattern using set_dash( )
- Creating a line.
Example 1 :
Python3
import cairo
with cairo.SVGSurface( "geekline.svg" , 700 , 700 ) as surface:
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 4 )
context.set_dash([ 4.0 , 21.0 , 2.0 ])
context.move_to( 40 , 30 )
context.line_to( 250 , 30 )
context.stroke()
print ( "File Saved" )
|
context.set_dash ( [ 4.0, 21.0, 2.0 ] )
We have 4 points drawn, 21 not drawn, and 2 drawn, then 4 points not drawn, 21 points drawn. and 2 not drawn.
Output :
Example 2 :
Python3
import cairo
with cairo.SVGSurface( "geekline.svg" , 700 , 700 ) as surface:
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 4 )
context.set_dash([ 14.0 , 6.0 ])
context.move_to( 40 , 30 )
context.line_to( 250 , 30 )
context.stroke()
print ( "File Saved" )
|
context.set_dash ( [ 14.0, 6.0 ] )
In this pattern, we have always 14 points drawn and 6 not drawn.
Output :
Example 3 :
Python3
import cairo
with cairo.SVGSurface( "geekline.svg" , 700 , 700 ) as surface:
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 4 )
context.set_dash([ 1.0 ])
context.move_to( 40 , 30 )
context.line_to( 250 , 30 )
context.stroke()
print ( "File Saved" )
|
context.set_dash ( [ 1.0 ] )
we create a pen dash of a symmetric pattern of alternating single on and off point.
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...