---
url: https://paperpic.dev/guide/tools/straighten.md
description: >-
  How the Straighten tool of PaperPic turns a photo taken at an angle into a
  flat page, how it estimates proportions and when to set aspectRatio. Live
  demo.
---

# Straighten

A document photographed at an angle looks like a trapezoid: the far edge is shorter and the text leans. Straighten turns it back into a flat, rectangular page, as if it had been scanned. Its id in the API is `perspective`.

## Use it

1. Press Edit, then Straighten. A handle appears for each corner, on the document if the viewer [finds it](#finding-the-document).
2. Drag each handle that is off onto its corner of the document. Zoom in with the buttons of its panel to place them precisely.
3. If you know the paper, choose its [size](#paper-size): A4, Letter or Card. Auto estimates it.
4. Press Done, or `Enter`. The viewer shows the page flat, cut to the four corners. Done waits until the viewer finds the document or you move a corner.

The handles work as in [Crop](./crop): a magnifier on touch screens, press and tap instead of dragging, and Tab and the arrow keys on the keyboard. Here the magnifier also shows while a mouse drags a corner, because each corner must land exactly on one of the paper. The four corners always keep a convex shape, so the page can always be flattened.

Straighten shows the photo without the black and white of [Clean up](./clean-up), because the edges of the paper are hard to find on a cleaned up page. Black and white comes back after Done, and gray stays.

## Finding the document {#finding-the-document}

As Straighten opens, the viewer looks for the document in the photo and places the four corners on it, so Done is ready at once. The search runs in the browser in a fraction of a second, and it changes nothing until Done.

The viewer proposes only what it is sure enough of. If the paper blends into the table, or the photo has no document, the corners start inside the image and wait for the user. A corner that the user has already touched never moves on its own.

## Paper size {#paper-size}

The panel of Straighten offers paper sizes, which give the result the exact proportions of that paper:

| Choice | Proportions                                                                               |
| ------ | ----------------------------------------------------------------------------------------- |
| Auto   | [`aspectRatio`](../../reference/options#aspectratio), or else an [estimate](#proportions) |
| A4     | 210 × 297 mm, which A3 and A5 share                                                       |
| Letter | 8.5 × 11 in, US Letter                                                                    |
| Card   | 85.6 × 53.98 mm, an ID or a bank card                                                     |

A size sets only the proportions: it does not move the corners. The paper stands or lies as the corners do, so there is no orientation to choose. A size that is not the one of the document distorts it, so Straighten starts in Auto each time it opens.

With the four corners on the corners of the image, Auto changes nothing, and a size gives the whole image the proportions of that paper.

## Proportions {#proportions}

A photo does not tell the real shape of the page, so Straighten estimates its proportions from where the corners sit in the photo. The estimate suits photos taken from a normal distance, but it cannot be exact.

The estimate uses the original photo, even after a crop or a rotation in the same edit. Once the user applies, the result counts as the new photo, so straighten before applying.

When you know the document, set [`aspectRatio`](../../reference/options#aspectratio) to its width divided by its height, and the result takes those proportions:

:::tabs key:framework
\== JavaScript

```ts
const pic = new PaperPic(container, {
  src: '/invoice.jpg',
  aspectRatio: 210 / 297,
})
```

\== Vue

```vue
<PaperPic src="/invoice.jpg" :aspect-ratio="210 / 297" />
```

\== React

```tsx
<PaperPic src="/invoice.jpg" aspectRatio={210 / 297} />
```

:::

| Document                  | `aspectRatio`  |
| ------------------------- | -------------- |
| A4 sheet, portrait        | `210 / 297`    |
| US Letter sheet, portrait | `8.5 / 11`     |
| ID card, landscape        | `85.6 / 53.98` |

For a sheet in landscape, swap the numbers, such as `297 / 210`. The proportions apply to the page as it shows on screen, so turn a sideways photo with [Rotate left](./rotate-and-flip) before you straighten it.

## Corners cut off by the photo

Sometimes a corner of the document falls outside the photo. The handles stay inside the image, so place that handle on the border of the photo, along the edge of the document that keeps the content. The last rows lean slightly and the estimated proportions suffer, which [`aspectRatio`](../../reference/options#aspectratio) can fix.

The [playground](../../playground) has a sample receipt with a cut-off corner to try it.

## From code

Straighten opens with [`openTool('perspective')`](../../reference/methods#opentool) and closes with [`confirmTool()`](../../reference/methods#confirmtool) or [`cancelTool()`](../../reference/methods#canceltool):

```ts
pic.edit()
pic.openTool('perspective')
pic.setPaper({ size: 'a4' })

// Later, from a button of your own
pic.confirmTool()
```

Choose the paper size with [`setPaper()`](../../reference/methods#setpaper) while the tool is open. `confirmTool()` waits until the viewer finds the document or a corner moves, and the `canConfirm` of the [state](../../reference/state) says when it can.
