📜  PyCairo – 将距离向量从设备空间转换到用户空间

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

PyCairo – 将距离向量从设备空间转换到用户空间

在本文中,我们将学习如何使用Python中的 PyCairo 将距离向量从设备空间转换到用户空间。该函数类似于 Context.device_to_user() ,只是在转换 (dx,dy) 时会忽略逆 CTM 的转换组件。

Pycairo 是一个Python模块,为 cairo 图形库提供绑定。这个库用于在Python中创建 SVG 即矢量文件。打开 SVG 文件进行查看(只读)的最简单快捷的方法是使用现代 Web 浏览器。几乎所有的 Web 浏览器都支持某种 SVG 格式的渲染。

SVG 代表可缩放矢量图形。它基本上定义了 XML 格式的基于矢量的图形。 SVG 图形在缩放或调整大小时不会失去任何质量。 SVG 文件中的每个元素和每个属性都可以设置动画。

为了做到这一点,我们将使用 device_to_user_distance() 方法和 Context 对象。

例子 :

Python3
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek1 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek1.svg", 700, 700) as surface:
  
    # creating a cairo context object
    context = cairo.Context(surface)
  
    # creating a arc without using closing path method
    context.arc(100, 60, 40, 0, 1*22/7)
  
    # stroke the context to remove the moved pen
    context.stroke()
  
    # creating a arc with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
  
    # makeing close path
    context.close_path()
  
    # convert device co-ordinate to user co-ordinate
    a = context.device_to_user_distance(1, 10)
  
    # stroke the context to remove the moved pen
    context.stroke()
  
# printing message when file is saved
print(a)


输出 :

(1.0, 10.0)