MantisIDE

Lesson 8: Animations · Round 1.28" touch watch

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

Lesson 8 - Animations Goal: animate any property with lv.anim_t. You give a start value, an end value, a duration, an easing path and a function that applies the value. anim(obj, start, end, ms, setter, path, repeat, playback, delay) wraps it.

You’ll learn: lv.anim_t, easing paths, repeat, playback

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

# Lesson 8 - Animations
# Goal: animate any property with lv.anim_t. You give a start value, an end value,
# a duration, an easing path and a function that applies the value.
# anim(obj, start, end, ms, setter, path, repeat, playback, delay) wraps it.
import lvgl as lv
import display

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

BG = 0x05110A
LINE = 0x24452F
TEXT = 0xE3EFE6
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
keep = []                             # styles, buffers and callbacks LVGL uses must stay referenced

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb


def dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
    """A filled circle: a plain lv.obj with every theme style removed."""
    o = lv.obj(parent)
    o.remove_style_all()
    size = max(1, int(size))
    o.set_size(size, size)
    o.set_style_radius(lv.RADIUS_CIRCLE, 0)
    o.set_style_bg_color(lv.color_hex(color), 0)
    o.set_style_bg_opa(lv.OPA.COVER, 0)
    o.remove_flag(lv.obj.FLAG.CLICKABLE)
    o.align(align, int(x), int(y))
    return o


def ring(parent, size, width, color, value=100, bg_color=LINE):
    """An lv.arc used as a progress ring (no knob, not touchable)."""
    a = lv.arc(parent)
    a.set_size(size, size)
    a.center()
    a.set_rotation(270)                  # start at 12 o'clock
    a.set_bg_angles(0, 360)
    a.set_range(0, 100)
    a.set_value(value)
    a.remove_style(None, lv.PART.KNOB)
    a.remove_flag(lv.obj.FLAG.CLICKABLE)
    a.set_style_arc_width(width, lv.PART.MAIN)
    a.set_style_arc_width(width, lv.PART.INDICATOR)
    a.set_style_arc_color(lv.color_hex(bg_color), lv.PART.MAIN)
    a.set_style_arc_color(lv.color_hex(color), lv.PART.INDICATOR)
    return a


def anim(obj, start, end, ms, setter, path=None, repeat=1, playback=0, delay=0, done=None):
    """Animate start -> end over ms, calling setter(obj, value). repeat=-1 loops forever."""
    a = lv.anim_t()
    a.init()
    a.set_var(obj)
    a.set_values(start, end)
    a.set_duration(ms)
    a.set_delay(delay)
    a.set_custom_exec_cb(lambda _a, v: setter(obj, v))
    a.set_path_cb(path or lv.anim_t.path_ease_in_out)
    if repeat < 0:
        a.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
    elif repeat > 1:
        a.set_repeat_count(repeat)
    if playback:
        a.set_reverse_duration(playback)  # play back to start
    if done:
        a.set_completed_cb(lambda _a: done())
    a.start()
    keep.append(a)
    return a


A = lv.anim_t
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)

# 1. Move: a ball bouncing on an invisible floor (path_bounce)
ball = dot(scr, 26, AMBER, 0, -70)
anim(ball, -70, 30, 900, lambda o, v: o.set_y(v), path=A.path_bounce, repeat=-1, playback=0)

# 2. Fade: a label breathing in and out (opacity 0..255)
fade = label(scr, "fade", TEXT, 16, x=-60, y=60)
anim(fade, 40, 255, 1000, lambda o, v: o.set_style_opa(v, 0), repeat=-1, playback=1000)

# 3. Scale: pulse with overshoot
pulse = dot(scr, 30, ACCENT, 60, 60)
anim(pulse, 180, 300, 600, lambda o, v: o.set_style_transform_scale(v, 0),
        path=A.path_overshoot, repeat=-1, playback=600)

# 4. Arc progress: a ring that fills, then empties (ease in-out)
progress_ring = ring(scr, 220, 8, BLUE, 0)
anim(progress_ring, 0, 100, 2000, lambda o, v: o.set_value(v), repeat=-1, playback=2000)

# 5. Colour: interpolate the background between two colours
c1, c2 = lv.color_hex(0x05110A), lv.color_hex(0x10203A)
anim(scr, 0, 255, 3000, lambda o, v: o.set_style_bg_color(c2.mix(c1, v), 0),
        repeat=-1, playback=3000)

print("Paths to try: path_linear, path_ease_in, path_ease_out, path_bounce, path_overshoot, path_step")
Loading post…

Dialog