# Drawing and media

Build a sketch pad from the smallest app: a canvas, a photo to trace, your own palette of colors, undo and an eraser, and ink that thickens as you slow down.

Media tags put the phone's own capabilities on screen: `<canvas>` for drawing, `<img>` and `<video>` for pictures and film, `<camera>` and `<audio>` for what the phone can capture and play. Each one is a single tag with attributes, and each answers commands from any button: `#pad.undo`, `#pad.clear`, `#pad.pen.color:teal`.

This guide builds a sketch pad that traces over a photo.

## 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 screen's title across the top, and `<config primary_color="amber">` gives every control an amber. Use a named color like `amber`, `teal` or `rose`.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <text>Hello</text>
        </main>
    </screen>
</app>
```

## Step 3: Add a canvas

`<canvas>` is a drawing surface: draw with a finger or a pencil, pinch to zoom and pan. `id="pad"` names it, so buttons can talk to it later, and `w-full h-380` makes it the screen's width and 380 points tall. It comes with its own toolbar of colors and sizes.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column>
                <canvas id="pad"
                        style="w-full h-380"/>
            </column>
        </main>
    </screen>
</app>
```

## Step 4: Trace a photo

`underlay` lays a picture under the canvas, `underlay-opacity="0.35"` fades it so the ink stands out, and `underlay-fit="cover"` fills the canvas with it. The photo is a guide only: it's not part of the drawing.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column>
                <canvas id="pad"
                        underlay="https://hypeapps.dev/gallery/assets/landscape.jpg"
                        underlay-opacity="0.35"
                        underlay-fit="cover"
                        style="w-full h-380"/>
            </column>
        </main>
    </screen>
</app>
```

## Step 5: Make your own palette

`toolbar="false"` hides the built-in toolbar, so the app can have its own. A `<row>` of five round swatches, each a small `<column>` in a named color. Tapping one runs two commands: `#pad.pen` picks the pen, and `#pad.pen.color:teal` sets its color.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column>
                <canvas id="pad"
                        toolbar="false"
                        underlay="https://hypeapps.dev/gallery/assets/landscape.jpg"
                        underlay-opacity="0.35"
                        underlay-fit="cover"
                        style="w-full h-380"/>
                <row style="gap-12">
                    <column @click="#pad.pen; #pad.pen.color:black"
                            style="w-36 h-36 rounded-full bg-black"/>
                    <column @click="#pad.pen; #pad.pen.color:red"
                            style="w-36 h-36 rounded-full bg-red"/>
                    <column @click="#pad.pen; #pad.pen.color:amber"
                            style="w-36 h-36 rounded-full bg-amber"/>
                    <column @click="#pad.pen; #pad.pen.color:teal"
                            style="w-36 h-36 rounded-full bg-teal"/>
                    <column @click="#pad.pen; #pad.pen.color:white"
                            style="w-36 h-36 rounded-full bg-white"/>
                </row>
            </column>
        </main>
    </screen>
</app>
```

## Step 6: Add the tools

A second row of buttons talking to the pad: `#pad.undo`, `#pad.redo`, `#pad.eraser` and `#pad.clear`. The pad also publishes what it's doing, and `$pad.strokes` counts the strokes so far.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column>
                <canvas id="pad"
                        toolbar="false"
                        underlay="https://hypeapps.dev/gallery/assets/landscape.jpg"
                        underlay-opacity="0.35"
                        underlay-fit="cover"
                        style="w-full h-380"/>
                <row style="gap-12">
                    <column @click="#pad.pen; #pad.pen.color:black"
                            style="w-36 h-36 rounded-full bg-black"/>
                    <column @click="#pad.pen; #pad.pen.color:red"
                            style="w-36 h-36 rounded-full bg-red"/>
                    <column @click="#pad.pen; #pad.pen.color:amber"
                            style="w-36 h-36 rounded-full bg-amber"/>
                    <column @click="#pad.pen; #pad.pen.color:teal"
                            style="w-36 h-36 rounded-full bg-teal"/>
                    <column @click="#pad.pen; #pad.pen.color:white"
                            style="w-36 h-36 rounded-full bg-white"/>
                </row>
                <row>
                    <button @click="#pad.undo"
                            icon="undo"/>
                    <button @click="#pad.redo"
                            icon="redo"/>
                    <button @click="#pad.eraser"
                            icon="ink_eraser"/>
                    <text flex="1">@{$pad.strokes} strokes</text>
                    <button @click="#pad.clear"
                            icon="delete">Clear</button>
                </row>
            </column>
        </main>
    </screen>
</app>
```

## Step 7: Get the ink right

The pen gets its own settings: `pen-color="black"` and `pen-width="6"` to start, and `pen-dynamics="fountain"`, which makes the line thinner when you draw fast and fuller when you slow down, like a fountain pen.

`var-ink` remembers which swatch is picked, each swatch sets it, and a check mark with `@show` marks the one in use. The eraser clears it, since no color is picked.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column var-ink="black">
                <canvas id="pad"
                        toolbar="false"
                        pen-color="black"
                        pen-width="6"
                        pen-dynamics="fountain"
                        underlay="https://hypeapps.dev/gallery/assets/landscape.jpg"
                        underlay-opacity="0.35"
                        underlay-fit="cover"
                        style="w-full h-380"/>
                <row style="gap-12">
                    <column @click="#pad.pen; #pad.pen.color:black; $ink = 'black'"
                            style="w-36 h-36 rounded-full items-center justify-center bg-black">
                        <icon name="check size-sm white"
                              @show="$ink == 'black'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:red; $ink = 'red'"
                            style="w-36 h-36 rounded-full items-center justify-center bg-red">
                        <icon name="check size-sm white"
                              @show="$ink == 'red'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:amber; $ink = 'amber'"
                            style="w-36 h-36 rounded-full items-center justify-center bg-amber">
                        <icon name="check size-sm black"
                              @show="$ink == 'amber'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:teal; $ink = 'teal'"
                            style="w-36 h-36 rounded-full items-center justify-center bg-teal">
                        <icon name="check size-sm white"
                              @show="$ink == 'teal'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:white; $ink = 'white'"
                            style="w-36 h-36 rounded-full items-center justify-center bg-white">
                        <icon name="check size-sm black"
                              @show="$ink == 'white'"/>
                    </column>
                </row>
                <row>
                    <button @click="#pad.undo"
                            icon="undo"/>
                    <button @click="#pad.redo"
                            icon="redo"/>
                    <button @click="#pad.eraser; $ink = ''"
                            icon="ink_eraser"/>
                    <text flex="1">@{$pad.strokes} strokes</text>
                    <button @click="#pad.clear"
                            icon="delete">Clear</button>
                </row>
            </column>
        </main>
    </screen>
</app>
```

## Step 8: Style it

The canvas gets rounded corners and a thin border, the swatches a rim, a shadow and a little press, the buttons an outline, and the stroke count fades back into the row.

That's a sketch pad: one tag for the canvas, and a few rows of buttons that tell it what to do.

```ham
<app>
    <config primary_color="amber"/>
    <screen>
        <app-bar title="Sketch"/>
        <main>
            <column style="w-full p-16 gap-14"
                    var-ink="black">
                <canvas id="pad"
                        toolbar="false"
                        pen-color="black"
                        pen-width="6"
                        pen-dynamics="fountain"
                        underlay="https://hypeapps.dev/gallery/assets/landscape.jpg"
                        underlay-opacity="0.35"
                        underlay-fit="cover"
                        style="w-full h-380 rounded-3xl border border-grey-300"/>
                <row style="w-full gap-12 justify-center">
                    <column @click="#pad.pen; #pad.pen.color:black; $ink = 'black'; haptic:selection"
                            style="w-36 h-36 rounded-full items-center justify-center bg-black border border-grey-300 shadow press:scale-90">
                        <icon name="check size-sm white"
                              @show="$ink == 'black'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:red; $ink = 'red'; haptic:selection"
                            style="w-36 h-36 rounded-full items-center justify-center bg-red border border-grey-300 shadow press:scale-90">
                        <icon name="check size-sm white"
                              @show="$ink == 'red'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:amber; $ink = 'amber'; haptic:selection"
                            style="w-36 h-36 rounded-full items-center justify-center bg-amber border border-grey-300 shadow press:scale-90">
                        <icon name="check size-sm black"
                              @show="$ink == 'amber'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:teal; $ink = 'teal'; haptic:selection"
                            style="w-36 h-36 rounded-full items-center justify-center bg-teal border border-grey-300 shadow press:scale-90">
                        <icon name="check size-sm white"
                              @show="$ink == 'teal'"/>
                    </column>
                    <column @click="#pad.pen; #pad.pen.color:white; $ink = 'white'; haptic:selection"
                            style="w-36 h-36 rounded-full items-center justify-center bg-white border border-grey-300 shadow press:scale-90">
                        <icon name="check size-sm black"
                              @show="$ink == 'white'"/>
                    </column>
                </row>
                <row style="w-full gap-8 items-center">
                    <button @click="#pad.undo"
                            variant="outlined"
                            icon="undo"
                            style="rounded-full"/>
                    <button @click="#pad.redo"
                            variant="outlined"
                            icon="redo"
                            style="rounded-full"/>
                    <button @click="#pad.eraser; $ink = ''"
                            variant="outlined"
                            icon="ink_eraser"
                            style="rounded-full"/>
                    <text style="muted font-sm text-right"
                          flex="1">@{$pad.strokes} strokes</text>
                    <button @click="#pad.clear"
                            variant="text"
                            icon="delete">Clear</button>
                </row>
            </column>
        </main>
    </screen>
</app>
```

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