Getting started
This page takes you from installing PaperPic to showing an image and getting the edited result back. Choose your framework in any example, and the others on the site follow.
To try PaperPic before you install it, open a starter in StackBlitz: JavaScript, Vue, React or Nuxt. Each one shows a sample receipt to edit.
Installation
npm install @paperpic/corepnpm add @paperpic/coreyarn add @paperpic/corebun add @paperpic/coreWithout a bundler
The core also loads from a CDN that serves the files of the package as they are, such as jsDelivr. Import dist/index.js from a module script, and link dist/style.css:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@paperpic/core@0.9/dist/style.css" />
<div id="viewer" style="height: 480px"></div>
<script type="module">
import { PaperPic } from 'https://cdn.jsdelivr.net/npm/@paperpic/core@0.9/dist/index.js'
const pic = new PaperPic(document.querySelector('#viewer'), { src: '/receipt.jpg', alt: 'A receipt' })
</script>Browsers only start a worker from the origin of the page, so with the package on a CDN the editing runs on the main thread: it works, but large photos freeze the page for a moment on Apply. Serve the files from your own origin, or use a bundler, to keep the worker. The Vue and React components need a bundler.
Show an image
The viewer fills its container, so give the container a height. Import the stylesheet once in your app.
<div id="viewer" style="height: 480px"></div>import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/receipt.jpg',
alt: 'A receipt',
})src takes a URL from the same origin as your page, a File that the user picks, such as from an <input type="file">, or a Blob. alt describes the image for screen readers. Loading images covers images from other origins, formats and load errors.
Get the edited image
The pencil in the toolbar starts the edit mode. When the user applies the changes, the result replaces the image on display, and the apply event hands it to your code:
pic.on('apply', ({ blob }) => upload(blob))Here upload stands for your own code, such as:
async function upload(blob: Blob) {
const body = new FormData()
body.append('image', blob)
await fetch('/api/documents', { method: 'POST', body })
}The blob keeps the format of the source when it is JPEG, PNG or WebP, and is PNG when the user cleaned up the document into black and white. Format and quality explains how PaperPic tells the format, and the output option changes it.
Remove the viewer
The Vue and React components create the viewer when they mount and destroy it when they unmount. With the core, call destroy() when you take the viewer out of the page, such as when the route changes:
pic.destroy()It removes the viewer from its container, stops its listeners and its worker, and frees the memory of the image. To show it again, create a new one.
Only show the image
To show a document that people should not change, turn editing off with editable: false. The Viewer page shows it at work.
Requirements
PaperPic works in the browsers of Baseline Widely Available. Performance and support covers the worker, Content Security Policy, server-side rendering and large photos.
Next steps
- Loading images: where the image comes from, and which formats open.
- Viewer: zoom, fit and read-only use.
- Editing: the edit mode, the tools, Undo and Apply.
- Options: everything you can configure.
- Troubleshooting: when the viewer does not look or work as it should.