State
The state is a snapshot of the viewer: whether there is an image, the mode, the zoom, the open tool and whether there are changes to apply. You get it in two ways, and both give the same PaperPicState object:
getState()returns it whenever you ask.- The
changeevent sends it each time the user or a method changes it.
Keeping controls in step
change does not report the image. A loaded image comes with load, and a load or an Apply that fails comes with error. To keep controls of your own in step, such as an Apply button, read the state at the start, take it from each change, and read it again on load and error:
import type { PaperPicState } from '@paperpic/core'
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const applyButton = document.querySelector<HTMLButtonElement>('#apply')!
const pic = new PaperPic(container, { src: '/receipt.jpg', toolbar: false })
function update({ canApply, busy }: PaperPicState) {
applyButton.disabled = !canApply || busy
}
update(pic.getState())
pic.on('change', update)
pic.on('load', () => update(pic.getState()))
pic.on('error', () => update(pic.getState()))
applyButton.addEventListener('click', () => pic.apply())In Vue and React the viewer exists only once the component mounts, so the state starts as undefined and arrives with the first load or error. Until then, there is nothing to apply.
Fields
| Field | Type | Description |
|---|---|---|
activeTool | ToolId | null | The open tool, such as 'crop' or 'adjust', or null |
adjustments | { brightness, contrast } | The brightness and the contrast, each from -1 to 1. They do not apply in black and white. While Adjust is open, they are the values on its panel |
busy | boolean | Whether Apply is running |
canApply | boolean | Whether there are changes to apply. Rotations and flips that cancel out are no change |
canConfirm | boolean | Whether confirmTool() can keep what the open tool shows. Straighten waits until it finds the document or a corner moves. It is false with no tool open |
canRedo | boolean | Whether there is an edit to bring back |
canUndo | boolean | Whether there is an edit to undo |
finish | { mode, sensitivity } | The finish of Clean up, a FinishMode, and the sensitivity of black and white, from 0 to 1. While Clean up is open, they are the values on its panel |
fit | Fit | null | The fit in effect, or null once the user has zoomed by hand |
hint | string | null | What the open tool asks of the user, as the built-in edit bar shows it, such as "Drag the corners", or null. With toolbar: false, show it in your interface |
image | Size | null | The size of the image in pixels, at full resolution and with the edits so far, but without the turn of rotation, or null without an image |
mode | 'view' | 'edit' | Whether the user is viewing or editing the image |
paper | { size } | The paper size on the panel of Straighten, a PaperSize, while it is open, and 'auto' otherwise |
redaction | { color } | The color of Redact, a RedactColor: the one on its panel while it is open, and the one of the next area otherwise |
rotation | Rotation | How far the image on display is turned from image, in degrees clockwise |
status | 'empty' | 'loading' | 'ready' | 'error' | 'empty' without src, 'loading' while it loads, 'ready' once it is on display, and 'error' if it could not be loaded |
zoom | number | Screen pixels per pixel of the image |
With a rotation of 90 or 270, the image on screen, and the result of Apply, have the width and the height of image swapped. maxOutputSize can make the result smaller.