Context API
A context provides an API to provide a rendered image, and implements a
mechanism to present that image for display. The concept of a context is
heavily inspired by the <canvas> and its contexts in the browser.
Available context classes
The
BaseContextexposes the common API.The
BitmapContextexposes an API that takes image bitmaps in RAM.The
WgpuContextexposes an API that provides image textures on the GPU to render to.
Getting a context
Context objects must be created using context = canvas.get_context(..),
or the dedicated canvas.get_bitmap_context() and
canvas.get_wgpu_context().
Using a context
All contexts provide detailed size information (which is kept up-to-date by the canvas). A rendering system should generally be capable to perform the rendering with just the context object; without a reference to the canvas. With this, we try to promote a clear separation of concerns, where one system listens to events from the canvas to update a certain state, and the renderer uses this state and the context to render the image.
Advanced: creating a custom context API
It’s possible for users to create their own context sub-classes. This can be
a good solution, e.g. when your system needs to handle the presentation by
itself. In general it’s better, when possible, to create an object that wraps a
built-in context object: my_ob = MyClass(canvas.get_context(..)). That
way your code will not break if the internal interface between the context
and the canvas is changed.
- class rendercanvas.contexts.BaseContext(present_info: dict)
The base class for context objects in
rendercanvas.- property logical_size: tuple[float, float]
The logical size (width, height) of the render target in float pixels.
The logical size can be smaller than the physical size, e.g. on HiDPI monitors or when the user’s system has the display-scale set to e.g. 125%. The logical size can in theory also be larger than the physical size, but this is much less common.
- class rendercanvas.contexts.BitmapContext(present_info: dict)
A context that exposes an API that takes a (grayscale or rgba) images bitmap.
This is loosely inspired by JS’
ImageBitmapRenderingContext. Rendering bitmaps is a simple way to userendercanvas, but usually not as performant as a wgpu context. Usecanvas.get_bitmap_context()to create aBitmapContext.- set_bitmap(bitmap)
Set the rendered bitmap image.
Call this in the draw event. The bitmap must be an object that can be converted to a numpy array. It must represent a 2D image in either grayscale or rgba format, with uint8 values
- class rendercanvas.contexts.WgpuContext(present_info: dict)
A context that exposes an API that provides a GPU texture to render to.
This is inspired by JS’
GPUCanvasContext, and the more performant approach for rendering to arendercanvas. Usecanvas.get_wgpu_context()to create aWgpuContext.- configure(*, device: object, format: str, usage: str | int = 'RENDER_ATTACHMENT', view_formats: Sequence[str] = (), alpha_mode: str = 'opaque') None
Configures the presentation context for the associated canvas. Destroys any textures produced with a previous configuration.
- Parameters:
device (WgpuDevice) – The GPU device object to create compatible textures for.
format (wgpu.TextureFormat) – The format that textures returned by
get_current_texture()will have. Must be one of the supported context formats. Can beNoneto use the canvas’ preferred format.usage (wgpu.TextureUsage) – Default “RENDER_ATTACHMENT”.
view_formats (list[wgpu.TextureFormat]) – The formats that views created from textures returned by
get_current_texture()may use.alpha_mode (wgpu.CanvasAlphaMode) – Determines the effect that alpha values will have on the content of textures returned by
get_current_texture()when read, displayed, or used as an image source. Default “opaque”.