MantisIDE

Canvas drawing · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Widget gallery 9 - Canvas: draw shapes, arcs, lines and text into your own buffer A canvas is a bitmap you paint with LVGL's draw API (great for custom graphics).

You’ll learn: draw_rect, draw_arc, draw_line, draw_label

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Widget gallery. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Widget gallery 9 - Canvas: draw shapes, arcs, lines and text into your own buffer
# A canvas is a bitmap you paint with LVGL's draw API (great for custom graphics).
import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
RED = 0xFF6B6B
VIOLET = 0xB593FF
keep = []                             # styles, buffers and callbacks LVGL uses must stay referenced
scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

W, H = 180, 180
buf = lv.draw_buf_create(W, H, lv.COLOR_FORMAT.RGB565, 0)
cv = lv.canvas(scr)
cv.set_draw_buf(buf)
cv.center()
cv.fill_bg(lv.color_hex(0x0C1A12), lv.OPA.COVER)
keep.append(buf)

layer = lv.layer_t()
cv.init_layer(layer)


def area(x1, y1, x2, y2):
    a = lv.area_t()
    a.x1, a.y1, a.x2, a.y2 = x1, y1, x2, y2
    return a


# Rounded rectangle with gradient + border
rect = lv.draw_rect_dsc_t()
rect.init()
rect.radius = 14
rect.bg_color = lv.color_hex(BLUE)
rect.bg_grad.dir = lv.GRAD_DIR.VER
rect.bg_grad.stops[0].color = lv.color_hex(BLUE)
rect.bg_grad.stops[1].color = lv.color_hex(VIOLET)
rect.bg_grad.stops_count = 2
rect.border_width = 2
rect.border_color = lv.color_hex(0xFFFFFF)
lv.draw_rect(layer, rect, area(20, 20, 90, 70))

# Arc
arc = lv.draw_arc_dsc_t()
arc.init()
arc.color = lv.color_hex(ACCENT)
arc.width = 8
arc.center.x, arc.center.y = 130, 50
arc.radius = 30
arc.start_angle, arc.end_angle = 0, 270
arc.rounded = 1
lv.draw_arc(layer, arc)

# Lines (a little zig-zag)
ln = lv.draw_line_dsc_t()
ln.init()
ln.color = lv.color_hex(AMBER)
ln.width = 4
ln.round_start = ln.round_end = 1
pts = [(20, 140), (50, 100), (80, 140), (110, 100), (140, 140), (165, 105)]
for (x1, y1), (x2, y2) in zip(pts, pts[1:]):
    ln.p1.x, ln.p1.y, ln.p2.x, ln.p2.y = x1, y1, x2, y2
    lv.draw_line(layer, ln)

# Text
txt = lv.draw_label_dsc_t()
txt.init()
txt.color = lv.color_hex(0xFFFFFF)
txt.text = "canvas"
lv.draw_label(layer, txt, area(60, 155, 170, 175))

cv.finish_layer(layer)

# Pixel art: set individual pixels
for i in range(40):
    cv.set_px(100 + i, 85 + (i * i) % 9, lv.color_hex(RED), lv.OPA.COVER)
print("Drawn with lv.draw_rect / draw_arc / draw_line / draw_label onto a canvas layer.")
Loading post…

Dialog