Events
Events tell your code what happens in the viewer. Listen to them with on(). In Vue they are component events, such as @apply, and in React they are props, such as onApply. A listener that throws does not stop the viewer: the page gets the error as any uncaught one, and the other listeners still run.
pic.on('apply', ({ blob }) => upload(blob))load
- Payload:
Size
The image is loaded and on display. width and height are its size in pixels. It fires each time a src loads, also with reload(), but not after Apply, whose result comes with apply.
error
- Payload:
{ error: Error, phase: 'load' | 'preview' | 'apply' }
Something failed, and phase says what:
'load': the image could not be loaded.'preview': an edit could not be shown, as when the image is too large for the browser. The edit stays open.'apply': the changes could not be applied. The edit stays open.
change
- Payload:
PaperPicState
The state changed, through the user or through a method, such as by zooming, opening a tool or undoing, or because Straighten found the document. It carries the whole state, the same that getState() would return at that moment. Apply emits it twice: when it starts, with busy: true, and when it ends well, after apply.
These change the state without emitting change:
- Loading an image, which
loadorerrorreports. - An Apply that fails, which
errorreports.busyisfalseagain. setOptions()and the props of the components, so two-way bindings do not loop.- A resize of the viewer, which changes
zoomwhile a fit is in effect.
A pan emits nothing, because the state does not hold the position of the image. State shows how to keep controls of your own in step.
apply
- Payload:
ExportedImage
Apply finished, from the button, Enter or apply(), and the result is already on display. blob holds the image in the format type, which output chooses.
download
- Payload:
PaperPicDownload
The image is about to be saved, from the download button or from download(). source is the image on display: the result of the last Apply, or else the src. filename is the name of the file.
Call preventDefault() to stop the browser from saving the file, such as to save it another way:
pic.on('download', ({ source, filename, preventDefault }) => {
preventDefault()
saveToCloud(source, filename)
})