# Aspect Ratio

A box that keeps its shape while its content loads, optionally with a pencilled frame round the edge.

Docs: https://ballpoint.st9wd.com/docs/aspect-ratio

```bash
npx shadcn@latest add @ballpoint/aspect-ratio
```

```tsx
import { AspectRatio } from "@/components/ui/aspect-ratio"

<AspectRatio ratio={16 / 9} framed>
  <img src="/photo.jpg" alt="…" className="size-full object-cover" />
</AspectRatio>
```

Props:

- `ratio`: `number` (default `1`). Width over height: 16 / 9, 4 / 3, 1.
- `framed`: `boolean` (default `false`). Draws a pencilled frame round the box.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `weight`, `speed` (see Pen settings).

## Example

```tsx
import { AspectRatio } from "@/components/ui/aspect-ratio";

export default function AspectRatioDemo() {
  return (
    <div className="w-full max-w-md">
      <AspectRatio ratio={16 / 9} framed seed="ratio-demo">
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img src="/doodle-portrait.svg" alt="A doodled portrait" className="size-full object-cover object-[50%_43%]" />
      </AspectRatio>
      <p className="mt-3 text-center text-sm text-ink-3">16 : 9</p>
    </div>
  );
}
```

## Ratios

Square, four to three, widescreen and portrait.

```tsx
import { AspectRatio } from "@/components/ui/aspect-ratio";

const ratios = [
  ["1 : 1", 1],
  ["4 : 3", 4 / 3],
  ["16 : 9", 16 / 9],
  ["9 : 16", 9 / 16],
] as const;

export default function AspectRatioRatios() {
  return (
    <div className="flex w-full max-w-xl flex-wrap items-end gap-6">
      {ratios.map(([label, ratio]) => (
        <figure key={label} className="flex flex-col gap-2" style={{ width: ratio < 1 ? 90 : 150 }}>
          <AspectRatio ratio={ratio} framed seed={`ratio-${label}`} className="text-ink-3">
            <div className="grid size-full place-items-center text-lg">{label}</div>
          </AspectRatio>
          <figcaption className="text-center text-sm text-ink-3">{ratio.toFixed(2)}</figcaption>
        </figure>
      ))}
    </div>
  );
}
```

## Holding a place

An image box that does not jump when the picture arrives.

```tsx
import { AspectRatio } from "@/components/ui/aspect-ratio";

export default function AspectRatioImage() {
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <AspectRatio ratio={4 / 3} className="bg-ink-5/50">
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img src="/doodle-portrait.svg" alt="A doodled portrait" className="size-full object-cover" />
      </AspectRatio>
      <p className="text-sm text-ink-3">The box holds its shape while the picture loads, so nothing below it jumps.</p>
    </div>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
