Text

Text, LaTeX math and highlighted code, with addressable parts.

Contents:

  • k.Text: Text with minimal inline markup (**bold**, *italic*, `code`).
  • k.Math: Formula in LaTeX syntax, typeset by the built-in engine (no TeX installation needed).
  • k.Code: Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background).
  • k.underline: Annotation marks on any object or part of a text or formula (eq["dx"]): k.underline(target), k.box(target), k.encircle(target), k.strike(target) (crossed out) and k.cross(target) (an X).
  • k.box: A rounded box around target, pad away from its box.
  • k.cross: An X over target: two lines corner to corner, pad past its box.
  • k.encircle: An ellipse around target (its box, pad larger on each side, times √2 so the corners fit inside).
  • k.strike: A line through the middle of target (struck out), overhang past each side.
  • k.Terminal: A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".

Methods in this area:

  • terminal.type: Named transition: types a command after the prompt, character by character, as k.Code in the terminal's language, with the caret following; cps= characters per second (22 by default) or duration=.
  • terminal.output: Named transition: prints text below the last row, one row per line, all at once or stagger= seconds apart; lines that do not fit scroll the older ones up.
  • code.highlight: Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight.

Back to the reference index.

k.Text (class)

k.Text(
    text: StrVal = "",
    *,
    size: FloatVal | None = None,
    width: float | None = None,
    align: Align = "left",
    **props: Unpack[TextKeywords],
)

Text with minimal inline markup (**bold**, *italic*, `code`). size= sets the size, width= wraps lines, align= aligns. With a lambda the text is reactive: k.Text(lambda: f"{x():.1f} kWh").

Parameters:

Name Type Default Description
text StrVal ""
size FloatVal | None None size= sets the size, width= wraps lines, align= aligns.
width float | None None size= sets the size, width= wraps lines, align= aligns.
align Align "left" size= sets the size, width= wraps lines, align= aligns.
**props Unpack[TextKeywords] variadic Keyword arguments (TextKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, glow: FloatVal, glow_color: ColorVal, color: ColorVal, mono: BoolVal.

Props (animatable with .to(), settable with .set() or in the constructor):

Prop Kind Default Interpolation
fill color theme.fg linear
fill_opacity float 1.0 linear
stroke color theme.fg linear
stroke_width float 0.0 linear
dash floats () step_end
glow float 0.0 linear
glow_color color theme.no_color linear
text str "" step_end
size float from the theme linear
wrap float 0.0 linear
align str "left" step_end
mono bool False step_end

Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible, bleed, emphasis.

Example:

@k.scene
def text(s: k.Scene):
    x = k.signal(0.0)
    title = k.Text("**Solar** energy", size=0.7).place(at="top", margin=0.8)
    value = k.Text(lambda: f"{x():.1f} kWh").place(at="center")
    s.play(k.write(title), k.fade_in(value))
    s.play(x.to(12), duration=2)

See also: k.write, k.signal.

Members:

  • to: Like Node.to; a new text= string morphs the glyphs (equal characters travel).
  • find_all: Every occurrence of needle (in the text without markup), as parts.
  • chars: Characters as parts: txt.chars[3:7] (markup removed).
  • words: Whitespace-separated words as parts: txt.words[1].
  • lines: Laid-out lines as parts: txt.lines[0].

Inherited from k.Group: children, swap, insert, pop, fit. Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.

k.Text.to (method)

text.to(
    *,
    text: str | None = None,
    duration: float | None = None,
    ease: EaseLike | None = None,
    delay: float = 0.0,
    blend: Blend = "replace",
    place: PlaceKeywords | Mapping[str, object] | None = None,
    **props: Unpack[PropChanges],
) -> Animation

Like Node.to; a new text= string morphs the glyphs (equal characters travel).

Parameters:

Name Type Default Description
text str | None None Like Node.to; a new text= string morphs the glyphs (equal characters travel).
duration float | None None
ease EaseLike | None None
delay float 0.0
blend Blend "replace"
place PlaceKeywords | Mapping[str, object] | None None
**props Unpack[PropChanges] variadic

k.Text.find_all (method)

text.find_all(needle: str) -> list[TextPart]

Every occurrence of needle (in the text without markup), as parts. A $math$ span reads as its TeX: txt["f'(x)"].

Parameters:

Name Type Default Description
needle str required Every occurrence of needle (in the text without markup), as parts.

k.Text.chars (property)

text.chars: PartView  # read-only

Characters as parts: txt.chars[3:7] (markup removed).

k.Text.words (property)

text.words: PartView  # read-only

Whitespace-separated words as parts: txt.words[1].

k.Text.lines (property)

text.lines: PartView  # read-only

Laid-out lines as parts: txt.lines[0].

k.Math (class)

k.Math(
    tex: str,
    *,
    size: float = 0.6,
    display: bool = True,
    engine: str = "builtin",
    **props: Unpack[StyleKeywords],
)

Formula in LaTeX syntax, typeset by the built-in engine (no TeX installation needed). \id{name}{...} names a subexpression (eq["name"]); any subexpression is found through the syntax tree (eq["c^2"] ≡ eq["c^{2}"]). k.morph between equations matches names first, then identical TeX subtrees. color= colors the formula and any part (eq["E"].set(color=k.YELLOW), or .to(color=...) to animate). Unsupported command: K0801.

Parameters:

Name Type Default Description
tex str required
size float 0.6
display bool True
engine str "builtin"
**props Unpack[StyleKeywords] variadic Keyword arguments (StyleKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, glow: FloatVal, glow_color: ColorVal, color: ColorVal.

Props (animatable with .to(), settable with .set() or in the constructor):

Prop Kind Default Interpolation
fill color theme.fg linear
fill_opacity float 1.0 linear
stroke color theme.fg linear
stroke_width float 0.0 linear
dash floats () step_end
glow float 0.0 linear
glow_color color theme.no_color linear
tex str "" step_end
size float 0.6 linear
display bool True step_end

Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible, bleed, emphasis.

Example:

@k.scene
def formula(s: k.Scene):
    eq = k.Math(r"\id{lhs}{a^2 + b^2} = c^2", color=k.BLUE).place(at="center")
    eq["="].set(color=k.WHITE)
    s.play(k.write(eq))
    s.play(eq["lhs"].to(color=k.YELLOW))
    s.play(eq["c^2"].to(color=k.GREEN))

See also: k.morph, k.Text, k.write.

Members:

  • find_all: Every occurrence of needle (in the text without markup), as parts.
  • chars: Characters as parts: txt.chars[3:7] (markup removed).
  • words: Whitespace-separated words as parts: txt.words[1].
  • lines: Laid-out lines as parts: txt.lines[0].

Inherited from k.Group: children, to, swap, insert, pop, fit. Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.

k.Math.find_all (method)

math.find_all(needle: str) -> list[TextPart]

Every occurrence of needle (in the text without markup), as parts. A $math$ span reads as its TeX: txt["f'(x)"].

Parameters:

Name Type Default Description
needle str required Every occurrence of needle (in the text without markup), as parts.

k.Math.chars (property)

math.chars: PartView  # read-only

Characters as parts: txt.chars[3:7] (markup removed).

k.Math.words (property)

math.words: PartView  # read-only

Whitespace-separated words as parts: txt.words[1].

k.Math.lines (property)

math.lines: PartView  # read-only

Laid-out lines as parts: txt.lines[0].

k.Code (class)

k.Code(
    src: str,
    lang: str = "python",
    *,
    theme: str = "auto",
    line_numbers: bool = False,
    size: float = 0.32,
    **props: Unpack[StyleKeywords],
)

Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background). code.highlight highlights lines and k.morph between two versions animates the diff: unchanged lines slide, inserted lines enter, removed lines leave.

Parameters:

Name Type Default Description
src str required
lang str "python" Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background).
theme str "auto" Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background).
line_numbers bool False Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background).
size float 0.32 Code with syntax highlighting (tree-sitter) and stable tokens: lang=, line_numbers=True, size=, theme="auto" (follows the scene background).
**props Unpack[StyleKeywords] variadic Keyword arguments (StyleKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis, fill: ColorVal, fill_opacity: FloatVal, stroke: ColorVal, stroke_width: FloatVal, dash: FloatsVal, glow: FloatVal, glow_color: ColorVal, color: ColorVal.

Props (animatable with .to(), settable with .set() or in the constructor):

Prop Kind Default Interpolation
fill color theme.fg linear
fill_opacity float 1.0 linear
stroke color theme.fg linear
stroke_width float 0.0 linear
dash floats () step_end
glow float 0.0 linear
glow_color color theme.no_color linear
code str "" step_end
lang str "text" step_end
size float 0.32 linear
line_numbers bool False step_end
palette str "dark" step_end
highlight floats () step_start
highlight_amount float 0.0 linear

Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible, bleed, emphasis.

Example:

SRC = """
def add(a, b):
    return a + b
"""

@k.scene
def code_block(s: k.Scene):
    code = k.Code(SRC, lang="python", line_numbers=True).place(at="center")
    s.play(k.write(code))
    s.wait(0.5)

See also: code.highlight, k.morph, k.Text.

Members:

  • highlight: Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight.
  • find_all: Every occurrence of needle (in the text without markup), as parts.
  • chars: Characters as parts: txt.chars[3:7] (markup removed).
  • words: Whitespace-separated words as parts: txt.words[1].
  • lines: Laid-out lines as parts: txt.lines[0].

Inherited from k.Group: children, to, swap, insert, pop, fit. Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.

k.Code.highlight (method)

code.highlight(
    lines: Sequence[int] | None = None,
    *,
    duration: float | None = None,
    ease: EaseLike | None = None,
) -> Animation

Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight. Equivalent to code.to(highlight=lines, highlight_amount=1).

Parameters:

Name Type Default Description
lines Sequence[int] | None None Named transition: dims every line except lines (numbered from 1); code.highlight(None) removes the highlight.
duration float | None None
ease EaseLike | None None

Example:

SRC = """
x = 1
y = 2
print(x + y)
"""

@k.scene
def code_highlight(s: k.Scene):
    code = k.Code(SRC, lang="python").place(at="center")
    s.add(code)
    s.play(code.highlight(lines=[3]))
    s.play(code.highlight(None))

See also: k.Code.

k.Code.find_all (method)

code.find_all(needle: str) -> list[TextPart]

Every occurrence of needle (in the text without markup), as parts. A $math$ span reads as its TeX: txt["f'(x)"].

Parameters:

Name Type Default Description
needle str required Every occurrence of needle (in the text without markup), as parts.

k.Code.chars (property)

code.chars: PartView  # read-only

Characters as parts: txt.chars[3:7] (markup removed).

k.Code.words (property)

code.words: PartView  # read-only

Whitespace-separated words as parts: txt.words[1].

k.Code.lines (property)

code.lines: PartView  # read-only

Laid-out lines as parts: txt.lines[0].

k.underline (function)

k.underline(
    target: Node,
    *,
    pad: float = 0.08,
    color: ColorLike = k.YELLOW,
    stroke_width: FloatVal = 4.0,
    **props: Unpack[VisibilityKeywords],
) -> Line

Annotation marks on any object or part of a text or formula (eq["dx"]): k.underline(target), k.box(target), k.encircle(target), k.strike(target) (crossed out) and k.cross(target) (an X). Each is an ordinary object bound to the target's box in world coordinates, so it follows the target; it enters with k.draw and leaves with k.fade_out. pad= (or overhang= for strike), color= and stroke_width=.

Parameters:

Name Type Default Description
target Node required A line under target, pad below its box; k.draw draws it left to right.
pad float 0.08 pad= (or overhang= for strike), color= and stroke_width=.
color ColorLike k.YELLOW pad= (or overhang= for strike), color= and stroke_width=.
stroke_width FloatVal 4.0 pad= (or overhang= for strike), color= and stroke_width=.
**props Unpack[VisibilityKeywords] variadic Keyword arguments (VisibilityKeywords): opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

Example:

@k.scene
def marks(s: k.Scene):
    eq = k.Math(r"a^2 + b^2 = c^2", size=0.8).place(at="center")
    s.add(eq)
    s.play(k.draw(k.box(eq["c^2"])))
    s.play(k.draw(k.underline(eq["a^2"])), k.draw(k.encircle(eq["b^2"])))
    wrong = k.Text("a + b = c", size=0.5).place(below=eq, gap=1)
    s.play(k.write(wrong))
    s.play(k.draw(k.cross(wrong)))
    s.wait(0.5)

See also: k.Math, k.draw, k.fade_out.

k.box (function)

k.box(
    target: Node,
    *,
    pad: float = 0.12,
    radius: float = 0.08,
    color: ColorLike = k.YELLOW,
    stroke_width: FloatVal = 4.0,
    **props: Unpack[VisibilityKeywords],
) -> RoundedRect

A rounded box around target, pad away from its box.

Documented together with k.underline.

Parameters:

Name Type Default Description
target Node required A rounded box around target, pad away from its box.
pad float 0.12 A rounded box around target, pad away from its box.
radius float 0.08
color ColorLike k.YELLOW
stroke_width FloatVal 4.0
**props Unpack[VisibilityKeywords] variadic Keyword arguments (VisibilityKeywords): opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

k.cross (function)

k.cross(
    target: Node,
    *,
    pad: float = 0.06,
    color: ColorLike = k.RED,
    stroke_width: FloatVal = 4.0,
    **props: Unpack[VisibilityKeywords],
) -> Group

An X over target: two lines corner to corner, pad past its box.

Documented together with k.underline.

Parameters:

Name Type Default Description
target Node required An X over target: two lines corner to corner, pad past its box.
pad float 0.06 An X over target: two lines corner to corner, pad past its box.
color ColorLike k.RED
stroke_width FloatVal 4.0
**props Unpack[VisibilityKeywords] variadic Keyword arguments (VisibilityKeywords): opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

k.encircle (function)

k.encircle(
    target: Node,
    *,
    pad: float = 0.18,
    color: ColorLike = k.YELLOW,
    stroke_width: FloatVal = 4.0,
    **props: Unpack[VisibilityKeywords],
) -> Ellipse

An ellipse around target (its box, pad larger on each side, times √2 so the corners fit inside).

Documented together with k.underline.

Parameters:

Name Type Default Description
target Node required An ellipse around target (its box, pad larger on each side, times √2 so the corners fit inside).
pad float 0.18 An ellipse around target (its box, pad larger on each side, times √2 so the corners fit inside).
color ColorLike k.YELLOW
stroke_width FloatVal 4.0
**props Unpack[VisibilityKeywords] variadic Keyword arguments (VisibilityKeywords): opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

k.strike (function)

k.strike(
    target: Node,
    *,
    overhang: float = 0.06,
    color: ColorLike = k.RED,
    stroke_width: FloatVal = 4.0,
    **props: Unpack[VisibilityKeywords],
) -> Line

A line through the middle of target (struck out), overhang past each side.

Documented together with k.underline.

Parameters:

Name Type Default Description
target Node required A line through the middle of target (struck out), overhang past each side.
overhang float 0.06 A line through the middle of target (struck out), overhang past each side.
color ColorLike k.RED
stroke_width FloatVal 4.0
**props Unpack[VisibilityKeywords] variadic Keyword arguments (VisibilityKeywords): opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

k.Terminal (class)

k.Terminal(
    *,
    title: str = "",
    prompt: str = "$ ",
    lang: str = "bash",
    width: float = 8.0,
    rows: int = 8,
    size: float = 0.28,
    theme: str = "auto",
    chrome: bool = True,
    caret: bool = True,
    **props: Unpack[TransformKeywords],
)

A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto". term.type(cmd) types a command after the prompt as k.Code, term.output(text) prints monospace lines; the window grows row by row and scrolls up when full. term.lines are the rows on screen (an input row has .prompt and .code), term.frame, term.title and term.caret are the window's parts.

Parameters:

Name Type Default Description
title str "" A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
prompt str "$ " A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
lang str "bash" A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
width float 8.0 A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
rows int 8 A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
size float 0.28 A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
theme str "auto" A terminal or REPL window: title=, prompt= ("$ ", ">>>"), lang= of the commands ("bash", "python"...), width= and rows= on screen, size=, theme="auto".
chrome bool True
caret bool True
**props Unpack[TransformKeywords] variadic Keyword arguments (TransformKeywords): name: str | None, key: str | None, x: FloatVal, y: FloatVal, position: VecVal, rotate: FloatVal, anchor: VecVal, z: FloatVal, scale: FloatVal, scale_x: FloatVal, scale_y: FloatVal, opacity: FloatVal, visible: BoolVal, bleed: BoolVal, emphasis: Emphasis.

Props inherited from k.Node: x, y, rotate, scale, scale_x, scale_y, anchor, opacity, z, visible, bleed, emphasis.

Example:

@k.scene
def terminal(s: k.Scene):
    term = k.Terminal(title="zsh", width=9, rows=5).place(at="center")
    s.play(k.fade_in(term), duration=0.4)
    s.play(term.type("pip install kinemo"))
    s.play(term.output("Successfully installed kinemo"))
    s.play(term.type("kinemo check scene.py"))
    s.play(term.output("scene.py — scene 'hello' — ok"))
    s.wait(0.5)

See also: terminal.type, terminal.output, k.Code.

Members:

  • type: Named transition: types a command after the prompt, character by character, as k.Code in the terminal's language, with the caret following; cps= characters per second (22 by default) or duration=.
  • output: Named transition: prints text below the last row, one row per line, all at once or stagger= seconds apart; lines that do not fit scroll the older ones up.
  • clear: Remove every row (they fade out); the next row starts at the top.

Inherited from k.Group: children, to, swap, insert, pop, fit. Inherited from k.Node: set, unbind, edge, age, entered, exited, copy, place, to_place, unpin.

k.Terminal.type (method)

terminal.type(
    command: str,
    *,
    duration: float | None = None,
    cps: float = 22.0,
) -> Animation

Named transition: types a command after the prompt, character by character, as k.Code in the terminal's language, with the caret following; cps= characters per second (22 by default) or duration=.

Parameters:

Name Type Default Description
command str required Type command after the prompt, character by character (cps per second, or over duration), as k.Code in the terminal's language; the caret follows the typing.
duration float | None None Named transition: types a command after the prompt, character by character, as k.Code in the terminal's language, with the caret following; cps= characters per second (22 by default) or duration=.
cps float 22.0 Named transition: types a command after the prompt, character by character, as k.Code in the terminal's language, with the caret following; cps= characters per second (22 by default) or duration=.

Example:

@k.scene
def repl(s: k.Scene):
    term = k.Terminal(title="python", prompt=">>>", lang="python", rows=4).place(at="center")
    s.add(term)
    s.play(term.type("sum(range(10))", cps=12))
    s.play(term.output("45"))
    s.wait(0.5)

See also: k.Terminal, terminal.output.

k.Terminal.output (method)

terminal.output(
    text: str,
    *,
    stagger: float = 0.0,
    duration: float | None = None,
) -> Animation

Named transition: prints text below the last row, one row per line, all at once or stagger= seconds apart; lines that do not fit scroll the older ones up. term.clear() fades every row out and starts again at the top.

Parameters:

Name Type Default Description
text str required Named transition: prints text below the last row, one row per line, all at once or stagger= seconds apart; lines that do not fit scroll the older ones up.
stagger float 0.0 Named transition: prints text below the last row, one row per line, all at once or stagger= seconds apart; lines that do not fit scroll the older ones up.
duration float | None None

Example:

@k.scene
def build_log(s: k.Scene):
    term = k.Terminal(title="cargo", rows=4).place(at="center")
    s.add(term)
    s.play(term.type("cargo build"))
    s.play(term.output("Compiling kinemo\nCompiling kinemo-py\nFinished release", stagger=0.4))
    s.play(term.clear())
    s.wait(0.5)

See also: k.Terminal, terminal.type.

k.Terminal.clear (method)

terminal.clear(*, duration: float = 0.3) -> Animation

Remove every row (they fade out); the next row starts at the top.

Parameters:

Name Type Default Description
duration float 0.3