Skip to content

Fixed proportions ​

Straighten estimates the proportions of a document from the photo, and the estimate cannot be exact. When your app knows what the user photographs, such as a Legal form or a passport page, aspectRatio gives the result its real shape. This example lets the user pick the type of document before straightening it.

html
<select id="type">
  <option value="legal">Legal sheet</option>
  <option value="passport">Passport page</option>
  <option value="envelope">DL envelope</option>
  <option value="other">Other</option>
</select>
<div id="viewer" style="height: 480px"></div>
ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'

const proportions: Record<string, number | undefined> = {
  legal: 8.5 / 14,
  passport: 125 / 88,
  envelope: 220 / 110,
  other: undefined,
}

const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
  src: '/scans/form.jpg',
  alt: 'Signed form',
  tools: ['perspective', 'rotate', 'finish'],
  aspectRatio: proportions.legal,
})

document.querySelector('#type')!.addEventListener('change', (event) => {
  const type = (event.target as HTMLSelectElement).value
  pic.setOptions({ aspectRatio: proportions[type] })
})

Notes ​

  • Straighten stores the proportions when the user presses Done, so choose the type before straightening. Changing it later affects only the next straightening.
  • For a document in landscape, swap the numbers, such as 14 / 8.5.
  • The proportions apply to the page as it shows on screen. If the photo is sideways, turn it with Rotate left before you use Straighten, or a sheet in portrait comes out squashed.
  • Leave aspectRatio out for documents without a fixed size, such as receipts, and Straighten estimates them.
  • The panel of Straighten already offers A4, Letter and Card as paper sizes, so this example adds other documents. A size the user chooses there wins over aspectRatio for that straightening.
  • Straighten explains the estimate and how to handle corners that fall outside the photo.

Released under the MIT License.