Skip to content
Related Articles

Related Articles

PyCairo – Transform a distance vector from device space to user space

Improve Article
Save Article
  • Last Updated : 23 Jan, 2022
Improve Article
Save Article

In this article, we will learn how we can transform a distance-vector from device space to user space using PyCairo in python. This function is similar to Context.device_to_user() except that the translation components of the inverse CTM will be ignored when transforming (dx,dy).

Pycairo is a Python module providing bindings for the cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read only) is with a modern web browser. Nearly all web browsers support some sort of rendering for the SVG format.  

SVG stands for Scalable Vector Graphics. It basically defines vector-based graphics in XML format. SVG graphics do NOT lose any quality if they are zoomed or resized. Every element and every attribute in SVG files can be animated.

In order to do this, we will use the device_to_user_distance() method with the Context object.

Syntax : context.device_to_user_distance()

Argument : It takes dx, dy

Return : It returns dx, dy both float

Example :


# 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
    # creating a arc with using close path method
    context.arc(300, 60, 40, 0, 1*22/7)
    # making 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
# printing message when file is saved

Output :

(1.0, 10.0)
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!