Skip to content

rain

The digital rain Reloaded plays as it closes.

Every drop spells the word, so a trail reads "mujoco-mojo" from the bottom up, each column starting at a different letter. The head is the bright bottom end, so the start of the word is what shows brightest and the end is what fades. It is kept calm on purpose. A letter is set once, when a drop reaches it, and then holds still as the trail fades, and the trail fades from the accent color into the background. A fresh random glyph in every cell on every frame is what makes rain look blotchy.

WORD module-attribute

Python
WORD: str = 'mujoco-mojo'

What the drops spell, read from the bottom of a trail up, over and over.

WORD_GAP module-attribute

Python
WORD_GAP: int = 4

Blank cells between one reading of the word and the next. 0 runs them together.

COLUMN_SPACING module-attribute

Python
COLUMN_SPACING: int = 2

Only every this many columns carries a drop, so 2 leaves a blank column between drops.

TRAIL_LENGTH module-attribute

Python
TRAIL_LENGTH: tuple[int, int] = (len(WORD), 16)

The shortest and longest trail a drop can have.

RESTART_WITHIN module-attribute

Python
RESTART_WITHIN: float = 1 / 3

A drop that has left the screen starts again within this fraction of the screen height above it, which sets how closely one follows another.

Drop dataclass

Python
Drop(head: int, length: int, period: int, phase: int = 0)

One falling trail.

head instance-attribute

Python
head: int

Row of the leading letter. Negative while it is still above the screen.

length instance-attribute

Python
length: int

How many letters trail behind the head.

period instance-attribute

Python
period: int

It falls one row every this many steps, so some drops are slower than others.

phase class-attribute instance-attribute

Python
phase: int = 0

Which letter of the text sits on row 0, so neighboring columns do not spell it in step.

Rain dataclass

Python
Rain(width: int, height: int, rng: Random = Random())

The falling letters as plain data, so the animation can be checked without a screen.

drops class-attribute instance-attribute

Python
drops: dict[int, Drop] = field(init=False)

The drop in each column that has one.

letters class-attribute instance-attribute

Python
letters: list[list[str]] = field(init=False)

The letter at each row and column, fixed until a drop's head next reaches that cell. A cell no drop has reached holds a space.

step

Python
step() -> None

Moves every drop down, setting the letter of the text where a head arrives and sending a drop that has left the screen back above it.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def step(self) -> None:
    """Moves every drop down, setting the letter of the text where a head arrives and sending a drop that has left the screen back above it."""
    self._step += 1
    for column, drop in self.drops.items():
        if self._step % drop.period:
            continue
        drop.head += 1
        if 0 <= drop.head < self.height:
            self.letters[drop.head][column] = letter_at(drop.head, drop.phase)
        if drop.head - drop.length > self.height:
            self.drops[column] = self._new_drop(int(self.height * RESTART_WITHIN))

trail

Python
trail(row: int, column: int) -> tuple[int, int] | None

How far behind its drop's head the cell is and how long that drop's trail is, or None for an empty cell. A head is 0 behind.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def trail(self, row: int, column: int) -> tuple[int, int] | None:
    """How far behind its drop's head the cell is and how long that drop's trail is, or `None` for an empty cell. A head is 0 behind."""
    drop = self.drops.get(column)
    if drop is None:
        return None
    behind = drop.head - row
    return (behind, drop.length) if 0 <= behind <= drop.length else None

MatrixRain

Python
MatrixRain(rng: Random | None = None)

Bases: Widget

Draws a Rain over the whole widget and keeps it falling.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def __init__(self, rng: random.Random | None = None) -> None:
    super().__init__()
    self._rng = rng or random.Random()
    self._rain: Rain | None = None

RainScreen

Python
RainScreen(seconds: float)

Bases: Screen[None]

Fills the terminal with rain for a moment, then dismisses itself.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def __init__(self, seconds: float) -> None:
    super().__init__()
    self._seconds = seconds

repeating_text

Python
repeating_text() -> str

The word followed by its gap, which a trail repeats from the bottom up.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def repeating_text() -> str:
    """The word followed by its gap, which a trail repeats from the bottom up."""
    return WORD + " " * WORD_GAP

letter_at

Python
letter_at(row: int, phase: int) -> str

The letter a head sets on row. The text reads from the bottom up, so each row down is one letter back.

Source code in src/mujoco_mojo/utils/layers/reloaded/rain.py
Python
def letter_at(row: int, phase: int) -> str:
    """The letter a head sets on `row`. The text reads from the bottom up, so each row down is one letter back."""
    text = repeating_text()
    return text[(phase - row) % len(text)]