Canvas

namespace ezgl

A library for creating a graphical user interface.

class canvas
#include <canvas.hpp>

Responsible for creating, destroying, and maintaining the rendering context of a QWidget.

The backing widget is selected at runtime via render_backend (one of immediate / deferred / rhi); the canvas connects to the widget’s resize and paint events to remain responsive.

Each canvas is double-buffered. A draw callback (see: ezgl::draw_canvas_fn) is invoked each time the canvas needs to be redrawn. This may be caused by the user (e.g., resizing the screen), but can also be forced by the programmer.

Public Functions

~canvas() = default

Destructor.

inline char const *id() const

Get the name (identifier) of the canvas.

int width() const

Get the width of the canvas in pixels.

int height() const

Get the height of the canvas in pixels.

void redraw()

Force the canvas to redraw itself.

This will invoke the ezgl::draw_canvas_fn callback and queue a redraw of the QWidget.

void redraw_at_view_change(view_change_reason reason)

Redraw using only a camera (MVP) update when cached geometry is reusable. Meaningful on the RHI path only.

On the RHI path, the RHI backend checks whether the existing scene buffer can be reused. If reuse is allowed, the backend updates the camera transform/overlay without re-running the draw callback. Otherwise, or on non-RHI paths, this falls back to a full redraw.

Parameters:

reason – Reason that triggers this view change (e.g. pan, zoom_in).

inline camera const &get_camera() const

Get an immutable reference to this canvas’ camera.

inline camera &get_camera()

Get a mutable reference to this canvas’ camera.

inline void set_renderer_type(renderer_type t)

Set the rendering backend type. Must be called before application::build_ui() or the first application::run().

inline void set_decide_reuse_geometry_callback(decide_reuse_geometry_fn cbk)

Set the callback which decides before a camera-only redraw if the cached geometry can be reused. Only meaningful on the RHI path. If this setter is not called, redraw_at_view_change() will always perform a camera-only redraw.

inline renderer_type get_renderer_type() const

Get the currently selected rendering backend type.

See also

ezgl::renderer_type for the available backend types.

Returns:

The active renderer_type.

inline void set_frame_timing_callback(std::function<void(double)> fn)

Register a callback invoked after each canvas::redraw() completes. Receives the total CPU time of the redraw in milliseconds — for RHI this includes both command recording and flush(); for QPainter backends it covers the full draw callback execution.

renderer *create_animation_renderer()

Create an animation renderer that can be used to draw on top of the current canvas

bool print_pdf(const char *file_name, int width = 0, int height = 0)

print_pdf, print_svg, and print_png generate a PDF, SVG, or PNG output file showing all the graphical content of the current canvas.

Parameters:

file_name – name of the output file

Returns:

returns true if the function has successfully generated the output file, otherwise failed due to errors such as out of memory occurs.

bool print_svg(const char *file_name, int width = 0, int height = 0)
bool print_png(const char *file_name, int width = 0, int height = 0)
void draw_offscreen(int width, int height)

Run the draw callback on an offscreen surface of the given size without saving any file. Use this to measure pure render time, separate from PNG/PDF encoding overhead.

Protected Functions

canvas(std::string canvas_id, draw_canvas_fn draw_callback, rectangle coordinate_system, color background_color)

Create a canvas that can be drawn to.

void initialize(QWidget *drawing_area)

Lazy initialization of the canvas class.

This function is required because the GUI is not built until ezgl::application::run() loads the .ui file (UI loading is deferred from the constructor to run() so Qt resources are registered). Canvas construction happens before that via add_canvas(), so the backing widget is bound here at run time, before exec() starts the event loop.

Private Functions

QImage render_to_image(int surface_width, int surface_height)
void suspend_redraw()

Suspend redrawing on the active backend; no-op if no backend is active.

See also

render_backend::suspend_redraw

void resume_redraw()

Resume redrawing on the active backend, flushing any pending redraw; no-op if no backend is active.

See also

render_backend::suspend_redraw

Private Members

std::string m_canvas_id
draw_canvas_fn m_draw_callback
decide_reuse_geometry_fn m_decide_reuse_geometry_callback = nullptr
camera m_camera
color m_background_color
QWidget *m_drawing_area = nullptr
renderer_type m_renderer_type = renderer_type::rhi
std::function<void(double)> m_frame_timing_fn = nullptr
std::unique_ptr<render_backend> m_backend

Friends

friend class application