Skip to main content

Canvas

You can either draw directly in the window (the default) or onto a custom Canvas.

Summary

Classes

Name
classp6::Canvas
A canvas is an image that can be drawn onto.

Functions

Name
voidsave_image(const Canvas & canvas, std::filesystem::path path)
Saves the content of the canvas as an image file.
voidrender_to_canvas(Canvas & canvas)
Sets a canvas to be the one that all the drawing commands will draw on, until you call render_to_main_canvas.
voidrender_to_main_canvas()
Reset the Context to render to the main canvas. The main canvas is the one that will be displayed in the window.
Canvas &main_canvas()
const Canvas &main_canvas() const
Canvas &current_canvas()
const Canvas &current_canvas() const
floataspect_ratio() const
Returns the aspect ratio (a.k.a.
floatinverse_aspect_ratio() const
Returns the inverse aspect ratio (a.k.a.
ImageSizemain_canvas_size() const
Returns the size of the main canvas (width and height).
intmain_canvas_width() const
Returns the width of the main canvas.
intmain_canvas_height() const
Returns the height of the main canvas.
ImageSizecurrent_canvas_size() const
Returns the size of the current canvas (width and height).
intcurrent_canvas_width() const
Returns the width of the current canvas.
intcurrent_canvas_height() const
Returns the height of the current canvas.
voidmain_canvas_mode(CanvasSizeMode mode)
Sets how the size of the main canvas will be computed.
floatcanvas_ratio(const Canvas & canvas) const
Returns the ratio that you need to multiply with in order to match the position and size of the canvas when it is fitted in the window.
voidsave_image(std::filesystem::path path) const
Saves the content of the window's main canvas as an image file.
Colorread_pixel(glm::vec2 position) const
Returns the color of the pixel at the given position in the main canvas.

Functions

save_image()

void save_image(const Canvas & canvas, std::filesystem::path path);

Saves the content of the canvas as an image file.

Supported file types are .png and .jpeg/.jpg Simply use the corresponding extension to save in the desired format. If the path already exists, a number will be appended to the name and the previous file won't be overwritten. If the path is relative, it will be relative to the directory containing your executable. If some directories in the path don't exist yet, they will be created automatically.

render_to_canvas()

void render_to_canvas(Canvas & canvas);

Sets a canvas to be the one that all the drawing commands will draw on, until you call render_to_main_canvas.

render_to_main_canvas()

void render_to_main_canvas();

Reset the Context to render to the main canvas. The main canvas is the one that will be displayed in the window.

main_canvas()

Canvas & main_canvas();

main_canvas()

const Canvas & main_canvas() const;

current_canvas()

Canvas & current_canvas();

current_canvas()

const Canvas & current_canvas() const;

aspect_ratio()

float aspect_ratio() const;

Returns the aspect ratio (a.k.a.

width / height) of the current canvas. This canvas is the window's main canvas by default, unless you called render_to_canvas() in which case it will be the given canvas. When you call render_to_main_canvas() the current canvas goes back to beeing the window's main canvas.

inverse_aspect_ratio()

float inverse_aspect_ratio() const;

Returns the inverse aspect ratio (a.k.a.

height / width) of the current canvas. This canvas is the window's main canvas by default, unless you called render_to_canvas() in which case it will be the given canvas. When you call render_to_main_canvas() the current canvas goes back to beeing the window's main canvas.

main_canvas_size()

ImageSize main_canvas_size() const;

Returns the size of the main canvas (width and height).

main_canvas_width()

int main_canvas_width() const;

Returns the width of the main canvas.

main_canvas_height()

int main_canvas_height() const;

Returns the height of the main canvas.

current_canvas_size()

ImageSize current_canvas_size() const;

Returns the size of the current canvas (width and height).

current_canvas_width()

int current_canvas_width() const;

Returns the width of the current canvas.

current_canvas_height()

int current_canvas_height() const;

Returns the height of the current canvas.

main_canvas_mode()

void main_canvas_mode(CanvasSizeMode mode);

Sets how the size of the main canvas will be computed.

The default mode is CanvasSizeMode_SameAsWindow.

canvas_ratio()

float canvas_ratio(const Canvas & canvas) const;

Returns the ratio that you need to multiply with in order to match the position and size of the canvas when it is fitted in the window.

save_image()

void save_image(std::filesystem::path path) const;

Saves the content of the window's main canvas as an image file.

Supported file types are .png and .jpeg/.jpg Simply use the corresponding extension to save in the desired format. If the path already exists, a number will be appended to the name and the previous file won't be overwritten. If the path is relative, it will be relative to the directory containing your executable. If some directories in the path don't exist yet, they will be created automatically.

read_pixel()

Color read_pixel(glm::vec2 position) const;

Returns the color of the pixel at the given position in the main canvas.

The coordinates are expressed in the usual p6 coordinate system.


Updated on 2024 October 24