Editing
A photo of a document usually needs a few fixes before it is worth keeping. The edit mode groups them: the user makes the changes, checks them, and applies them all at once. The playground has the whole edit mode to try.
The edit flow
- The user presses Edit, the pencil in the toolbar. The toolbar hides, so nothing covers the image, and the edit bar appears at the bottom of the viewer.
- Rotate left and Flip add an edit at once. The other tools open a panel in the edit bar, and Done adds what the user chose there as one edit. Undo and Redo move through the edits.
- Apply computes the result and goes back to viewing. Cancel discards the edits, and asks first when there are some.
Nothing changes the image until Apply. Before that, the edits are a list that the viewer previews.
In a narrow viewer, the edit bar has two rows: the tools, or the panel of the open tool, above, and below, Cancel at the left and Apply, or Done, at the right. From 38rem wide, everything fits on one row. Either way, the bar keeps its height when a tool opens, so the image keeps its place. A dot marks Adjust and Clean up while their setting is in effect.
Tools
| Tool | What it does | Id |
|---|---|---|
| Crop | Keeps the part of the image inside four corners | crop |
| Straighten | Turns a photo taken at an angle into a flat page | perspective |
| Rotate left | Turns the image a quarter turn to the left | rotate |
| Flip | Mirrors the image left to right | flip |
| Adjust | Changes the brightness and the contrast | adjust |
| Clean up | Turns the document gray, or into crisp black and white without shadows | finish |
| Redact | Covers an area with solid black or white | redact |
The tools option chooses which ones the edit bar offers:
const pic = new PaperPic(container, {
src: '/receipt.jpg',
tools: ['crop', 'perspective', 'rotate'],
})Crop, Straighten, Redact, Adjust and Clean up open a panel in place of the tools. While one of them is open, the other tools, Undo, Redo and Apply wait. Done, or Enter, adds what the tool chose to the edits, and Cancel this tool, or Esc, discards it.
Inside a panel, the image looks as the result will, Clean up included. Straighten is the exception: it shows the photo without black and white, so the edges of the paper stay visible.
Preview and Apply
While editing, the viewer previews the edits on a copy of the image of about 2 megapixels, so it stays fast with large photos. Zoomed in, the preview looks softer than the result. Apply computes every edit at full resolution, or at maxOutputSize, in a Web Worker.
Apply cannot be interrupted. It blocks the controls and shows a message while it runs. Then the result replaces the image on display, the history empties, and the viewer goes back to the view mode. If Apply fails, the edit stays open, so the user can try again.
What your code receives
| Event | When |
|---|---|
apply | When the result is ready, with { blob, width, height, type } |
change | Each time the user or a method changes the state |
error | When the image cannot be loaded, or an edit cannot be shown or applied |
pic.on('apply', ({ blob }) => upload(blob))
pic.on('error', ({ error }) => console.error(error))What discards the edits
- Cancel, after the user confirms, or
cancel(), which does not ask. - A new
src, orreload(), while editing. - A change of
editablewhile editing.
After Apply, passing the same src again does nothing, so the result stays on display. reload() brings back the original.
Keyboard
With the focus on the viewer, keys undo, redo, confirm and cancel. Accessibility lists every key.