Skip to content

Viewer ​

Small print and long receipts are hard to read at a glance on a screen. The viewer fits the image when it loads and lets people zoom into any part of it.

Zoom and pan ​

ActionMouse and trackpadTouchKeyboard
ZoomWheel, or pinch on a trackpadPinch+ and -
PanDragDragArrow keys
Switch between the fit and 100%Double clickDouble tap
Fit the whole image0

The keys work while the focus is inside the viewer. On a button, the arrow keys move between the buttons instead, and while there is nothing to move, they scroll the page. A pinch zooms around the point between the fingers. On a touch screen, one finger scrolls the page while the image is fitted, and pans the image once it is zoomed in.

The zoom goes from the fit of the whole image, or 100% for a small image, up to 800%. An image that fits stays centered, and one that overflows pans up to its edges.

When the image loads, it fits the viewer whole, and it keeps its fit when the viewer changes size, until the user zooms by hand.

In a long page, the wheel would zoom the image instead of scrolling the page. Set wheelZoom: false there: the buttons, the keyboard and a pinch on a touch screen still zoom. A pinch on a trackpad no longer zooms the image, because the browser reports it as the wheel.

The toolbar ​

The toolbar sits at the right of the viewer, and scrolls when the viewer is too short for its buttons. It hides while editing, so it never covers a corner of Crop or Straighten.

ButtonWhat it does
EditStarts the edit mode. It shows once the image is ready, and never with editable: false
Zoom in, Zoom outZoom one step of 25%, around the center
Fit to width, Fit to heightFit the width, which suits long documents, or the height
Rotate left, Rotate rightOnly in a read-only viewer: turn the image on display
DownloadOnly with download: true: save the image on display

Fit to width and Fit to height stay pressed while their fit holds. No button fits the whole image again: press 0, or zoom out all the way.

With toolbar: false, the toolbar and the edit bar are hidden, and you can build your own controls with the methods.

Read-only viewer ​

With editable: false, the Edit button goes away, and the toolbar gets two buttons that rotate the image. That rotation only changes the display, and nothing is saved: Download saves the image without it. Use it for documents that people should read but not change, such as the attachments of a record.

ts
const pic = new PaperPic(container, {
  src: '/receipt.jpg',
  alt: 'A receipt',
  editable: false,
})

Control the view from code ​

The methods zoomIn(), zoomOut(), zoomTo() and fit() do what the buttons do, and getState() tells you the zoom and the fit in effect:

ts
pic.fit('width')
pic.zoomTo(2)

const { zoom, fit } = pic.getState()

Released under the MIT License.