📜  PyCairo – 绘制不同的笔划线

📅  最后修改于: 2022-05-13 01:55:51.278000             🧑  作者: Mango

PyCairo – 绘制不同的笔划线

每条线都可以用不同的笔划线绘制。笔划线可以定义为线条的样式。虚线模式由set_dash()方法指定。模式由浮点值的破折号列表设置。他们可以在模式中设置破折号的开和关部分。 stroke( )方法使用破折号来创建线条图案。如果短划线数为 0,则禁用短划线并绘制平面简单线。如果短划线的数量为 1,则假定对称模式具有指定为短划线中的单个值的大小的交替打开和关闭部分。

实施步骤:

  1. 导入 Pycairo 模块。
  2. 创建一个 SVG 表面对象并向其添加上下文。
  3. 设置上下文的颜色和线宽
  4. 使用 set_dash( ) 设置线型
  5. 创建一条线。

示例 1:

Python3
# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([4.0, 21.0, 2.0])
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([14.0, 6.0])
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([1.0])
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


输出 :

示例 2:

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([14.0, 6.0])
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")

输出 :

示例 3:

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([1.0])
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")

输出 :