Clear canvas content

Hello, everyone, I was drawing some pixels and according to the docs, the methods I see are not necesarly clearing the canvas, I was wondering if there is a best practice to clear the canvas content, right now I am creating the canvas like this:

canvas = lv_canvas_create(lv_scr_act(), NULL);
    lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_INDEXED_1BIT);
    lv_canvas_set_palette(canvas, 0, LV_COLOR_TRANSP);
    lv_canvas_set_palette(canvas, 1, LV_COLOR_MAKE(155, 205, 0x00));

And I draw some rectangles in pixel format with this little method

void mydrawRec(uint32_t x,uint32_t y,uint32_t w,uint32_t h){
    lv_color_t c0;  
    c0.full = 1;
    uint32_t mx;
    uint32_t my;
    for( my = y; my < y+h; my++) {
        for( mx = x; mx < x+w; mx++) {
            lv_canvas_set_px(canvas, mx, my, c0);
        }
    }
}

and when I need to erase the content, I do this:

void clearCanvas(){
    lv_color_t c1;  
    c1.full = 0;
    uint32_t x;
    uint32_t y;
    for( y = 0; y < CANVAS_HEIGHT; y++) {
        for( x = 0; x < CANVAS_WIDTH; x++) {
            lv_canvas_set_px(canvas, x, y, c1);
        }
    }
}

Fortunately it is erasing the canvas, but my guess is that, going pixel by pixel is not the best approach

You can clear the canvas by using lv_canvas_fill_bg with your desired background color.

awesome!, yes it did work :slight_smile: feels faster, btw, by adding a “set_palette” to the canvas I tell lvgl to only use those colors right?

Yes. Since you have chosen LV_IMG_CF_INDEXED_1BIT, there can only be 2 colors on the canvas at a time. set_palette selects what those 2 colors are.

ok, I asked you because, I wanted to create a label inside the canvas but for some reason the text is not showing:


canvas = lv_canvas_create(lv_scr_act(), NULL);
    lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_INDEXED_2BIT);
    lv_canvas_set_palette(canvas, 0, LV_COLOR_TRANSP);
    lv_canvas_set_palette(canvas, 1, LV_COLOR_MAKE(155, 205, 0x00));
    lv_canvas_set_palette(canvas, 2, LV_COLOR_YELLOW);

    lv_draw_label_dsc_t label_dsc;
    lv_draw_label_dsc_init(&label_dsc);
    label_dsc.color = LV_COLOR_MAKE(155, 205, 0x00);
    lv_canvas_draw_text(canvas, 40, 20, 100, &label_dsc, "Some text on text canvas", LV_LABEL_ALIGN_LEFT);