# Animations and effects

Build a 10-second tap game from the smallest app: three screens in one, a countdown, a tap counter, a best score that's remembered, and confetti when time's up.

Motion in HAM runs on the phone. You don't send frames; you name the effect, and the device plays it at full frame rate: a punch when something is tapped, a pop when it appears, a countdown that ticks, a burst of confetti. Each is a style token, a tag or one action, and each turns itself off when the phone asks for reduced motion.

This guide builds Tap rush: tap the circle as many times as you can in 10 seconds.

## Step 1: Start with the smallest app

Every HAM document starts with `<app>`. A `<screen>` is one page of the app, and `<main>` holds what's on it: for now, one line of `<text>`.

```ham
<app>
    <screen>
        <main>
            <text>Hello</text>
        </main>
    </screen>
</app>
```

## Step 2: Add a title bar and a color

`<app-bar>` puts the game's name across the top, and `<config primary_color="red">` gives every control a red. Use a named color like `red`, `teal` or `amber`.

```ham
<app>
    <config primary_color="red"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <text>Hello</text>
        </main>
    </screen>
</app>
```

## Step 3: Lay out the start screen

A `<stack>` holds the game. It draws its children in layers, and the second layer comes later. In it, a `<column>` with the question and a Start button.

```ham
<app>
    <config primary_color="red"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column>
                    <text>How many taps in 10 seconds?</text>
                    <button>Start</button>
                </column>
            </stack>
        </main>
    </screen>
</app>
```

## Step 4: Three screens in one

The game has three states, so `<globals state="ready"/>` declares `$state` for the whole app. `<@if>`, `<@elseif>` and `<@else>` build only the branch whose `test` is true:

- ready: the question and Start, which sets `$state = 'playing'`;
- playing: a `<countdown>` from 10, whose `on-done` sets `$state = 'done'` when it reaches zero;
- done: Time's up, and Again, back to ready.

Switching branches builds the new one fresh, so the countdown starts from 10 every time.

```ham
<app>
    <config primary_color="red"/>
    <globals state="ready"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column>
                    <@if test="$state == 'ready'">
                        <text>How many taps in 10 seconds?</text>
                        <button @click="$state = 'playing'">Start</button>
                    <@elseif test="$state == 'playing'">
                        <countdown seconds="10"
                                   format="ss"
                                   on-done="$state = 'done'"/>
                    <@else>
                        <text>Time's up!</text>
                        <button @click="$state = 'ready'">Again</button>
                    </@if>
                </column>
            </stack>
        </main>
    </screen>
</app>
```

## Step 5: Count the taps

A second global, `score`. Start resets it, and while playing, a column you tap runs `$score++` and shows the score with `@text`. The done screen says how many.

```ham
<app>
    <config primary_color="red"/>
    <globals state="ready"
             score="0"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column>
                    <@if test="$state == 'ready'">
                        <text>How many taps in 10 seconds?</text>
                        <button @click="$score = 0; $state = 'playing'">Start</button>
                    <@elseif test="$state == 'playing'">
                        <countdown seconds="10"
                                   format="ss"
                                   on-done="$state = 'done'"/>
                        <column @click="$score++">
                            <text @text="$score"/>
                            <text>TAP</text>
                        </column>
                    <@else>
                        <text>@{$score} taps!</text>
                        <button @click="$state = 'ready'">Again</button>
                    </@if>
                </column>
            </stack>
        </main>
    </screen>
</app>
```

## Step 6: Remember the best

`<globals persist best="0"/>` declares `$best` and keeps it across restarts. When the countdown ends, `$best = max($best, $score)` keeps the higher of the two, and the done screen says whether you beat it.

```ham
<app>
    <config primary_color="red"/>
    <globals state="ready"
             score="0"/>
    <globals persist
             best="0"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column>
                    <text>Best @{$best}</text>
                    <@if test="$state == 'ready'">
                        <text>How many taps in 10 seconds?</text>
                        <button @click="$score = 0; $state = 'playing'">Start</button>
                    <@elseif test="$state == 'playing'">
                        <countdown seconds="10"
                                   format="ss"
                                   on-done="$state = 'done'; $best = max($best, $score)"/>
                        <column @click="$score++">
                            <text @text="$score"/>
                            <text>TAP</text>
                        </column>
                    <@else>
                        <text>@{$score} taps!</text>
                        <text>
                            @{$score >= $best ? 'A new best.' : 'Your best is ' + $best + '.'}
                        </text>
                        <button @click="$state = 'ready'">Again</button>
                    </@if>
                </column>
            </stack>
        </main>
    </screen>
</app>
```

## Step 7: Celebrate

Now the motion:

- The stack's second layer is `<particles preset="confetti">`, stretched over the whole game. `count="0"` keeps it empty until `#party.burst` throws 160 pieces of confetti, which the countdown does at zero.
- Each tap runs `#pad.animate:punch-110-120`: the circle swells to 110% and back in 120 milliseconds.
- `haptic:light` on every tap and `haptic:success` at the end, so you feel the game too.

```ham
<app>
    <config primary_color="red"/>
    <globals state="ready"
             score="0"/>
    <globals persist
             best="0"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column>
                    <text>Best @{$best}</text>
                    <@if test="$state == 'ready'">
                        <text>How many taps in 10 seconds?</text>
                        <button @click="$score = 0; $state = 'playing'">Start</button>
                    <@elseif test="$state == 'playing'">
                        <countdown seconds="10"
                                   format="ss"
                                   on-done="$state = 'done'; $best = max($best, $score); #party.burst; haptic:success"/>
                        <column id="pad"
                                @click="$score++; #pad.animate:punch-110-120; haptic:light">
                            <text @text="$score"/>
                            <text>TAP</text>
                        </column>
                    <@else>
                        <text>@{$score} taps!</text>
                        <text>
                            @{$score >= $best ? 'A new best.' : 'Your best is ' + $best + '.'}
                        </text>
                        <button @click="$state = 'ready'">Again</button>
                    </@if>
                </column>
                <particles id="party"
                           preset="confetti"
                           count="0"
                           burst="160"
                           style="inset-0"/>
            </stack>
        </main>
    </screen>
</app>
```

## Step 8: Style it

The tap target becomes a 220-point circle (`w-220 h-220 rounded-full`) fading red to orange, with the score in huge white type, and `press:scale-95` sinks it under your finger. The countdown is big and red, the start and done screens get large type and full-width pill buttons, and the best score gets a trophy.

That's a game: three states, a countdown, a counter that punches, and confetti, with nothing running but the phone.

```ham
<app>
    <config primary_color="red"/>
    <globals state="ready"
             score="0"/>
    <globals persist
             best="0"/>
    <screen>
        <app-bar title="Tap rush"/>
        <main>
            <stack style="w-full">
                <column style="w-full p-24 gap-24 items-center">
                    <row style="gap-6 items-center">
                        <icon name="emoji_events amber"/>
                        <text style="font-bold">Best @{$best}</text>
                    </row>
                    <@if test="$state == 'ready'">
                        <text style="font-size-32 font-bold text-center">
                            How many taps in 10 seconds?
                        </text>
                        <button @click="$score = 0; $state = 'playing'"
                                variant="filled"
                                icon="play_arrow"
                                style="w-full rounded-full">Start</button>
                    <@elseif test="$state == 'playing'">
                        <countdown seconds="10"
                                   format="ss"
                                   on-done="$state = 'done'; $best = max($best, $score); #party.burst; haptic:success"
                                   style="font-size-56 text-red"/>
                        <column id="pad"
                                @click="$score++; #pad.animate:punch-110-120; haptic:light"
                                style="w-220 h-220 rounded-full items-center justify-center gradient-tl-red-orange-br shadow-xl press:scale-95">
                            <text style="text-white font-size-72 font-bold"
                                  @text="$score"/>
                            <text style="text-white opacity-80 font-sm">TAP</text>
                        </column>
                    <@else>
                        <text style="font-size-32 font-bold text-center">@{$score} taps!</text>
                        <text style="muted">
                            @{$score >= $best ? 'A new best.' : 'Your best is ' + $best + '.'}
                        </text>
                        <button @click="$state = 'ready'"
                                variant="filled"
                                icon="replay"
                                style="w-full rounded-full">Again</button>
                    </@if>
                </column>
                <particles id="party"
                           preset="confetti"
                           count="0"
                           burst="160"
                           style="inset-0"/>
            </stack>
        </main>
    </screen>
</app>
```

All of HAM, for AI tools: https://hypeapps.dev/llms.txt
