画布(Canvas)

画布是一个使用nanovg绘制几何图形的控件。

警告

打开帧缓冲区需要足够的内存。

用法

创建控件

使用 gui_canvas_create() 创建一个画布。

添加回调函数

使用 gui_canvas_set_canvas_cb() 设置绘制具体图形的回调函数。

示例

圆角矩形

以下是一个绘制三个不同颜色的圆角矩形的简单例子。

  • 其中,三种颜色参考 Colors’ RGB Data

  • firebrick

  • olive drab

  • dodger blue 设置透明度为100。

#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);
}
https://foruda.gitee.com/images/1698649650262539854/8b1a974f_10088396.png

圆弧动画

以下是一个绘制圆弧动画的例子,其中,屏幕刷新的每一帧都会触发 arc_cb 函数。

#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

请参考以下地址:

Nanovg Introduction

Nanovg Github

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.

参数:
  • 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.

返回:

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.

参数:
  • 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.

参数:
  • 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.

返回:

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.

参数:
  • 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.

参数:
  • 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.

返回:

Return a pointer to the initialized NanoVG context.

void gui_canvas_output_buffer_blank_close(NVGcontext *vg)

Close the canvas and free resources.

参数:

vg – Pointer to the NanoVG context to be closed.

struct gui_canvas_t

canvas structure

Public Members

gui_obj_t base
NVGcontext *vg
void (*nanovg_canvas_cb)(struct gui_canvas *this_widget)
uint8_t checksum
uint8_t render