Canvas
The canvas widget is the basic widget used to drawing graphics in nanovg.
Warning
Sufficient memory is needed to open a framebuffer.
Usage
Creat Canvas
gui_canvas_create()
creates a canvas.
Add Callback Function
gui_canvas_set_canvas_cb()
sets the callback function for drawing specific shapes.
Example
Rounded Rectangle
A simple example of drawing three rounded rectangles of different colors.
Three color refer to Colors’ RGB Data.
firebrick
olive drab
dodger blue
with 100 value opacity
#include "gui_canvas.h"
static void canvas_cb(gui_canvas_t *canvas)
{
nvgRoundedRect(canvas->vg, 10, 10, 348, 200, 30);
nvgFillColor(canvas->vg, nvgRGB(178,34,34));
nvgFill(canvas->vg);
nvgBeginPath(canvas->vg);
nvgRoundedRect(canvas->vg, 10, 220, 348, 200, 30);
nvgFillColor(canvas->vg, nvgRGB(107,142,35));
nvgFill(canvas->vg);
nvgBeginPath(canvas->vg);
nvgRoundedRect(canvas->vg, 110, 125, 148, 200, 30);
nvgFillColor(canvas->vg, nvgRGBA(30,144,255, 100));
nvgFill(canvas->vg);
}
static void app_ui_design(gui_app_t *app)
{
gui_canvas_t *canvas = gui_canvas_create(&(app->screen), "canvas", 0, 0, 0, 368, 448);
gui_canvas_set_canvas_cb(canvas, canvas_cb);
}

Arc Animation
An example of drawing an arc animation. arc_cb
will be triggered every frame.
#include "math.h"
#include "gui_canvas.h"
static void arc_cb(gui_canvas_t *canvas)
{
static float progress;
progress +=0.01;
nvgArc(canvas->vg, 368/2, 448/2, 150, 0, 3.14*(sinf(progress)+1), NVG_CCW);
nvgStrokeWidth(canvas->vg, 20);
nvgStrokeColor(canvas->vg, nvgRGB(178,34,34));
nvgStroke(canvas->vg);
}
static void app_ui_design(gui_app_t *app)
{
gui_canvas_t *canvas = gui_canvas_create(&(app->screen), "canvas", 0, 0, 0, 368, 448);
gui_canvas_set_canvas_cb(canvas, arc_cb);
}

API
Nanovg API
Please refer to
RealGUI API
Defines
-
GUI_CANVAS_OUTPUT_PNG 1
-
GUI_CANVAS_OUTPUT_JPG 2
-
GUI_CANVAS_OUTPUT_RGBA 3
-
GUI_CANVAS_OUTPUT_RGB565 4
Typedefs
-
typedef void (*gui_canvas_render_function)(NVGcontext *vg)
Functions
-
gui_canvas_t *gui_canvas_create(void *parent, const char *name, void *addr, int16_t x, int16_t y, int16_t w, int16_t h)
-
Create a canvas widget used to draw graphics in NanoVG.
- Parameters:
parent – The father widget nested in.
name – This widget’s canvas name.
addr – Address.
x – The X-axis coordinate relative to parent widget.
y – The Y-axis coordinate relative to parent widget.
w – Width.
h – Height.
- Returns:
-
Return the canvas widget pointer.
-
void gui_canvas_set_canvas_cb(gui_canvas_t *this_widget, void (*cb)(gui_canvas_t *this_widget))
-
Set the callback function for drawing specific shapes.
- Parameters:
this_widget – This widget pointer.
cb – The callback function for drawing specific shapes.
-
const uint8_t *gui_canvas_output(int output_format, bool compression, int image_width, int image_height, gui_canvas_render_function renderer)
-
Render a canvas and output it in the specified format.
- Parameters:
output_format – The format of the output image.
compression – Whether to apply compression to the output.
image_width – The width of the output image.
image_height – The height of the output image.
renderer – A function pointer to the rendering function.
- Returns:
-
Return a pointer to the buffer containing the output image data.
-
void gui_canvas_output_buffer(int format, bool compression, int image_width, int image_height, gui_canvas_render_function renderer, uint8_t *target_buffer)
-
Render a canvas and output the result to a target buffer in the specified format.
- Parameters:
format – The format of the output image.
compression – Whether to apply compression to the output.
image_width – The width of the output image.
image_height – The height of the output image.
renderer – A function pointer to the rendering function.
target_buffer – A pointer to the buffer where the rendered image data will be written.
-
NVGcontext *gui_canvas_output_buffer_blank(int format, bool compression, int image_width, int image_height, uint8_t *target_buffer)
-
Initialize a blank output buffer for the canvas.
- Parameters:
format – The format of the output buffer.
compression – Boolean flag indicating whether compression should be used.
image_width – The width of the image.
image_height – The height of the image.
target_buffer – Pointer to the target buffer where the output will be stored.
- Returns:
-
Return a pointer to the initialized NanoVG context.
-
void gui_canvas_output_buffer_blank_close(NVGcontext *vg)
-
Close the canvas and free resources.
- Parameters:
-
vg – Pointer to the NanoVG context to be closed.
-
struct gui_canvas_t
-
canvas structure