Skip to main content

set-ctx

set-ctx() 

An advanced element that allows you to modify the current canvas context.

A context object holds the canvas' state, such as the element dictionary, the current transformation matrix, group and canvas unit length. The following fields are considered stable:

length:

Length of one canvas unit as typst length

transform:

Current 4x4 transformation matrix

debug:

True if the canvas' debug flag is set

Note: The transformation matrix (transform) is rounded after calling the callback function and therefore might be not exactly the matrix specified. This is due to rounding errors and should not cause any problems.

// Setting a custom transformation matrix
set-ctx(ctx => {
let mat = ((1, 0, .5, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1))
ctx.transform = mat
return ctx
})
circle((z: 0), fill: red)
circle((z: 1), fill: blue)
circle((z: 2), fill: green)

callback:

A function that accepts the context dictionary and only returns a new one.