1 #include "../../lv_examples.h"
2 #if LV_USE_LABEL && LV_USE_CANVAS && LV_BUILD_EXAMPLES && LV_DRAW_COMPLEX
3 
4 #define MASK_WIDTH 100
5 #define MASK_HEIGHT 45
6 
add_mask_event_cb(lv_event_t * e)7 static void add_mask_event_cb(lv_event_t * e)
8 {
9     static lv_draw_mask_map_param_t m;
10     static int16_t mask_id;
11 
12     lv_event_code_t code = lv_event_get_code(e);
13     lv_obj_t * obj = lv_event_get_target(e);
14     lv_opa_t * mask_map = lv_event_get_user_data(e);
15     if(code == LV_EVENT_COVER_CHECK) {
16         lv_event_set_cover_res(e, LV_COVER_RES_MASKED);
17     }
18     else if(code == LV_EVENT_DRAW_MAIN_BEGIN) {
19         lv_draw_mask_map_init(&m, &obj->coords, mask_map);
20         mask_id = lv_draw_mask_add(&m, NULL);
21 
22     }
23     else if(code == LV_EVENT_DRAW_MAIN_END) {
24         lv_draw_mask_free_param(&m);
25         lv_draw_mask_remove_id(mask_id);
26     }
27 }
28 
29 /**
30  * Draw label with gradient color
31  */
lv_example_label_4(void)32 void lv_example_label_4(void)
33 {
34     /* Create the mask of a text by drawing it to a canvas*/
35     static lv_opa_t mask_map[MASK_WIDTH * MASK_HEIGHT];
36 
37     /*Create a "8 bit alpha" canvas and clear it*/
38     lv_obj_t * canvas = lv_canvas_create(lv_scr_act());
39     lv_canvas_set_buffer(canvas, mask_map, MASK_WIDTH, MASK_HEIGHT, LV_IMG_CF_ALPHA_8BIT);
40     lv_canvas_fill_bg(canvas, lv_color_black(), LV_OPA_TRANSP);
41 
42     /*Draw a label to the canvas. The result "image" will be used as mask*/
43     lv_draw_label_dsc_t label_dsc;
44     lv_draw_label_dsc_init(&label_dsc);
45     label_dsc.color = lv_color_white();
46     label_dsc.align = LV_TEXT_ALIGN_CENTER;
47     lv_canvas_draw_text(canvas, 5, 5, MASK_WIDTH, &label_dsc, "Text with gradient");
48 
49     /*The mask is reads the canvas is not required anymore*/
50     lv_obj_del(canvas);
51 
52     /* Create an object from where the text will be masked out.
53      * Now it's a rectangle with a gradient but it could be an image too*/
54     lv_obj_t * grad = lv_obj_create(lv_scr_act());
55     lv_obj_set_size(grad, MASK_WIDTH, MASK_HEIGHT);
56     lv_obj_center(grad);
57     lv_obj_set_style_bg_color(grad, lv_color_hex(0xff0000), 0);
58     lv_obj_set_style_bg_grad_color(grad, lv_color_hex(0x0000ff), 0);
59     lv_obj_set_style_bg_grad_dir(grad, LV_GRAD_DIR_HOR, 0);
60     lv_obj_add_event_cb(grad, add_mask_event_cb, LV_EVENT_ALL, mask_map);
61 }
62 
63 #endif
64