Skip to content

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.
  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: 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: 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, 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 ​

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 ​

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

ChoiceProportions
AutoaspectRatio, or else an estimate
A4210 × 297 mm, which A3 and A5 share
Letter8.5 × 11 in, US Letter
Card85.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 ​

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 to its width divided by its height, and the result takes those proportions:

ts
const pic = new PaperPic(container, {
  src: '/invoice.jpg',
  aspectRatio: 210 / 297,
})
DocumentaspectRatio
A4 sheet, portrait210 / 297
US Letter sheet, portrait8.5 / 11
ID card, landscape85.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 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 can fix.

The playground has a sample receipt with a cut-off corner to try it.

From code ​

Straighten opens with openTool('perspective') and closes with confirmTool() or 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() while the tool is open. confirmTool() waits until the viewer finds the document or a corner moves, and the canConfirm of the state says when it can.

Released under the MIT License.