ctx module

class ctx.Context(*args, **kwargs)

Context is the rendering/rasterization API used by st3m.

It’s a WebCanvas-style vector API, with an implicit pen which can be moved and can draw lines, arcs, text, etc.

In st3m, the Context object is backed by a drawlist generator. That is, any operation performed on the ctx object will cause an entry to be added to an in-memory draw list. Then, when the rasterizer is ready, it will rasterize said drawlist to pixels in a separate thread.

A Context object is passed to all draw() calls to Responder. This object should only be used within the lifecycle of the draw method and must not be persisted.

For more information, see: https://ctx.graphics/

apply_transform(a: float, b: float, c: float, d: float, e: float, f: float, g: float, h: float, i: float) Context

Adds a 3x3 matrix on top of the existing user to device space transform.

TODO(q3k): we probably shouldn’t expose this

arc(x: float, y: float, radius: float, angle1: float, angle2: float, direction: float) Context

TOD(q3k): document

arc_to(x1: float, y1: float, x2: float, y2: float, radius: float) Context

TOD(q3k): document

begin_path() Context

Clears the current path if any.

clip() Context

Use the current path as a clipping mask, subsequent draw calls are limited by the path. The only way to increase the visible area is to first call save and then later restore to undo the clip.

close_path() Context

Close the current open path with a curve back to where the current sequence of path segments started.

curve_to(cx0: float, cy0: float, cx1: float, cy1: float, x: float, y: float) Context

Add a cubic bezier segment to current path, with control handles at cx0, cy0 and cx1,cy1, the virtual pens new position is x, y.

end_frame() Context

We’re done rendering a frame, this does nothing on a context created for a framebuffer, where drawing commands are immediate.

TODO(q3k): we probably shouldn’t expose this

end_group() Context

End a compositing group, the global alpha, compositing mode and blend mode set before this call is used to apply the group.

fill() Context

TOD(q3k): document

get_font_name(ix: int) str

Returns font name from internal font index. See ctx_config.h for defined fonts.

TODO(q3k): expose these font indices into mpy

gray(a: float) Context

Set current draw color to a floating point grayscale value from 0 to 1.

image(path: str, x: float, y: float, w: float, h: float) Context

Draw the image at path a in a rectangle with upper left coordinates at x,y which is w wide and h high. If w or h is -1 the other is set maintaining aspect ratio, if both are -1 the pixel dimensions of the image is used.

line_to(x: float, y: float) Context

Adds a line segment to the path, from the position of the virtual pen to the given coordinates, the coordinates are the new position of the virtual pen.

linear_gradient(x0: float, y0: float, x1: float, y1: float) Context

Change the source to a linear gradient from x0,y0 to x1,y1, by default an empty gradient from black to white exists, add stops with gradient_add_stop to specify a custom gradient.

TODO(q3k): check gradient_add_stop

TOD(q3k): document

move_to(x: float, y: float) Context

Moves the virtual pen to the given coordinates without drawing anything.

paint() Context

TOD(q3k): document

preserve() Context

TOD(q3k): document

quad_to(cx: float, cy: float, x: float, y: float) Context

Add a quadratic bezier segment to current path with control point at cx,cy ending at x,y.

radial_gradient(x0: float, y0: float, r0: float, x1: float, y1: float, r1: float) Context

Change the source to a radial gradient from a circle x0,y0 with radius0 to an outer circle x1,y1 with radidus r1.

NOTE: currently only the second circle’s origin is used, but both radiuses are in use.

rectangle(x: float, y: float, w: float, h: float) Context

Trace the outline of a rectangle with upper left coordinates at x,y which is w wide and h high.

rel_arc_to(x1: float, y1: float, x2: float, y2: float, radius: float) Context

TOD(q3k): document

rel_curve_to(cx0: float, cy0: float, cx1: float, cy1: float, x: float, y: float) Context

TOD(q3k): document

rel_line_to(x: float, y: float) Context

TOD(q3k): document

rel_move_to(x: float, y: float) Context

TOD(q3k): document

rel_quad_to(cx: float, cy: float, x: float, y: float) Context

TOD(q3k): document

restore() Context

Restores the state previously saved with save, calls to save/restore should be balanced.

rgb(r: float, g: float, b: float) Context

Set current draw color to an RGB color defined by component values from 0 to 1.

rgba(r: float, g: float, b: float, a: float) Context

Set current draw color to an RGBA color defined by component values from 0 to 1.

rotate(x: float) Context

Add rotation to the user to device space transform.

round_rectangle(x: float, y: float, w: float, h: float, r: float) Context

Trace the outline of a rectangle with upper left coordinates at x,y which is w wide and h high. With quarter circles with a radius of r for corners.

save() Context

Stores the transform, clipping state, fill and stroke sources, font size, stroking and dashing options.

scale(x: float, y: float) Context

Scales the user to device transform.

scope() Context

Draw an audio ‘oscilloscope’-style visualizer at -120,-120,120,120 bounding box.

Needs to be stroked/filled afterwards.

start_frame() Context

Prepare for rendering a new frame, clears internal drawlist and initializes the state.

TODO(q3k): we probably shouldn’t expose this

start_group() Context

Start a compositing group.

stroke() Context

TOD(q3k): document

text(text: str) Context

TOD(q3k): document

text_width(text: str) float

Calculates width of rendered text, without rendering it.

translate(x: float, y: float) Context

Adds translation to the user to device transform.

ALPHABETIC: str = 'alphabetic'
BOTTOM: str = 'bottom'
CENTER: str = 'center'
END: str = 'end'
HANGING: str = 'hanging'
IDEOGRAPHIC: str = 'ideographic'
JUSTIFY: str = 'justify'
LEFT: str = 'left'
MIDDLE: str = 'middle'
RIGHT: str = 'right'
START: str = 'start'
TOP: str = 'top'
font: str = ''
font_size: float = 10
global_alpha: float = 1.0
image_smoothing: bool = True
line_width: float = 1.0
text_align: str = 'start'
text_baseline: str = 'alphabetic'