Custom toolbar
When the controls of the viewer must match the rest of your interface, hide the built-in ones with toolbar: false and call the methods from buttons of your own. This example offers zoom, Straighten, Undo and Apply, and enables each button only when it can act.
The change event carries the state after each action, including those your buttons start. load and error report the moments when the image arrives or Apply fails.
html
<div id="controls">
<button type="button" data-action="zoomIn">Zoom in</button>
<button type="button" data-action="zoomOut">Zoom out</button>
<button type="button" data-action="edit">Edit</button>
<button type="button" data-action="straighten">Straighten</button>
<button type="button" data-action="confirmTool">Done</button>
<button type="button" data-action="cancelTool">Cancel this tool</button>
<button type="button" data-action="undo">Undo</button>
<button type="button" data-action="cancel">Cancel</button>
<button type="button" data-action="apply">Apply</button>
</div>
<p id="hint"></p>
<div id="viewer" style="height: 480px"></div>ts
import type { PaperPicState } from '@paperpic/core'
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/receipt.jpg',
alt: 'A receipt',
toolbar: false,
})
const actions: Record<string, () => void> = {
zoomIn: () => pic.zoomIn(),
zoomOut: () => pic.zoomOut(),
edit: () => pic.edit(),
straighten: () => pic.openTool('perspective'),
confirmTool: () => pic.confirmTool(),
cancelTool: () => pic.cancelTool(),
undo: () => pic.undo(),
cancel: () => pic.cancel(),
apply: () => pic.apply(),
}
const buttons = document.querySelectorAll<HTMLButtonElement>('[data-action]')
for (const button of buttons) {
button.addEventListener('click', () => actions[button.dataset.action!]!())
}
function update({ status, mode, activeTool, hint, canUndo, canApply, canConfirm, busy }: PaperPicState) {
const editing = mode === 'edit'
const visible: Record<string, boolean> = {
zoomIn: true,
zoomOut: true,
edit: !editing,
straighten: editing && !activeTool,
confirmTool: Boolean(activeTool),
cancelTool: Boolean(activeTool),
undo: editing && !activeTool,
cancel: editing && !activeTool,
apply: editing && !activeTool,
}
for (const button of buttons) {
const action = button.dataset.action!
button.hidden = !visible[action]
}
document.querySelector<HTMLButtonElement>('[data-action="edit"]')!.disabled = status !== 'ready'
document.querySelector<HTMLButtonElement>('[data-action="undo"]')!.disabled = !canUndo || busy
document.querySelector<HTMLButtonElement>('[data-action="confirmTool"]')!.disabled = !canConfirm
document.querySelector<HTMLButtonElement>('[data-action="apply"]')!.disabled = !canApply || busy
document.querySelector('#hint')!.textContent = hint ?? ''
}
pic.on('change', update)
pic.on('load', () => update(pic.getState()))
pic.on('error', () => update(pic.getState()))
update(pic.getState())A slider of your own
A slider of your own for Clean up opens the tool first. Each move then calls setFinish(), which changes what Clean up shows, and confirmTool() keeps the last value as one edit. Here Done also applies the result, and Cancel leaves the edit mode, so these controls are the whole interface.
html
<button id="clean-up" type="button">Clean up</button>
<div id="ruler" hidden>
<label>
Sensitivity
<input id="sensitivity" type="range" min="0" max="1" step="0.05" />
</label>
<button id="done" type="button">Done</button>
<button id="cancel" type="button">Cancel</button>
</div>
<div id="viewer" style="height: 480px"></div>ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/receipt.jpg',
alt: 'A receipt',
toolbar: false,
})
const slider = document.querySelector<HTMLInputElement>('#sensitivity')!
document.querySelector('#clean-up')!.addEventListener('click', () => {
pic.edit()
pic.openTool('finish')
})
slider.addEventListener('input', () => pic.setFinish({ sensitivity: slider.valueAsNumber }))
document.querySelector('#done')!.addEventListener('click', () => {
pic.confirmTool()
pic.apply()
})
document.querySelector('#cancel')!.addEventListener('click', () => {
pic.cancelTool()
pic.cancel()
})
pic.on('change', ({ activeTool, finish }) => {
const open = activeTool === 'finish'
document.querySelector<HTMLElement>('#ruler')!.hidden = !open
document.querySelector<HTMLElement>('#clean-up')!.hidden = open
slider.value = String(finish.sensitivity)
})Notes
- With
toolbar: false, the built-in edit bar is hidden too, so Crop and Straighten show their handles but no Done button. Your interface confirms them withconfirmTool(), asEnterdoes. To close them without their changes, callcancelTool(), asEscdoes. - While Crop or Straighten is open,
hintin the state says what to do, such as "Drag the corners", as the built-in edit bar does. Show it, because withtoolbar: falsenothing else does. A new hint, such as after pressing a corner or once Straighten finds the document, comes withchange. canConfirmin the state says whetherconfirmTool()can keep what the tool shows. Straighten waits until it finds the document or the user moves a corner, so disable your Done button until then.- Sliders of your own can call
setFinish()andsetAdjustments()directly, which adds one edit for each call. To leave one edit for a whole drag, open the tool withopenTool()first: the same methods then change what it shows, andconfirmTool()keeps it. cancel()does not ask before discarding the edits. Ask the user first if there are some, such as whencanApplyis true.- The keyboard shortcuts keep working with the focus on the viewer. See Accessibility.