Lesson 9: Staggered intro · Round 1.28" touch watch
Lesson 9 - Choreography: staggered intro animation Goal: combine delays + easing so many objects animate as one smooth "intro". The trick: the same animation, started with a growing delay for each item.
You’ll learn: delays, choreography, completed callbacks
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 9 - Choreography: staggered intro animation
# Goal: combine delays + easing so many objects animate as one smooth "intro".
# The trick: the same animation, started with a growing delay for each item.
import math
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
VIOLET = 0xB593FF
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 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
def polar(radius, degrees):
"""(x, y) from the centre for a point on a circle; 0 deg = 12 o'clock, clockwise."""
r = math.radians(degrees - 90)
return int(radius * math.cos(r)), int(radius * math.sin(r))
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)
N = 12
COLORS = [ACCENT, BLUE, VIOLET, AMBER]
for i in range(N):
x, y = polar(92, i * 360 / N)
d = dot(scr, 18, COLORS[i % 4], 0, 0)
d.set_style_opa(0, 0)
delay = i * 70
# fly out from the centre to the ring position
anim(d, 0, x, 700, lambda o, v: o.set_x(v), path=A.path_overshoot, delay=delay)
anim(d, 0, y, 700, lambda o, v: o.set_y(v), path=A.path_overshoot, delay=delay)
anim(d, 0, 255, 400, lambda o, v: o.set_style_opa(v, 0), delay=delay)
title = label(scr, "MANTIS", TEXT, 16, y=-8, zoom=2)
title.set_style_opa(0, 0)
anim(title, 0, 255, 600, lambda o, v: o.set_style_opa(v, 0), delay=N * 70)
anim(title, 30, -8, 600, lambda o, v: o.set_y(v), path=A.path_ease_out, delay=N * 70)
sub = label(scr, "ready", MUTED, 14, y=26)
sub.set_style_opa(0, 0)
anim(sub, 0, 255, 500, lambda o, v: o.set_style_opa(v, 0), delay=N * 70 + 400,
done=lambda: print("intro finished"))
Comments