Skip to content

Editor code replace

examples/editor_code_replace.py
from keyed import UL, Code, Scene, stagger, tokenize

from keyed_extras import Editor

s = Scene(width=800, height=800, num_frames=72)

content = "import this\n" * 20
styled_tokens1 = tokenize(content)
code1 = Code(s, styled_tokens1, font_size=48)

styled_tokens2 = tokenize(r"import that")
code2 = Code(s, styled_tokens2, font_size=48, alpha=0)

# Line "import this" and "import that" up, based on their first characters.
code2.align_to(code1.lines[-1], from_=code2.chars[0].geom, direction=UL)

editor = Editor(s, title="hello_world.py", width=800).scale(0.4).center()
editor.add(code1, code2)

s.add(editor)

code1.chars[-1:-5:-1].write_on(
    "alpha",
    animator=stagger(start_value=1, end_value=0),
    delay=4,
    duration=1,
    start=24,
)

code2.chars[-5:].write_on(
    "alpha",
    animator=stagger(start_value=0, end_value=1),
    delay=4,
    duration=1,
    start=36,
)

editor.scroll_to(1, 0, 24)